1013. Battle Over Cities (25)
来源:互联网 发布:杭州网络传媒有限公司 编辑:程序博客网 时间:2024/05/29 04:08
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
用并查集计算无向图的连通分量
</pre>#include <iostream><br />#include <vector><br />#include <cstring><br />using namespace std;<br />const int maxn = 1005;<br />int n,m,k;<br />int ufs[maxn];<br />vector<int> G[maxn];<br />int find_set(int x){return x == ufs[x] ? x : ufs[x] = find_set(ufs[x]);}<br />int main(){<br /><span style="white-space:pre"></span>cin>>n>>m>>k;<br /><span style="white-space:pre"></span>int a,b,t,cnt;<br /><span style="white-space:pre"></span>for(int i=0;i<m;i++){<br /><span style="white-space:pre"></span>cin>>a>>b;<br /><span style="white-space:pre"></span>a--;b--;<br /><span style="white-space:pre"></span>G[a].push_back(b);<br /><span style="white-space:pre"></span>G[b].push_back(a);<br /><span style="white-space:pre"></span>}<br /><span style="white-space:pre"></span>for(int i=0;i<k;i++){<br /><span style="white-space:pre"></span>cin>>t;<br /><span style="white-space:pre"></span>t--;cnt = n - 1;<br /><span style="white-space:pre"></span>for(int j=0;j<n;j++)ufs[j] = j;<br /><span style="white-space:pre"></span>for(int j=0;j<n;j++){<br /><span style="white-space:pre"></span>if(j == t)continue;<br /><span style="white-space:pre"></span>for(int l=0;l<G[j].size();l++){<br /><span style="white-space:pre"></span>if(G[j][l] != t){<br /><span style="white-space:pre"></span>int r1 = find_set(j),r2 = find_set(G[j][l]);<br /><span style="white-space:pre"></span>if(r1 != r2){<br /><span style="white-space:pre"></span>cnt--;<br /><span style="white-space:pre"></span>ufs[r1] = r2;<br /><span style="white-space:pre"></span>}<br /><span style="white-space:pre"></span>}<br /><span style="white-space:pre"></span>}<br /><span style="white-space:pre"></span>}<br /><span style="white-space:pre"></span>cout<<cnt-1<<endl;<br /><span style="white-space:pre"></span>}<br /><span style="white-space:pre"></span>system("pause");<br /><span style="white-space:pre"></span>return 0;<br />}<br />
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 木马逆名欺骗:利用unicode控制符RLO
- Sublime Text 安装Package Control
- 矩阵快速幂
- HTML5开发移动web应用——Sencha Touch篇(12)
- 程序员到底是一个什么职业?
- 1013. Battle Over Cities (25)
- 在ubuntu下安装shadowsocks
- Configuring Beans in the Spring IoC Container
- 算法竞赛入门经典第五章习题
- php字符串处理函数(下)
- 点语法访问属性
- Java设计模式(十九)----备忘录模式
- HDU 2048 神、上帝以及老天爷(递推)
- 面试笔试杂项积累-leetcode 206-210