1013. Battle Over Cities (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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(并查集)的更多相关文章

  1. pat 1013 Battle Over Cities(25 分) (并查集)

    1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...

  2. PAT 1013 Battle Over Cities

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  3. 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 ...

  4. 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 ...

  5. 1013 Battle Over Cities (25分) DFS | 并查集

    1013 Battle Over Cities (25分)   It is vitally important to have all the cities connected by highways ...

  6. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  7. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  8. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

  9. 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 ...

随机推荐

  1. jfinal的maven配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  2. SQL Like中的逗号分隔符

    SQL Like中的逗号分隔符   在与数据库交互的过程中,我们经常需要把一串ID组成的字符串当作参数传给存储过程获取数据.很多时候我们希望把这个字符串转成集合以方便用于in操作. 有两种方式可以方便 ...

  3. Codeforces Round #265 (Div. 2) B. Inbox (100500)

    Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others ...

  4. poj1860--Currency Exchange

    Bellman-ford算法的反向应用--正循环检查 /** \brief poj 1860 Bellman-Ford * * \param date 2014/7/24 * \param state ...

  5. Atitit。激活某个程序的api attilax总结

    Atitit.激活某个程序的api attilax总结 1. 设置当前某窗口为当前窗口,有几个步骤要做:1.得到窗口句柄FindWindow2.切换键盘输入焦点AttachThreadInput3.显 ...

  6. mysql 两列互转

    1.mysql 一张表两列互转 UPDATE tm_position as a,tm_position as b SET a.gps_longitude = b.gps_latitude,a.gps_ ...

  7. windows编译tensorflow c++库

    1. 准备 windows 10系统.3.6GHz cpu.16G 内存 visual studio 2017 or 2015 下载安装git 下载安装cmake 下载安装swigwin 如果不需要p ...

  8. 浅谈配置文件:spring-servlet.xml(spring-mvc.xml) 与 applicationContext.xml

    在搭建 spring mvc 的框架时,会有2个配置文件必不可少: spring-servlet.xml 和applicationContext.xml.第一次接触spring mvc的工程师可能会对 ...

  9. azkaban入门中遇到的问题

    执行时报错,后来发现他的配置文件中写了相对路径!!所以必须在他的根目录下执行,命令为 nohup  bin/azkaban-web-start.sh  1>/tmp/azstd.out  2&g ...

  10. libubox

    lbubox是openwrt的一个核心库,封装了一系列基础实用功能,主要提供事件循环,二进制格式处理,linux链表实现和一些JSON辅助处理. 它的目的是以动态链接库方式来提供可重用的通用功能,给其 ...