View Code
1 #include<stdio.h>
2 #include<string.h>
3
4 #define N 1010
5 #define M N*N
6
7 int low[N],dfn[N],sub[N],index;
8 int tail[N],eNum;
9 struct Edge
10 {
11 int e,next;
12 }edge[M];
13
14 int Min(int x,int y)
15 {
16 if(x>y)return y;
17 return x;
18 }
19 int Max(int x,int y)
20 {
21 if(x>y)return x;
22 return y;
23 }
24 void Add(int x,int y)
25 {
26 edge[eNum].e=y;
27 edge[eNum].next=tail[x];
28 tail[x]=eNum++;
29
30 edge[eNum].e=x;
31 edge[eNum].next=tail[y];
32 tail[y]=eNum++;
33 }
34 void Build()
35 {
36 eNum=1;index=0;
37 memset(tail,-1,sizeof(tail));
38 memset(dfn,-1,sizeof(tail));
39 for(int i=1;i<=N;i++)
40 sub[i]=1;
41 sub[1]=0;
42 }
43 void Tarjan(int u)
44 {
45 int v;
46 dfn[u]=low[u]=++index;
47 for(int i=tail[u];i!=-1;i=edge[i].next)
48 {
49 v=edge[i].e;
50 if(dfn[v]==-1)
51 {
52 Tarjan(v);
53 low[u]=Min(low[u],low[v]);
54 if(dfn[u]<=low[v])
55 sub[u]++;
56 }
57 else low[u]=Min(low[u],dfn[v]);
58 }
59 }
60 int main()
61 {
62 int x,y,n,cas=1;
63 while(scanf("%d",&x),x)
64 {
65 int flag=0;n=0;
66 Build();
67 scanf("%d",&y);
68 Add(x,y);
69 n=Max(n,Max(x,y));
70 while(1)
71 {
72 scanf("%d",&x);
73 if(x==0)break;
74 scanf("%d",&y);
75 Add(x,y);
76 n=Max(n,Max(x,y));
77 }
78 Tarjan(1);
79 printf("Network #%d\n",cas++);
80 for(int i=1;i<=n;i++)
81 {
82 if(sub[i]>1)
83 {
84 flag=1;
85 printf(" SPF node %d leaves %d subnets\n",i,sub[i]);
86 }
87 }
88 if(flag==0)
89 printf(" No SPF nodes\n");
90 printf("\n");
91 }
92 return 0;
93
2 #include<string.h>
3
4 #define N 1010
5 #define M N*N
6
7 int low[N],dfn[N],sub[N],index;
8 int tail[N],eNum;
9 struct Edge
10 {
11 int e,next;
12 }edge[M];
13
14 int Min(int x,int y)
15 {
16 if(x>y)return y;
17 return x;
18 }
19 int Max(int x,int y)
20 {
21 if(x>y)return x;
22 return y;
23 }
24 void Add(int x,int y)
25 {
26 edge[eNum].e=y;
27 edge[eNum].next=tail[x];
28 tail[x]=eNum++;
29
30 edge[eNum].e=x;
31 edge[eNum].next=tail[y];
32 tail[y]=eNum++;
33 }
34 void Build()
35 {
36 eNum=1;index=0;
37 memset(tail,-1,sizeof(tail));
38 memset(dfn,-1,sizeof(tail));
39 for(int i=1;i<=N;i++)
40 sub[i]=1;
41 sub[1]=0;
42 }
43 void Tarjan(int u)
44 {
45 int v;
46 dfn[u]=low[u]=++index;
47 for(int i=tail[u];i!=-1;i=edge[i].next)
48 {
49 v=edge[i].e;
50 if(dfn[v]==-1)
51 {
52 Tarjan(v);
53 low[u]=Min(low[u],low[v]);
54 if(dfn[u]<=low[v])
55 sub[u]++;
56 }
57 else low[u]=Min(low[u],dfn[v]);
58 }
59 }
60 int main()
61 {
62 int x,y,n,cas=1;
63 while(scanf("%d",&x),x)
64 {
65 int flag=0;n=0;
66 Build();
67 scanf("%d",&y);
68 Add(x,y);
69 n=Max(n,Max(x,y));
70 while(1)
71 {
72 scanf("%d",&x);
73 if(x==0)break;
74 scanf("%d",&y);
75 Add(x,y);
76 n=Max(n,Max(x,y));
77 }
78 Tarjan(1);
79 printf("Network #%d\n",cas++);
80 for(int i=1;i<=n;i++)
81 {
82 if(sub[i]>1)
83 {
84 flag=1;
85 printf(" SPF node %d leaves %d subnets\n",i,sub[i]);
86 }
87 }
88 if(flag==0)
89 printf(" No SPF nodes\n");
90 printf("\n");
91 }
92 return 0;
93