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.
在战争中使所有城市通过公路连接起来是很重要的。如果一个城市被敌军占领,所有通往这个城市或者从这个城市开始的公路都会被封锁。我们必须立刻知道我们需要修建其他的哪条公路使得剩余的城市可以互相连接。给出地图,上面标明了所有可用公路,你需要告诉我们需要修建的公路数量
比如,如果我们有3个城市和两条公路连接了城市1-城市2,城市1-城市3. 然后城市1被敌军占领,我们必须修建1条公路那就是城市2-城市3
Input Specification:
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.
第一行有3个数字N(<1000),M和K。分别代表城市总数量,剩余公路数量,被封锁的城市数量。之后有M 行,每一行描述了一条公路连接的2个城市,城市从1到N编号,最后一行有K个数字,代表了我们关心的城市
Output Specification:
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
如果这K个城市被封锁了,我们需要修建几条公路
图的遍历:
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int N=1111;
vector<int> G[N];
bool vis[N];
int currentPoint;//当前需要删除的顶点编号
//dfs(v)遍历顶点v所在的连通块
void dfs(int v){
if(v==currentPoint) return;//遍历到已删除顶点v时,返回
vis[v]=true;
for(int i=0;i<G[v].size();i++){
if(vis[G[v][i]]==false)
dfs(G[v][i]);
}
}
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);
G[a].push_back(b);
G[b].push_back(a);
}
for(int query=0;query<k;query++){
scanf("%d",¤tPoint);
memset(vis,false,sizeof(vis));//初始化vis数组为false
int block=0;
for(int i=1;i<=n;i++){
if(i!=currentPoint&&vis[i]==false){
dfs(i);
block++;
}
}
printf("%d\n",block-1);
}
return 0;
}
并查集
#include<cstdio>
#include<vector>
using namespace std;
const int N=1111;
vector<int> G[N];
int father[N];
bool vis[N];
int findFather(int x){
int a=x;
while(x!=father[x]){
x=father[x];
}
while(a!=father[a]){
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void Union(int a,int b){
int faA=findFather(a);
int faB=findFather(b);
if(faA!=faB){
father[faA]=faB;
}
}
void init(){
for(int i=1;i<N;i++){
father[i]=i;
vis[i]=false;
}
}
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);
G[a].push_back(b);
G[b].push_back(a);
}
int currentPoint;
for(int query=0;query<k;query++){
scanf("%d",¤tPoint);
init();
for(int i=1;i<=n;i++){
for(int j=0;j<G[i].size();j++){
int u=i,v=G[i][j];
if(u==currentPoint||v==currentPoint) continue;
Union(u,v);
}
}
int block=0;
for(int i=1;i<=n;i++){
if(i==currentPoint) continue;
int fa_i=findFather(i);
if(vis[fa_i]==false){
block++;
vis[fa_i]=true;
}
}
printf("%d\n",block-1);
}
return 0;
}