# 1013 Battle Over Cities（PAT 甲等 C++实现）

1013 Battle Over Cities （25 point(s)）

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 city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​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.

### 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.

### Sample Input:

3 2 3
1 2
1 3
1 2 3


### Sample Output:

1
0
0

#include <iostream>
#include <algorithm>
using namespace std;

void DFS(int v[][1005],bool visit[],int n,int node){
visit[node]=true;
for(int i=1;i<=n;++i){
if(visit[i]==false && v[node][i]==1){
DFS(v,visit,n,i);
}
}
}

// 1103 Integer Factorization （30 point(s)）
int main(void){
int n,m,k;   // n=cities m=remaining highways k=checked
cin>>n>>m>>k;// 顶点 边 查询数

int v[1005][1005]={0};     // 邻接矩阵
int temp1,temp2;
for(int i=1;i<=m;++i){    // 边连通
cin>>temp1>>temp2;
v[temp1][temp2]=1;    // 无向边
v[temp2][temp1]=1;
}

for(int i=1;i<=k;++i){    // 查询次数
bool visit[1005]={false}; // 标记
//fill(visit, visit+1005, false); // 赋值
int temp,sum=0;
cin>>temp;
visit[temp]=true;

for(int j=1;j<=n;++j){
if(visit[j]==false){
DFS(v,visit,n,j);
++sum;
}
}
cout<<(sum-1)<<endl;
}

return 0;
} // jinzheng 2018.9.19 17:22


#include <iostream>
#include <vector>
using namespace std;

vector<int> G[1005];
int current;

// 深度优先遍历
void dfs(bool visit[],int v){
if(v==current) return;
visit[v] = true;

for(int i=0;i<G[v].size();++i){
if(visit[G[v][i]]==false){
dfs(visit,G[v][i]);
}
}
}

// 1013 Battle Over Cities （25 point(s)）
int main(void){
int n,m,k;
cin>>n>>m>>k;

for(int i=1;i<=m;++i){
int temp1,temp2;
cin>>temp1>>temp2;
G[temp1].push_back(temp2);
G[temp2].push_back(temp1);
}

for(int i=1;i<=k;++i){
cin>>current;
bool visit[1005]={false};

int sum=0;
for(int j=1;j<=n;++j){
if(j!=current && visit[j]==false){
dfs(visit,j);
++sum;
}
}
cout<<(sum-1)<<endl;
}

return 0;
} // jinzheng 2018.9.19 21:09


04-19 37

02-25 137

05-17 47

05-18 517

07-12 1207

08-30 232

07-23 397

01-18 422

01-13 262

#### 1013.Battle Over Cities

©️2020 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。