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 Input
3 2 3
1 2
1 3
1 2 3
Sample Output
1
0
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 Input
3 2 3
1 2
1 3
1 2 3
Sample Output
1
0
0
城市和高速公路网组成一张图,城市是节点,高速公路是连通节点的边,如果某一个城市被占领,即与它连通的高速公路都被破坏,则原来的相互连通的城市群可能不在连通。若某城市被占领后,图中不连通的区域有N块,则需要维修N-1条新的高速路。
判断图的连通性,很显然可以用DFS或者BFS。
这道题做的比较早,代码可以写的更简略些,不过因为解题思想很简单,不再重复做了。
#include <iostream>
#include <vector>
using namespace std;
int N, M, K;
vector<int> cities;
vector< vector<int> > cur_map;
vector< vector<int> > new_map;
vector<int> visited;
void initialpara()
{
cur_map.resize(N);
new_map.resize(N);
visited.resize(N);
for (int i = 0; i != N; ++i)
{
cur_map[i].resize(N);
new_map[i].resize(N);
for (int j = 0; j != N; ++j)
{
cur_map[i][j] = 0;
}
}
return ;
}
#if 0
void initialmap()
{
cur_map = new int*[N];
new_map = new int*[N];
for (int i = 0; i != N; ++i)
{
cur_map[i] = new int[N];
new_map[i] = new int[N];
for (int j = 0; j != N; ++j)
{
cur_map[i][j] = 0;
}
}
return ;
}
void destorymap()
{
for (int i = 0; i != N; ++i)
{
delete[] cur_map[i];
cur_map[i] = NULL;
delete[] new_map[i];
new_map[i] = NULL;
}
delete[] cur_map;
cur_map = NULL;
delete[] new_map;
new_map = NULL:
}
#endif
void copymap(int index)
{
for (int i = 0; i != N; ++i)
{
for (int j = 0; j != N; ++j)
{
if (i == index || j == index)
{
new_map[i][j] = 0;
}
else
{
new_map[i][j] = cur_map[i][j];
}
}
}
}
void dfsmap(int target)
{
visited[target] = 1;
for (int i = 0; i != N; ++i)
{
if (0 == visited[i] && 1 == new_map[target][i])
{
dfsmap(i);
}
}
}
int partsofmap()
{
int parts = 0;
for (int i = 0; i != N; ++i)
{
visited[i] = 0;
}
for (int i = 0; i != N; ++i)
{
if (0 == visited[i])
{
++parts;
dfsmap(i);
}
}
return parts;
}
int main()
{
int ni, nj, target;
cin >> N >> M >> K;
initialpara();
for (int i = 0; i != M; ++i)
{
cin >> ni >> nj;
cur_map[ni-1][nj-1] = 1;
cur_map[nj-1][ni-1] = 1;
}
for (int i = 0; i != K; ++i)
{
cin >> target;
copymap(target-1);
cout << partsofmap()-2 << endl;
}
return 0;
}