It is vitally important to has all the cities connected by highways in a war. If a city was occupied by the enemy, all of the highways From/toward that city was closed. We must know immediately if we need to repair all other highways to keep the rest of the cities connected. Given the map of cities which has all the remaining highways marked, you is supposed to tell the number of highways need To be repaired, quickly.
For example, if we have 3 cities and 2 highways connecting City1-city2 and City1-city3. Then if city1 are occupied by the enemy, we must has 1 highway repaired, which is the highway city2-city3.
Input
Each input file contains the one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which is 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 is the numbers of the cities of Highway. The cities is 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
#include <iostream> #include <string.h>using namespace std; #include <queue> #define N 1001int main () { int n,m,k,i,j,t,c1,c2; BOOL Highways[n][n]; int concern[n]; memset (highways,0,sizeof (highways)); Memset (concern,0,sizeof (concern)); cin>>n; cin>>m; cin>>k; for (i=0;i<m;i++) {cin>>c1; cin>>c2; Highways[c1][c2]=true; Highways[c2][c1]=true; } for (i=0;i<k;i++) {cin>>concern[i]; } for (i=0;i<k;i++) {int visit[n],count,tmp; Queue<int> Q; memset (visit,0,sizeof (visit)); Visit[concern[i]]=1; count=0; for (j=1;j<=n;j++) {if (visit[j]!=1) {queue<int> dq; Visit[j]=1; Dq.push (j); For each city directly connected to the occupied city do BFS while (!dq.empty ()) {Tmp=dq.front (); Dq.pop (); for (t=0;t<=n;t++) { if (highways[tmp][t]!=false&&visit[t]!=1) {Dq.push (t); Visit[t]=1; }}//* If the existing node can traverse all the points, do not need to add edge, exit. Otherwise, the independent "island" +1, through the next directly connected node and not access to the node, to find the next island * * count++; }} cout<<count-1<<endl;/* need to add the path, that is, to connect to the individual islands the minimum required * *} return 0;}
1013. Battle over Cities (25)