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. C语言printf格式化输出修饰符详解

    转换说明 输出 %a,%A 浮点数.十六进制数和p-计数法(C99) %c 一个字符 %d 有符号十进制数 %e,%E 浮点数,e计数法 %f 浮点数,十进制计数法 %g,%G 根据数值不同自动选择% ...

  2. 多线程-synchronized

    引言 synchronized是Java线程同步中的一个重要的概念,synchronized是独占锁(互斥锁),同时也是可重入锁(可重入锁一定程度上避免了死锁的问题,内部是关联一个计数器,加一次锁计数 ...

  3. Atitit. 图像处理jpg图片的压缩 清理垃圾图片 java版本

    Atitit. 图像处理jpg图片的压缩  清理垃圾图片 java版本 1. 清理图片压缩图片尺寸 1 2. 所以要使用ImageWriter 1 3. Thumbnails质量压缩builder.o ...

  4. Postman 常用操作

    一,入参为json 格式 实例:添加学生信息,这个接口是用来讲入参式json 类型的 二,上传文件 三,添加header 信息 实例:获取所有学生信息 四,添加cookie以及身份验证 实例:学生金币 ...

  5. JSON学习【转自慕课网】

    视频网址 从慕课网视频里的PPT截图过来的,因为是用PHP讲的,而且后面讲的一般,所以只截取了前两节课的基础内容,其实只看一下第一张PPT就可以了.

  6. Vim -&gt; 移动光标

    Movement Command 前翻一屏 Ctrl + F 后翻一屏 Ctrl + B 前翻半屏 Ctrl + D 后翻半屏 Ctrl + U 前滚一行 Ctrl + E 后滚一行 Ctrl + Y ...

  7. Python中sort以及sorted函数初探

    sorted(...) Help on built-in function sorted in module __builtin__: sorted(...) sorted(iterable, cmp ...

  8. 【JMeter4.0】之遇到的问题总结(持续更新)

    目录: 一.图形结果监听器选择文件报错 二.TCP取样器压测出现500 错误,读取数据超时 三.如何解决JMeter通过JDBC访问MySQL的问题总结 四.如何解决JMeter通过JDBC访问Ora ...

  9. AJAX防止多次请求

    ajax诠释 ajax 的全称是Asynchronous JavaScript and XML,其中,Asynchronous 是异步的意思,它有别于传统web开发中采用的同步的方式. ajax所包含 ...

  10. storm 入门原理介绍_AboutYUN

    转自:http://www.aboutyun.com/thread-7394-1-1.html 了解Storm:http://www.aboutyun.com/thread-9547-1-2.html ...