PAT Advanced 1013 Battle Over Cities (25分)

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 (<), 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

PAT 图的遍历,这道题考察了图的遍历。我们要检查城市未连通的量,仅需要进行一次深度遍历,

进行计数,然后减去初始的1次,剩余的就是需要进行连通的了

#include <iostream>
#include <algorithm>
#define VERNUM 1010
using namespace std;
bool edge[VERNUM][VERNUM];
bool vis[VERNUM];
int N, M, K;
void dfs(int v) {
    vis[v] = true;
    for(int i = 1; i <= N; i++){
        if(vis[i] == false && edge[v][i] == 1)
            dfs(i);
    }
}
int main() {
    /** N 城市个数 M 剩余的高速公路数量 K 检查过的城市数量 **/
    /** 该题要求,去除一个顶点,判定是否是连通图 */
    scanf("%d%d%d", &N, &M, &K);
    int a, b, tmp_city;
    for(int i = 0; i < M; i++) {
        scanf("%d%d", &a, &b);
        edge[a][b] = edge[b][a] = 1;
    }
    for(int i = 0; i < K; i++) {
        fill(vis, vis + VERNUM, false);
        scanf("%d", &tmp_city);
        int cnt = 0;
        vis[tmp_city] = true;
        for(int j = 1; j <= N; j++) {
            if(vis[j] == false) {
                dfs(j);
                cnt++;
            }
        }
        printf("%d\n", cnt - 1);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/littlepage/p/12247299.html

时间: 2024-12-11 21:39:00

PAT Advanced 1013 Battle Over Cities (25分)的相关文章

PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

1013 Battle Over Cities (25 分) 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 oth

1013 Battle Over Cities (25分) DFS | 并查集

1013 Battle Over Cities (25分) 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 othe

PAT:1013. Battle Over Cities (25) AC

#include<stdio.h> #include<string.h> #include<vector> using namespace std; const int MAX=1010; int n,m,k; //城市数,高速公路数,查询次数 int DELETE; //要删除的点 vector<int> ADJ[MAX]; //邻接表 bool vis[MAX]; void DFS(int s) { if(DELETE==s) return; //表示该

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) 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

1013. Battle Over Cities (25)——PAT (Advanced Level) Practise

题目信息: 1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 32000 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 th

PAT (Advanced Level) 1013. Battle Over Cities (25)

并查集判断连通性. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> using namespace std; const int maxn=1010; struct Edge { int u,v; }e[maxn*maxn]; int n,m,k; int f[maxn]; i

PAT甲题题解-1013. Battle Over Cities (25)-求联通分支个数

题目就是求联通分支个数删除一个点,剩下联通分支个数为cnt,那么需要建立cnt-1边才能把这cnt个联通分支个数求出来怎么求联通分支个数呢可以用并查集,但并查集的话复杂度是O(m*logn*k)我这里用的是dfs,dfs的复杂度只要O((m+n)*k)这里k是指因为有k个点要查询,每个都要求一下删除后的联通分支数.题目没给定m的范围,所以如果m很大的话,dfs时间会比较小. for一遍1~n个点,每次从一个未标记的点u开始dfs,标记该dfs中访问过的点.u未标记过,说明之前dfs的时候没访问过

1013. Battle Over Cities (25)(连通分量个数 、 并查集)

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

1013. Battle Over Cities (25)

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