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 ...
随机推荐
- 远程重启WIN服务器
- c语言循环单链表
/************************************************************************* > File Name: singleLin ...
- jsp tld的function 自定义方法扩展
引入方式示例: <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %> 写法示例: <? ...
- mysql获取插入的id主键
long _retId = -1; Connection con = null; try { con = queryRunner.getDataSource().getConnection(); qu ...
- Java 使用 HttpClient调用https 最新源码 JDK7+ apache4.3+
在项目使用https方式调用别人服务的时候,以前要写很多的代码,现在框架封装了很多,所以不用再写那么多了. 网上看了一下,都是很老的版本,用过时的DefaultHttpClient. 以spring为 ...
- spring boot +mybatis分页查询
这是spring boot集合mybatis的分页查询. pom依赖: <!-- 分页插件 --> <dependency> <groupId>com.github ...
- YUV转为RGB24及IplImage格式(I420和YV12)及Java版实现
http://blog.csdn.net/xy365/article/details/18735849 ———————————————————————————————————————————————— ...
- 01 Servlet & Jsp 技术概述
Servlet 介绍 servlet 是运行在web服务器或应用服务器上的java程序, 它是一个中间层, 负责连接来自web浏览器或其他http客户端的请求和HTTP服务器上的数据库或应用程序. 为 ...
- EasyUI DataGrid 编辑单元格
如下图: 现改为单击某个单元格只对此单元格进行可编辑 <TABLE>标记添加 onClickCell <table id="dg" class="eas ...
- linux输入法
sudo apt-get install fcitx-table-wbpy安装fcitx 中文是灰色的就拖到第一个 桌面右上角有个设置,可以添加拼音 拼音特别难用