HDU 3394 双联通求块
Railway
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 665 Accepted Submission(s): 270
Problem Description
There are some locations in a park, and some of them are connected by roads. The park manger needs to build some railways along the roads, and he would like to arrange tourist routes to each circuit. If a railway belongs to more than one tourist routes, there might be clash on it, and if a railway belongs to none tourist route, it doesn’t need to build.
Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.
Now we know the plan, and can you tell us how many railways are no need to build and how many railways where clash might happen.
Input
The Input consists of multiple test cases. The first line of each test case contains two integers, n (0 < n <= 10000), m (0 <= m <= 100000), which are the number of locations and the number of the railways. The next m lines, each line contains two integers, u, v (0 <= u, v < n), which means the manger plans to build a railway on the road between u and v.
You can assume that there is no loop and no multiple edges.
The last test case is followed by two zeros on a single line, which means the end of the input.
You can assume that there is no loop and no multiple edges.
The last test case is followed by two zeros on a single line, which means the end of the input.
Output
Output the number of railways that are no need to build, and the number of railways where clash might happen. Please follow the format as the sample.
Sample Input
8 10 0 1 1 2 2 3 3 0 3 4 4 5 5 6 6 7 7 4 5 7 0 0
Sample Output
1 5
一个公园中有 n 个景点,景点之间通过无向的道路来连接,如果至少两个环公用一条路,路上的游客就会发生冲突;如果一条路不属于任何的环,这条路就没必要修
问,有多少路不必修,有多少路会发生冲突
解题思路:首先tarjin缩点,找出点双联通分量,计算每个分量内的点数,和边数,假如点数大于边数,则不需要,假如点数小于边数,则冲突。
代码:
1 #include<iostream>
2 #include<stdio.h>
3 #include<string.h>
4 #include<algorithm>
5 #include<map>
6 #include<vector>
7 using namespace std;
8 struct node
9 {
10 int next,to;
11 }edge[200020];
12 int head[20000],tol,flag[20000],low[20000],dfn[20000],indexx,top,Stack[490000],instack[20000],ans1,ans2,block[20000];
13 void add(int u,int v)
14 {
15 edge[tol].to=v;
16 edge[tol].next=head[u];
17 head[u]=tol++;
18 }
19 void tarjin(int u,int pre)
20 {
21 low[u]=dfn[u]=++indexx;
22 Stack[top++]=u;instack[u]=1;
23 int i,v;
24 for(i=head[u];i!=-1;i=edge[i].next)
25 {
26 v=edge[i].to;
27 if(!dfn[v])
28 {
29 tarjin(v,u);
30 if(low[u]>low[v])low[u]=low[v];
31 if(low[v]>=dfn[u])
32 {
33 block[0]=0;
34 memset(flag,0,sizeof(flag));
35 int p;
36 do
37 {
38 p=Stack[--top];
39 instack[p]=0;
40 flag[p]=1;
41 block[++block[0]]=p;
42 }while(p!=v);
43 block[++block[0]]=u;flag[u]=1;
44 int a,b;
45 a=block[0];b=0;
46 for(int f=1;f<=block[0];f++)
47 {
48 int g=block[f];
49 for(int h=head[g];h!=-1;h=edge[h].next)
50 {
51 int r=edge[h].to;
52 if(flag[r])b++;
53 }
54 }
55 b/=2;
56 if(a>b)ans1+=b;
57 else if(a<b)ans2+=b;
58 }
59 }
60 else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
61 }
62 }
63 int main()
64 {
65 int i,j,k,m,n;
66 //freopen("data1.in","r",stdin);
67 //freopen("data.out","w",stdout);
68 while(~scanf("%d%d",&n,&m))
69 {
70 if(n==0&&m==0)break;
71 memset(head,-1,sizeof(head));tol=0;
72 while(m--)
73 {
74 scanf("%d%d",&i,&j);
75 add(i,j);
76 add(j,i);
77 }
78 //cout<<tol<<endl;
79 memset(dfn,0,sizeof(dfn));
80 memset(instack,0,sizeof(instack));
81 top=indexx=ans1=ans2=0;
82 for(i=1;i<=n;i++)if(!dfn[i])tarjin(i,i);
83 printf("%d %d\n",ans1,ans2);
84 }
85 return 0;
86 }
2 #include<stdio.h>
3 #include<string.h>
4 #include<algorithm>
5 #include<map>
6 #include<vector>
7 using namespace std;
8 struct node
9 {
10 int next,to;
11 }edge[200020];
12 int head[20000],tol,flag[20000],low[20000],dfn[20000],indexx,top,Stack[490000],instack[20000],ans1,ans2,block[20000];
13 void add(int u,int v)
14 {
15 edge[tol].to=v;
16 edge[tol].next=head[u];
17 head[u]=tol++;
18 }
19 void tarjin(int u,int pre)
20 {
21 low[u]=dfn[u]=++indexx;
22 Stack[top++]=u;instack[u]=1;
23 int i,v;
24 for(i=head[u];i!=-1;i=edge[i].next)
25 {
26 v=edge[i].to;
27 if(!dfn[v])
28 {
29 tarjin(v,u);
30 if(low[u]>low[v])low[u]=low[v];
31 if(low[v]>=dfn[u])
32 {
33 block[0]=0;
34 memset(flag,0,sizeof(flag));
35 int p;
36 do
37 {
38 p=Stack[--top];
39 instack[p]=0;
40 flag[p]=1;
41 block[++block[0]]=p;
42 }while(p!=v);
43 block[++block[0]]=u;flag[u]=1;
44 int a,b;
45 a=block[0];b=0;
46 for(int f=1;f<=block[0];f++)
47 {
48 int g=block[f];
49 for(int h=head[g];h!=-1;h=edge[h].next)
50 {
51 int r=edge[h].to;
52 if(flag[r])b++;
53 }
54 }
55 b/=2;
56 if(a>b)ans1+=b;
57 else if(a<b)ans2+=b;
58 }
59 }
60 else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
61 }
62 }
63 int main()
64 {
65 int i,j,k,m,n;
66 //freopen("data1.in","r",stdin);
67 //freopen("data.out","w",stdout);
68 while(~scanf("%d%d",&n,&m))
69 {
70 if(n==0&&m==0)break;
71 memset(head,-1,sizeof(head));tol=0;
72 while(m--)
73 {
74 scanf("%d%d",&i,&j);
75 add(i,j);
76 add(j,i);
77 }
78 //cout<<tol<<endl;
79 memset(dfn,0,sizeof(dfn));
80 memset(instack,0,sizeof(instack));
81 top=indexx=ans1=ans2=0;
82 for(i=1;i<=n;i++)if(!dfn[i])tarjin(i,i);
83 printf("%d %d\n",ans1,ans2);
84 }
85 return 0;
86 }

浙公网安备 33010602011771号