PAT (Advanced Level) Practise 1013 Battle Over Cities
来源:互联网 发布:淘宝文具店推荐 编辑:程序博客网 时间:2024/06/14 05:51
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
题意:有n个城市,m条道路,k个询问,若毁去其中一个城市,至少建几条道路才能使剩余的城市联通
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;int n,m,k,t,f[1009],a[1009];int x[1009000],y[1009000];int get(int p){ return p==f[p]?p:f[p]=get(f[p]);}int main(){ scanf("%d %d %d",&n,&m,&k); for (int i=0; i<m; i++) scanf("%d %d",&x[i],&y[i]); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) f[j]=j; a[i]=n-2; for(int j=0; j<m&&a[i]>0; j++) { if(x[j]==i||y[j]==i) continue; int xx=get(x[j]),yy=get(y[j]); if(xx==yy) continue; else f[xx]=yy,a[i]--; } } while(k--) { scanf("%d",&t); printf("%d\n",a[t]); } return 0;}
- PAT (Advanced Level) Practise 1013 Battle Over Cities (25)
- PAT (Advanced Level) Practise 1013 Battle Over Cities
- 1013. Battle Over Cities @ PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1013. Battle Over Cities (25)
- PAT (Advanced Level) Practise 1013. Battle Over Cities (25)
- PAT (Advanced Level) Practise 1013. Battle Over Cities (25)
- Pat(Advanced Level)Practice--1013(Battle Over Cities)
- PAT(Advanced Level) 1013 - Battle Over Cities(连通图)
- 1013. Battle Over Cities (25)——PAT (Advanced Level) Practise
- 【PAT Advanced Level】1013. Battle Over Cities (25)
- [PAT (Advanced Level) ]1013.Battle Over Cities 解题文档
- 浙大 PAT Advanced level 1013. Battle Over Cities
- 【PAT】【Advanced Level】1013. Battle Over Cities (25)
- PAT (Advanced) 1013. Battle Over Cities (25)
- PAT 1013 Battle Over Cities
- pat 1013 Battle Over Cities
- PAT 1013 Battle Over Cities
- PAT (Top Level) Practise 1001Battle Over Cities - Hard Version (35)
- [BZOJ3550][ONTAK2010]Vacation(单纯形)
- Node.js 使用 npm 有问题,转cnpm镜像
- leetcode学习笔记
- 1016. 部分A+B (15)
- Win8下安装SQL Server 2008的过程总结
- PAT (Advanced Level) Practise 1013 Battle Over Cities
- 红黑树
- 算法作业5
- Python之数组去除重复元素
- toArray方法浅析
- 几种云,很形象的比喻
- 1017. A除以B (20)
- C++ traits技术浅谈
- OS X下mysql创建用户与修改密码