1. 程式人生 > >pat甲級1013

pat甲級1013

每次 nta from 個數 需要 color ted ios ble

1013 Battle Over Cities (25)(25 分)

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city~1~-city~2~ and city~1~-city~3~. Then if city~1~ is occupied by the enemy, we must have 1 highway repaired, that is the highway city~2~-city~3~.

Input

Each input file contains one test case. Each case starts with a line containing 3 numbers N (&lt1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input

3 2 3
1 2
1 3
1 2 3

Sample Output

1
0
0
需要修的路個數就是連通分量的個數減一。
 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 
 5 int N, M, K;
 6 int G[1001][1001
]; 7 8 int connectCnt(int city); 9 void dfs(int s, bool marked[]); 10 11 int main() 12 { 13 int i, j, v, w, c; 14 cin >> N >> M >> K; 15 for (i = 0; i < M; i++) 16 { 17 cin >> v >> w; 18 G[v][w] = 1; 19 G[w][v] = 1; 20 } 21 for (i = 0; i < K; i++) 22 { 23 cin >> c; 24 vector<int> vec; 25 for (j = 1; j <= N; j++) 26 { 27 if (G[c][j] == 1) 28 { 29 G[c][j] = 0; 30 G[j][c] = 0; 31 vec.push_back(j); 32 } 33 } 34 cout << connectCnt(c) << endl; 35 for (int j : vec) 36 { 37 G[c][j] = 1; 38 G[j][c] = 1; 39 } 40 } 41 return 0; 42 } 43 44 int connectCnt(int city) 45 { 46 int v, cnt = 0; 47 bool marked[1001] = {}; 48 for (v = 1; v <= N; v++) 49 { 50 if (!marked[v] && v != city) 51 { 52 dfs(v, marked); 53 cnt++; 54 } 55 } 56 return cnt - 1; 57 } 58 59 void dfs(int s, bool marked[]) 60 { 61 marked[s] = true; 62 for (int v = 1; v <= N; v++) 63 { 64 if (!marked[v] && G[s][v]) 65 dfs(v, marked); 66 } 67 }

但是查看別人的博客發現不需要每次把和被占領的城市相連的道路標記為0再標記為1,只需要把marked數組被占領的城市標記為1即可。。

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int N, M, K;
 5 int G[1001][1001];
 6 
 7 int connectCnt(int city);
 8 void dfs(int s, bool marked[]);
 9 
10 int main()
11 {
12     int i, v, w, c;
13     scanf("%d%d%d", &N, &M, &K);
14     for (i = 0; i < M; i++)
15     {
16         scanf("%d%d", &v, &w);
17         G[v][w] = 1;
18         G[w][v] = 1;
19     }
20     for (i = 0; i < K; i++)
21     {
22         scanf("%d", &c);
23         printf("%d\n", connectCnt(c));
24     }
25     return 0;
26 }
27 
28 int connectCnt(int city)
29 {
30     int v, cnt = 0;
31     bool marked[1001] = {};
32     marked[city] = true;
33     for (v = 1; v <= N; v++)
34     {
35         if (!marked[v])
36         {
37             dfs(v, marked);
38             cnt++;
39         }
40     }
41     return cnt - 1;
42 }
43 
44 void dfs(int s, bool marked[])
45 {
46     marked[s] = true;
47     for (int v = 1; v <= N; v++)
48     {
49         if (!marked[v] && G[s][v])
50             dfs(v, marked);
51     }
52 }

 

pat甲級1013