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 city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.
Input
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), 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 Input3 2 3 1 2 1 3 1 2 3Sample Output
1 0 0备注:bfs 写了两个版本, 递归和非递归(用stack),都AC了。注意图中的节点是从1到N编号的,刚开始经常忘了,造成数组越界
#include<string.h>
#include<iostream>
#include<stack>
using namespace std;
#define MAXNUM 1001
#define VISITED 1
#define NOVISITED 0
// Note that the nodes are from 1 to N
// Not 0 to N-1
void Initialize(int visited[],int n)
{
int i;
for(i=1;i<=n;i++)
visited[i] = NOVISITED;
}
// DFS from node start
// recursive version
void dfs(int start, int *graph[], int n, int visited[],int concerned_city)
{
int i;
visited[start] = VISITED;
for(i=1;i<=n;i++)
{
if(i==concerned_city)
continue;
if(graph[start][i]==1 && visited[i]==NOVISITED)
dfs(i,graph,n,visited,concerned_city);
}
}
// DFS from node start
// iterative version, using stack
/*
void dfs(int start, int *graph[], int n, int visited[],int concerned_city)
{
int i;
stack<int> s;
s.push(start);
while(!s.empty())
{
int current = s.top();
s.pop();
if(visited[current]==NOVISITED)
{
visited[current] = VISITED;
for(i=1;i<=n;i++)
{
if(i==concerned_city)
continue;
if(graph[current][i]==1 && visited[i]==NOVISITED)
s.push(i);
}
}
}
}
*/
int main()
{
int n,m,k;
int *graph[MAXNUM];
int visited[MAXNUM];
int i,j;
int h1,h2;
int city[MAXNUM],concerned_city;
int count;
cin>>n>>m>>k;
for(i=1;i<=n;i++)
{
graph[i] = (int *)malloc(sizeof(int *)*(n+1));
memset(graph[i],0,n+1);
}
for(i=0;i<m;i++)
{
cin>>h1>>h2;
graph[h1][h2]=1;
graph[h2][h1]=1;
}
for(i=0;i<k;i++)
cin>>city[i];
for(i=0;i<k;i++)
{
Initialize(visited,n);
concerned_city = city[i];
visited[concerned_city] = VISITED;
// count the number of conneteced components using dfs
count = 0;
for(j = 1;j <= n ;j++)
{
//dfs from j
if(j==concerned_city)
continue;
if(visited[j]==NOVISITED)
{
dfs(j,graph,n,visited,concerned_city);
count++;
}
}
cout<<count-1<<endl;
}
return 0;
}