http://pat.zju.edu.cn/contests/pat-practise/1013
来源:互联网 发布:js中绑定含参数函数 编辑:程序博客网 时间:2024/04/30 15:30
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 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 Input3 2 31 21 31 2 3Sample Output
100
- #include<iostream>
- #include<cstdio>
- #include<memory.h>
- #include<algorithm>
- #include<cstring>
- #include<queue>
- #include<cmath>
- #include<cstdlib>
- #include<iomanip>
- #include<string>
- using namespace std;
- int map[1001][1001];
- int visit[1001];
- int n;
- void dfs(int x){
- visit[x] = 1;
- for(int i=1;i<=n;++i){
- if(map[x][i] && !visit[i]){
- dfs(i);
- }
- }
- }
- int getblock(){
- int ans = 0;
- for(int i=1;i<=n;++i){
- if(!visit[i]){
- ans++;
- dfs(i);
- }
- }
- return ans;
- }
- int main(){
- //freopen("in.txt", "r", stdin);
- int m, k;
- int a, b;
- int tmp;
- while(cin>>n>>m>>k){
- memset(map, 0, sizeof(map));
- for(int i=0;i<m;++i){
- scanf("%d%d", &a, &b);
- map[a][b] = map[b][a] = 1;
- }
- for(int i=0;i<k;++i){
- memset(visit, 0, sizeof(visit));
- scanf("%d", &tmp);
- visit[tmp] = 1;
- int ans = getblock();
- printf("%d\n", ans-1);
- }
- }
- //fclose(stdin);
- return 0;
- }
- http://pat.zju.edu.cn/contests/pat-practise/1013
- http://pat.zju.edu.cn/contests/pat-practise/1030
- http://pat.zju.edu.cn/contests/pat-practise/1034
- http://pat.zju.edu.cn/contests/pat-practise/1016
- http://pat.zju.edu.cn/contests/pat-practise/1032
- http://pat.zju.edu.cn/contests/pat-practise/1037
- http://pat.zju.edu.cn/contests/pat-practise/1021
- http://pat.zju.edu.cn/contests/pat-practise/1040
- http://pat.zju.edu.cn/contests/pat-practise/1038
- http://pat.zju.edu.cn/contests/pat-practise/1025
- http://pat.zju.edu.cn/contests/pat-practise/1023
- http://pat.zju.edu.cn/contests/pat-practise/1029
- http://pat.zju.edu.cn/contests/pat-practise/1035
- http://pat.zju.edu.cn/contests/pat-practise/1024
- http://pat.zju.edu.cn/contests/pat-practise/1031
- http://pat.zju.edu.cn/contests/pat-practise/1028
- http://pat.zju.edu.cn/contests/pat-practise/1020
- http://pat.zju.edu.cn/contests/pat-practise/1015
- http://pat.zju.edu.cn/contests/pat-practise/1014
- android Notification的使用
- 两数的最大值
- 关于java数据结构 在什么情况下使用什么样的数据结构的总结
- 越吃越胖的食物
- http://pat.zju.edu.cn/contests/pat-practise/1013
- 人民日报反驳我国地图曾承认钓鱼岛属日本说法-野田-钓鱼岛-国有化
- 调用网上在线聊天
- http://pat.zju.edu.cn/contests/pat-practise/1012
- Tizen简介
- 职场真言——让你变得更加“专业”
- CLI/C++中混合类的使用
- 网络分析笔记
- 分享浏览的文章、图片等信息到个人空间、微博……