【PAT】1013. Battle Over Cities (25)
2018-03-25 15:30
239 查看
题目描述:
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 FORMAT
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 FORMAT
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.翻译:对于K个城市,分别输出一行,为如果该城市被攻占所需修建的高速公路数。
Sample Input
3 2 31 2
1 3
1 2 3
Sample Output
10
0
解题思路:
对于每个需要检查的城市,对其他城市进行广度优先搜索,如果已搜索到就跳过,否则ccount(需要修建的高速公路数)加一,再搜索该城市。具体细节见代码。#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<vector> #include<queue> #include<algorithm> #define INF 99999999 using namespace std; int N,M,K,d[1010],judge[1010]; vector<int>G[1010]; queue<int> q; void bfs(int a,int except){ d[a]=1; q.push(a); while(!q.empty()){ int temp=q.front();q.pop(); for(int i=0;i<G[temp].size();i++){ if(G[temp][i]!=except&&!d[G[temp][i]]){ q.push(G[temp][i]); d[G[temp][i]]=1; } } } } int main(){ scanf("%d%d%d",&N,&M,&K); int a,b; for(int i=0;i<M;i++){ scanf("%d%d",&a,&b); G[a].push_back(b); G[b].push_back(a); } for(int i=0;i<K;i++)scanf("%d",&judge[i]); for(int i=0;i<K;i++){ a=judge[i]; memset(d,0,sizeof(d)); int ccount=-1; for(int j=1;j<=N;j++){ if(!d[j]&&j!=a)ccount++,bfs(j,a); } printf("%d\n",ccount); } return 0; }
相关文章推荐
- PAT - 甲级 - 1056. Mice and Rice (25)(STL-queue)
- PAT (Advanced Level) 1037. Magic Coupon (25) 求两数组元素相乘最大和,排序
- PAT (Advanced Level) 1047. Student List for Course (25) 哈希,排序,string与char数组的转换
- 1040. 有几个PAT(25)
- PAT 1051. Pop Sequence (25)
- PAT (Basic Level) Practise (中文)1030. 完美数列(25)
- PAT Basic Level 1025. 反转链表(25)
- PAT (Advanced Level) 1062. Talent and Virtue (25) 德才论,结构体排序
- 1060. Are They Equal (25)-PAT甲级真题(科学计数法)
- pat乙级1065. 单身狗(25)
- PAT 1009 Product of Polynomials (25)
- 1043. Is It a Binary Search Tree (25)-PAT甲级真题
- PAT 1012. The Best Rank (25)
- PAT 乙级 1065. 单身狗(25)
- PAT 1017. Queueing at Bank (25)
- PAT 1040. Longest Symmetric String (25)
- PAT乙级—1005. 继续(3n+1)猜想 (25)-native
- PAT.1005. 德才论 (25)
- PAT (Basic Level) Practise (中文)1010. 一元多项式求导 (25)
- 1048. Find Coins (25)-PAT甲级真题