# 1013. Battle Over Cities (25)

400 ms

65536 kB

16000 B

Standard

CHEN, Yue

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
0

#### 采用并查集的C++代码：

#include<bits/stdc++.h>
using namespace std;
struct UFS{//定义并查集类
int*father=nullptr;
int size=0;
UFS(int n){//构造函数，定义一个维度为n的数组，并将元素初始化为其下标
father=new int[n];
for(int i=0;i<n;++i)
father[i]=i;
size=n;
}
~UFS(){//析构函数
delete []father;
}
int findFather(int x){//查找父亲结点并进行路径压缩
if(x==father[x])
return x;
int temp=findFather(father[x]);
father[x]=temp;
return temp;
}
void unionSet(int a,int b){//合并两个集合
int ua=findFather(a),ub=findFather(b);
if(ua!=ub)
father[ua]=ub;
}
int countRoot(){//计算并查集中集合的数量
int num=0;
for(int i=0;i<size;++i)
if(father[i]==i)
++num;
return num;
}
};
vector<vector<int>>graph(1005);//存储图
int N,M,K;
int main(){
scanf("%d%d%d",&N,&M,&K);
for(int i=0;i<M;++i){
int a,b;
scanf("%d%d",&a,&b);
graph[a].push_back(b);
graph[b].push_back(a);
}
while(K--){
int v;
scanf("%d",&v);
UFS ufs(N+1);//定义一个长度为N+1的并查集
for(int i=1;i<N+1;++i)
if(i!=v)//编号为v的结点已经被删除，不统计其边
for(int j:graph[i])
if(j!=v)//如果边的末端不是编号为v的结点
ufs.unionSet(i,j);//合并两个集合
printf("%d\n",ufs.countRoot()-3);//注意编号为0和v的集合均独立，所以需要减3
}
return 0;
}

#### 采用深度优先遍历的c++代码：

#include<bits/stdc++.h>
using namespace std;
vector<vector<int>>graph(1005);//存储图
bool visit[1005]={false};//标记相应编号的结点是否已被访问
void DFS(int v,int deleteV){//深度优先遍历，v表示当前正在访问的结点，deleteV表示删除的结点
visit[v]=true;
for(int i=0;i<graph[v].size();++i){
int temp=graph[v][i];
if(!visit[temp]&&temp!=deleteV)
DFS(temp,deleteV);
}
}
int N,M,K;
int main(){
scanf("%d%d%d",&N,&M,&K);
for(int i=0;i<M;++i){
int a,b;
scanf("%d%d",&a,&b);
graph[a].push_back(b);
graph[b].push_back(a);
}
while(K--){
int v;//删除的结点
scanf("%d",&v);
fill(visit+1,visit+N+1,false);//初始化visit为false
int k=0;//连通分量的数量
for(int i=1;i<N+1;++i)
if(!visit[i]&&i!=v){
DFS(i,v);
++k;
}
printf("%d\n",k-1);
}
return 0;
}

• 擅长领域：
• 算法