PAT 1013 Battle Over Cities(并查集)
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 Input
3 2 3
1 2
1 3
1 2 3
Sample Output
1
0
0
简单的并查集应用:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>
#include <strstream>
#include <map> using namespace std;
struct Node
{
int x;
int y;
}edge[1005*1005];
int father[10005];
int find(int x)
{
if(father[x]!=x) father[x]=find(father[x]);
return father[x];
}
int n,m,k;
int a;
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++)
scanf("%d%d",&edge[i].x,&edge[i].y); for(int i=1;i<=k;i++)
{
scanf("%d",&a);
for(int j=1;j<=n;j++)
father[j]=j;
for(int j=1;j<=m;j++)
{
if(edge[j].x==a||edge[j].y==a)
continue;
int fx=find(edge[j].x);
int fy=find(edge[j].y);
if(fx!=fy)
father[fx]=fy;
}
int ans=0;
for(int j=1;j<=n;j++)
{
if(j==a) continue;
find(j);
if(father[j]==j)
ans++;
}
printf("%d\n",ans-1);
}
return 0; }
PAT 1013 Battle Over Cities(并查集)的更多相关文章
- pat 1013 Battle Over Cities(25 分) (并查集)
1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT 1013 Battle Over Cities (dfs求连通分量)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT 1013 Battle Over Cities DFS深搜
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
随机推荐
- AssionShop开源B2C电子商务系统-(二)定单流程活动图状态图(转载)
B2C系统的灵魂所在就是定单的流程了,下面展示下定单的最重要的两个动态图,一个是定单流程活动图.还有一个是简单的定单状态图.通过这两张图,基本可以 了解系统定单流的大概情况了啊.图画的只是初步的设想, ...
- memcache命令行
memcache运行状态可以方便的用stats命令显示. 首先用telnet 127.0.0.1 11211 [quit 退出]这样的命令连接上memcache,然后直接输入stats就可以得到当前 ...
- 180508 - 解决有关VIVO的2018-04-01安全补丁导致的APP闪退问题
解决有关VIVO的2018-04-01安全补丁导致的APP闪退问题 [√]问题原因猜测4: 最终解决方案 [√]问题原因猜测3: 尝试解决 [√成功] [×]问题原因猜测2: 尝试解决 [×失败] [ ...
- Atitit.ui控件---下拉菜单选择控件的实现select html
Atitit.ui控件---下拉菜单选择控件的实现select html 1. 调用& model的实现 1 2. -----select.jsp------ 1 1. 调用& m ...
- Myeclipse中误报错误解决办法
下午写jsp页面的时候,用了一个js文件,拖到MyEclipse下了报错,开始还以为是js文件问题,折腾了半天,后来才知道原来是Myeclipse误报错误.真坑爹啊呀~~ 解决方法: 点击你需要忽略错 ...
- HTML5 多图上传
HTML5 多图上传 时间 2014-06-05 16:06:29 月小升博客 原文 http://java-er.com/blog/html5-many-image-upload/ 主题 HTM ...
- poj 3617 Best Cow Line (字符串反转贪心算法)
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9284 Accepted: 2826 Des ...
- 完工尚需绩效指数 TCPI
转自:http://www.cnblogs.com/allenblogs/archive/2010/12/09/1901404.html TCPI To Complete Performance In ...
- android camera之nv21旋转
这周做的一个android的camera开发,需要获取到视频帧数据,并且需要是nv21格式的byte数组,并且视频帧的图像需要是正方向的.和android相机打过交道的都清楚,android的came ...
- java 教程
1.视频教程 http://blog.csdn.net/zhangdaiscott/article/details/18220411 2.书籍教程: 3.学习课程