C++博客-夜幻梦回-文章分类-连通分量http://www.cppblog.com/Ylemzy/category/16411.html足迹的足迹zh-cnTue, 26 Apr 2011 19:02:44 GMTTue, 26 Apr 2011 19:02:44 GMT60pku 3177 Redundant Paths——无向图双连通分量缩点http://www.cppblog.com/Ylemzy/articles/127795.html火碳黑火碳黑Sun, 26 Sep 2010 12:25:00 GMThttp://www.cppblog.com/Ylemzy/articles/127795.htmlhttp://www.cppblog.com/Ylemzy/comments/127795.htmlhttp://www.cppblog.com/Ylemzy/articles/127795.html#Feedback0http://www.cppblog.com/Ylemzy/comments/commentRss/127795.htmlhttp://www.cppblog.com/Ylemzy/services/trackbacks/127795.html Redundant Paths

Description

In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

				7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

				2

题意:给出一个连通图,求至少添加多少条边,使得对于任意两点,不只一条路。即两点间的路去掉一条边还是连通的。
#include <stdio.h>
#include 
<stdlib.h>
#define Min(a, b) a < b ? a : b
#define maxn 5001
struct T
{
    
int v, next;
}fn[maxn 
* 4];
int g[maxn], indegree[maxn], visit[maxn], low[maxn];
void set(int n)
{
    
for (int i = 1; i <= n; i++)
    {
        g[i] 
= -1;
        indegree[i] 
= 0;
        visit[i] 
= 0;
    }    
}
int tarjan(int u, int f, int time)
{
    
int i, v;
    visit[u] 
= 1;
    low[u] 
= time;
    
for (i = g[u]; i != -1; i = fn[i].next)
    {
        v 
= fn[i].v;
        
if (v != f)
        {
            
if(!visit[v])
            {
                time 
= tarjan(v, u, time + 1);
            }
            low[u] 
= Min(low[u], low[v]);
        }
    }
    
return time;
}
int isok(int u, int v)
{
    
int i;
    
for (i = g[u]; i != -1; i = fn[i].next)
    {
        
if (fn[i].v == v)
        {
            
//printf("ooo\n");
            return 0;
        }
    }
    
return 1;
}
int main()
{
    
int n, m, u, v, th, sum, i, j;
    
while (scanf("%d%d"&n, &m) != EOF)
    {
        th 
= 0;
        
set(n);
        
while (m--)
        {
            scanf(
"%d%d"&u, &v);
            
if (isok(u,v))//处理重边,重边在求双连通分量时没影响,但在统计度时,由于重边也要重建,所以度会变多 
            {
                fn[th].v 
= v, fn[th].next = g[u], g[u] = th++;
                fn[th].v 
= u, fn[th].next = g[v], g[v] = th++;
            }
        }
        tarjan(
1-11);
        
for (i = 1; i <= n; i++)
        {
            
for (j = g[i]; j != -1; j = fn[j].next)
            {
                
if (low[i] != low[fn[j].v])
                {
                    indegree[low[i]]
++;
                }
            }
        }
        
for (i = 1, sum = 0; i <= n; i++)
        {
            
if (indegree[i] == 1)
            {
                sum
++;
            }
        }
        printf(
"%d\n", (sum + 1/ 2);
    }
    
//system("pause");
    return 0;
}
/*
首先这道题有重边,如
2 2
1 2
1 2
应该输出 1
2 2
1 2
2 1
*/



火碳黑 2010-09-26 20:25 发表评论
]]>
pku 3352 Road Construction——无向图强连通分量http://www.cppblog.com/Ylemzy/articles/126856.html火碳黑火碳黑Fri, 17 Sep 2010 05:20:00 GMThttp://www.cppblog.com/Ylemzy/articles/126856.htmlhttp://www.cppblog.com/Ylemzy/comments/126856.htmlhttp://www.cppblog.com/Ylemzy/articles/126856.html#Feedback0http://www.cppblog.com/Ylemzy/comments/commentRss/126856.htmlhttp://www.cppblog.com/Ylemzy/services/trackbacks/126856.htmlRoad Construction

Description

It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

Output

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input

10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10

3 3
1 2
2 3
1 3

Sample Output

2
0
题意:给出一个连通图,求至少添加几条边,使得图去掉任意一条边,还是连通的。
强连通分量缩点生成一颗树,求入度为1的连通分量个数n,结果为(n+1)/2;
无向图不用像有向图那样考虑是否该点在栈中,因为u跟v只要有边,说明u一定可以到到达v,v可以到达u;
而有向图,比如1->2, 3->2,假如从1搜索到2,1跟2属于两个独立的强连通分量,而从3搜到2,发现2被搜过,但2不在栈里,所以
不能使low[3] = low[2];所以无向图的tarjan比有向图的简单了很多。

代码:
#include <stdio.h>
#include 
<stdlib.h>
#define Min(a, b) a < b ? a : b
#define Max(a, b) a > b ? a : b
#define maxn 1001
struct edge
{
    
int v, next;
}fn[maxn 
* maxn];
int visit[maxn], g[maxn], du[maxn],  low[maxn];
void set(int n)
{
    
for (int i = 1; i <= n; i++)
    {
        visit[i] 
= 0, g[i] = -1, du[i] = 0;
    }
}
int tarjan(int f, int u, int times)
{
    visit[u] 
= 1;
    low[u] 
= times;
    
int i, v;
    
for (i = g[u]; i != -1; i = fn[i].next)
    {
        v 
= fn[i].v;
        
if (v != f)
        {
            
if (!visit[v])
            {
                times 
= tarjan(u, v, times + 1);
                low[u] 
= Min(low[u], low[v]);
            }
            low[u] 
= Min(low[u], low[v]);
        }
    }
    
return times;
}
int main()
{
    
char a[10], b[10];
    
int ca, n, m, th, u, v, i, times, top, k, j;
    
while (scanf("%d%d"&n, &m) != EOF)
    {
        
set(n);
        th 
= 0;
        
while (m--)
        {
            scanf(
"%d%d"&u, &v);
            fn[th].v 
= v, fn[th].next = g[u], g[u] = th++;
            fn[th].v 
= u, fn[th].next = g[v], g[v] = th++;
        }
        
for (i = 1, times = 0; i <= n; i++)
        {
            
if (!visit[i])
            {
                times 
= tarjan(-1, i, times + 1); 
            }
        }
        
for (i = 1; i <= n; i++)
        {
            
for (j = g[i]; j != -1; j = fn[j].next)
            {
                
if (low[i] != low[fn[j].v])
                {
                    du[low[i]]
++;
                }
            }
        }
        
for (i = 1, k = 0; i <= n; i++)
        {
            
if (du[i] == 1)
            {
                k
++;
            }
        }
        printf(
"%d\n", (k + 1/ 2);
    }
    system(
"pause");
    
return 0;
}
代码2:
#include <stdio.h>
#include 
<stdlib.h>
#define Min(a, b) a < b ? a : b
#define Max(a, b) a > b ? a : b
#define maxn 1001
struct edge
{
    
int v, next;
}fn[maxn 
* maxn];
int visit[maxn], scc[maxn], g[maxn], du[maxn], stack[maxn], dfn[maxn], low[maxn], top, num, hash[maxn];
void set(int n)
{
    
for (int i = 1; i <= n; i++)
    {
        visit[i] 
= 0, scc[i] = -1, g[i] = -1, du[i] = 0, hash[i] = 0;
    }
}
int tarjan(int f, int u, int times)
{
    visit[u] 
= 1;
    stack[top
++= u;
    dfn[u] 
= low[u] = times;
    
int i, v;
    
for (i = g[u]; i != -1; i = fn[i].next)
    {
        v 
= fn[i].v;
        
if (v != f)
        {
            
if (!visit[v])
            {
                times 
= tarjan(u, v, times + 1);
                low[u] 
= Min(low[u], low[v]);
            }
            
else if (scc[v] == -1)
            {
                low[u] 
= Min(low[u], low[v]);
            }
        }
    }
    
if (low[u] == dfn[u])
    {
        num
++;
        
do
        {
            scc[stack[
--top]] = num;
            
//printf("%d ", stack[top]);
        }while (stack[top] != u);//printf("-=-=\n");
    }
    
return times;
}
int main()
{
    
char a[10], b[10];
    
int ca, n, m, th, u, v, i, times, top, k, j;
    scanf(
"%d%d"&n, &m);
        
set(n);
        th 
= 0;
        
while (m--)
        {
            scanf(
"%d%d"&u, &v);
            fn[th].v 
= v, fn[th].next = g[u], g[u] = th++;
            fn[th].v 
= u, fn[th].next = g[v], g[v] = th++;
        }
        
for (i = 1, times = 0, top = 0, num = 0; i <= n; i++)
        {
            
if (!visit[i])
            {
                times 
= tarjan(-1, i, times); 
            }
        }
        
for (i = 1; i <= n; i++)
        {
            hash[i] 
= 1;
            
for (j = g[i]; j != -1; j = fn[j].next)
            {
                
if (scc[i] != scc[fn[j].v])
                {
                    du[scc[i]]
++;
                }
            }
        }
        
for (i = 1, k = 0; i <= num; i++)
        {
            
//printf("[%d] = %d ", i, du[i]);
            if (du[i] == 1)
            {
                k
++;
            }
        }
        printf(
"%d\n", (k + 1/ 2);
    
return 0;
}



火碳黑 2010-09-17 13:20 发表评论
]]>
pku 1236 Network of Schools——强连通分量缩点求入度分量为0个数和出度为0的分量个数http://www.cppblog.com/Ylemzy/articles/126743.html火碳黑火碳黑Thu, 16 Sep 2010 03:05:00 GMThttp://www.cppblog.com/Ylemzy/articles/126743.htmlhttp://www.cppblog.com/Ylemzy/comments/126743.htmlhttp://www.cppblog.com/Ylemzy/articles/126743.html#Feedback0http://www.cppblog.com/Ylemzy/comments/commentRss/126743.htmlhttp://www.cppblog.com/Ylemzy/services/trackbacks/126743.htmlNetwork of Schools

Description

A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.

Input

The first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.

Output

Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.

Sample Input

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2
题意:1.要求出至少发分配多少站点,使所有点都能收到,即求入度为0的分量。
2.求要添加多少点,使任意一个点发送物品,其他点都能收到物品,即求Max(入度为0的分量个数,出度为0的分量个数)。
代码:
#include <stdio.h>
#include 
<stdlib.h>
#define Min(a, b) a < b ? a : b
#define Max(a, b) a > b ? a : b
#define maxn 101
struct node
{
    
int v, next;
}fn[maxn 
* maxn];
int g[maxn], visit[maxn], dfn[maxn], low[maxn], scc[maxn], stack[maxn], top, num, flag1[maxn], flag2[maxn];
void set(int n)
{
    
for (int i = 1; i <= n; i++)
    {
        g[i] 
= -1, scc[i] = -1, visit[i] = 0, flag1[i] = 0 ,flag2[i] = 0;
    }
}
int tarjan(int u, int times)
{
    low[u] 
= dfn[u] = times;
    visit[u] 
= 1;
    stack[top
++= u;
    
int i, v;
    
for (i = g[u]; i != -1; i = fn[i].next)
    {
        v 
= fn[i].v;
        
if (!visit[v])
        {
            times 
= tarjan(v, times + 1);
            low[u] 
= Min(low[u], low[v]);
        }
        
else if (scc[v] == -1)
        {
            low[u] 
= Min(low[u], low[v]);
        }
    }
    
if (low[u] == dfn[u])
    {
        num
++;
        
do
        {
            scc[stack[
--top]] = num;
        }
while (stack[top] != u);
    }
    
return times;
}
void circle(int n)
{
    
int times = 0, i;
    top 
= 0, num = 0;
    
for (i = 1; i <= n; i++)
    {
        
if (!visit[i])
        {
            times 
= tarjan(i, times + 1);
        }
    }
}
int main()
{
    
int n, u, v, i, j, ans1, ans2, th;
    
while (scanf("%d"&n) != EOF)
    {
        th 
= 0;
        
set(n);
        
for (u = 1; u <= n; u++)
        {
            
while (scanf("%d"&v), v)
            {
                fn[th].v 
= v, fn[th].next = g[u], g[u] = th++
            }
        }
        circle(n);
        
for (i = 1; i <= n; i++)
        {
            
for (j = g[i]; j != -1; j = fn[j].next)
            {
                v 
= fn[j].v;
                
if (scc[i] != scc[v])
                {
                    flag1[scc[v]] 
= 1;//scc[v]有入边 
                    flag2[scc[i]] = 1;//scc[i]有出边 
                }
            }
        }
        
for (i = 1, ans1 = 0, ans2 = 0; i <= num; i++)
        {
            
if (!flag1[i])
            {
                ans1
++;
            }
            
if (!flag2[i])
            {
                ans2
++;
            }
        }
        
if (num == 1)
        {
            printf(
"1\n0\n");
            
continue;
        }
        printf(
"%d\n%d\n", ans1, Max(ans1, ans2));
    }
    
//system("pause");
    return 0;
}



火碳黑 2010-09-16 11:05 发表评论
]]>
pku 2553 The Bottom of a Graph——强连通分量http://www.cppblog.com/Ylemzy/articles/126582.html火碳黑火碳黑Tue, 14 Sep 2010 04:14:00 GMThttp://www.cppblog.com/Ylemzy/articles/126582.htmlhttp://www.cppblog.com/Ylemzy/comments/126582.htmlhttp://www.cppblog.com/Ylemzy/articles/126582.html#Feedback0http://www.cppblog.com/Ylemzy/comments/commentRss/126582.htmlhttp://www.cppblog.com/Ylemzy/services/trackbacks/126582.htmlThe Bottom of a Graph

Description

We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E) is called a directed graph.
Let n be a positive integer, and let p=(e1,...,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices (v1,...,vn+1). Then p is called a path from vertex v1 to vertex vn+1 in G and we say that vn+1 is reachable from v1, writing (v1→vn+1).
Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from v, v is also reachable from w. The bottom of a graph is the subset of all nodes that are sinks, i.e., bottom(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}. You have to calculate the bottom of certain graphs.

Input

The input contains several test cases, each of which corresponds to a directed graph G. Each test case starts with an integer number v, denoting the number of vertices of G=(V,E), where the vertices will be identified by the integer numbers in the set V={1,...,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,...,ve,we with the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.

Output

For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.

Sample Input

3 3
1 3 2 3 3 1
2 1
1 2
0

Sample Output

1 3
2
题意:求出强连通分量,判断每个分量是否有出边。按序输出没出边的分量里的点。
代码:
#include <stdio.h>
#include 
<stdlib.h>
#define Min(a, b) a < b ? a : b
#define maxn 5001
struct T
{
    
int v, next;
}fn[maxn 
* maxn];
int g[maxn], visit[maxn], low[maxn], dfn[maxn], stack[maxn], f[maxn], flag[maxn];
int top, id;
void set(int n)
{
    
for (int i = 1; i <= n; i++)
    {
        g[i] 
= -1, visit[i] = 0, f[i] = 0, flag[i] = 0;
    }
}
int dfs(int u, int t)
{
    
int i;
    visit[u] 
= 1;
    stack[top
++= u;
    dfn[u] 
= low[u] = t;
    
for (i = g[u]; i != -1; i = fn[i].next)
    {
        
if (!visit[fn[i].v])
        {
            t 
= dfs(fn[i].v, t + 1);//返回遍历完u的子树的时间 
            low[u] = Min(low[u], low[fn[i].v]);//如果有孩子因为后向边被缩小了low值,则它也相应缩小low值,整个分量的low都取分量中low最小的值。
        }
        
else
        {
            
if (!f[fn[i].v])//如果f[v]还是0,表示u->v是后向边,v还没出过栈过
            {
                low[u] 
= Min(low[u], low[fn[i].v]);
            }
        }
    }
    
if (dfn[u] == low[u])
    {
        id
++;
        
do
        {
            f[stack[
--top]] = id; 
        }
while (top > 0 && stack[top] != u);
    }
    
return t;
}
void tarjan(int n)
{
    
int t = 0;
    id 
= 0;
    
int i;
    
for (i = 1; i <= n; i++)
    {
        
if (!visit[i])
        {
            
//如果i点能与其它点x构成一个分量,则一定能搜到x,否则,它自己便是一个分量,所以这里的搜索不会重复。
            t = dfs(i, t + 1);
        }
    }
}
int main()
{
    
int n, m, u, v, th, k, i, j;
    
while (scanf("%d"&n), n)
    {
        scanf(
"%d"&m);
        th 
=  0;
        
set(n);
        
while (m--)
        {
            scanf(
"%d%d"&u, &v);
            fn[th].v 
= v, fn[th].next = g[u], g[u] = th++;
        }
        tarjan(n);
        
for (i = 1; i <= n; i++)
        { 
            
for (j = g[i]; j != -1; j = fn[j].next)
            {
                
if (f[fn[j].v] != f[i])//两个点的所在分量不同,表示i有出边 
                {
                    
//hash[i] = 1;
                    flag[f[i]] = 1;
                    
break;
                }
            }
        }
        
for (i = 1; i <= n; i++)
        {
            
if (!flag[f[i]])
            {
                
if (i != n)
                {
                    printf(
"%d ", i);
                }
                
else
                {
                    printf(
"%d", i);
                }
            }
        }
        printf(
"\n");
    }
    system(
"pause");
    
return 0;
}
/*
6 8
1 3
1 2
3 5
5 6
2 4
4 6
3 4
4 1

4 5
1 2
2 3
3 1
1 3
3 4

*/




火碳黑 2010-09-14 12:14 发表评论
]]>