HOJ 1007 SPF 求割点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 bool arcs[1001][1001];
5 bool visted[1001];
6 int max_node;
7
8 //traversal of Graph
9 void DSF(int k);
10
11 int main()
12 {
13 int point_i, point_j;
14 int acount = 1;
15 int num_of_subnet;
16 bool isnot;
17
18 point_i = 1002;
19 point_j = 1002;
20
21 while (point_i != 0)
22 {
23 //Initial
24 max_node = 0;
25 for (int i=0; i<1001; i++)
26 {
27 for (int j=0; j<1001; j++)
28 {
29 arcs[i][j] = false;
30 }
31 }
32 //input
33 if (point_i != 1002)
34 {
35 scanf("%d", &point_j);
36 arcs[point_i][point_j] = true;
37 arcs[point_j][point_i] = true;
38 if (point_i > max_node)
39 {
40 max_node = point_i;
41 }
42 if (point_j > max_node)
43 {
44 max_node = point_j;
45 }
46 }
47 while (scanf("%d", &point_i)==1 && point_i!=0)
48 {
49 scanf("%d", &point_j);
50 if (point_i > max_node)
51 {
52 max_node = point_i;
53 }
54 if (point_j > max_node)
55 {
56 max_node = point_j;
57 }
58 arcs[point_i][point_j] = true;
59 arcs[point_j][point_i] = true;
60 }
61
62 //start reslove the problem here
63 //If the graph is not a connected graph, there is no SPF node.
64 isnot = true;
65 for (int i=1; i<=max_node; i++)
66 {
67 visted[i] = false;
68 }
69 DSF(1);
70 for (int i=1; i<=max_node; i++)
71 {
72 if (visted[i] == false)
73 {
74 if (acount == 1)
75 {
76 printf("Network #%d\n", acount ++);
77 }
78 else
79 {
80 printf("\nNetwork #%d\n", acount ++);
81 }
82 goto here;
83 }
84 }
85 //
86 if (acount == 1)
87 {
88 printf("Network #%d\n", acount ++);
89 }
90 else
91 {
92 printf("\nNetwork #%d\n", acount ++);
93 }
94 for (int i=1; i<=max_node; i++)
95 {
96 for (int j=1; j<=max_node; j++)
97 {
98 visted[j] = false;
99 }
100 visted[i] = true;
101 num_of_subnet = 0;
102 for (int k=1; k<=max_node; k++)
103 {
104 if (!visted[k])
105 {
106 DSF(k);
107 num_of_subnet ++;
108 }
109 }
110 if (num_of_subnet > 1)
111 {
112 isnot = false;
113 printf(" SPF node %d leaves %d subnets\n", i, num_of_subnet);
114 }
115 }
116 if (isnot)
117 {
118 here: printf(" No SPF nodes\n");
119 }
120
121 scanf("%d", &point_i);
122 }
123
124 return 0;
125 }
126
127 void DSF(int k)
128 {
129 visted[k] = true;
130 for (int i=1; i<=max_node; i++)
131 {
132 if (arcs[k][i] && !visted[i])
133 {
134 DSF(i);
135 }
136 }
137 }
2 #include <stdlib.h>
3
4 bool arcs[1001][1001];
5 bool visted[1001];
6 int max_node;
7
8 //traversal of Graph
9 void DSF(int k);
10
11 int main()
12 {
13 int point_i, point_j;
14 int acount = 1;
15 int num_of_subnet;
16 bool isnot;
17
18 point_i = 1002;
19 point_j = 1002;
20
21 while (point_i != 0)
22 {
23 //Initial
24 max_node = 0;
25 for (int i=0; i<1001; i++)
26 {
27 for (int j=0; j<1001; j++)
28 {
29 arcs[i][j] = false;
30 }
31 }
32 //input
33 if (point_i != 1002)
34 {
35 scanf("%d", &point_j);
36 arcs[point_i][point_j] = true;
37 arcs[point_j][point_i] = true;
38 if (point_i > max_node)
39 {
40 max_node = point_i;
41 }
42 if (point_j > max_node)
43 {
44 max_node = point_j;
45 }
46 }
47 while (scanf("%d", &point_i)==1 && point_i!=0)
48 {
49 scanf("%d", &point_j);
50 if (point_i > max_node)
51 {
52 max_node = point_i;
53 }
54 if (point_j > max_node)
55 {
56 max_node = point_j;
57 }
58 arcs[point_i][point_j] = true;
59 arcs[point_j][point_i] = true;
60 }
61
62 //start reslove the problem here
63 //If the graph is not a connected graph, there is no SPF node.
64 isnot = true;
65 for (int i=1; i<=max_node; i++)
66 {
67 visted[i] = false;
68 }
69 DSF(1);
70 for (int i=1; i<=max_node; i++)
71 {
72 if (visted[i] == false)
73 {
74 if (acount == 1)
75 {
76 printf("Network #%d\n", acount ++);
77 }
78 else
79 {
80 printf("\nNetwork #%d\n", acount ++);
81 }
82 goto here;
83 }
84 }
85 //
86 if (acount == 1)
87 {
88 printf("Network #%d\n", acount ++);
89 }
90 else
91 {
92 printf("\nNetwork #%d\n", acount ++);
93 }
94 for (int i=1; i<=max_node; i++)
95 {
96 for (int j=1; j<=max_node; j++)
97 {
98 visted[j] = false;
99 }
100 visted[i] = true;
101 num_of_subnet = 0;
102 for (int k=1; k<=max_node; k++)
103 {
104 if (!visted[k])
105 {
106 DSF(k);
107 num_of_subnet ++;
108 }
109 }
110 if (num_of_subnet > 1)
111 {
112 isnot = false;
113 printf(" SPF node %d leaves %d subnets\n", i, num_of_subnet);
114 }
115 }
116 if (isnot)
117 {
118 here: printf(" No SPF nodes\n");
119 }
120
121 scanf("%d", &point_i);
122 }
123
124 return 0;
125 }
126
127 void DSF(int k)
128 {
129 visted[k] = true;
130 for (int i=1; i<=max_node; i++)
131 {
132 if (arcs[k][i] && !visted[i])
133 {
134 DSF(i);
135 }
136 }
137 }
先前一直Time Limit Exceed,之后在求解之前加了对图的连通性的判断,终于AC了。不联通的图是没有割点的。