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<cstdio>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int G[][] = {};
int visit[] = {}, cnt = ;
int N, M, K;
void dfs(int vt, int lost){
visit[vt] = ;
for(int i = ; i <= N; i++){
if(G[vt][i] == && visit[i] == && i != lost && vt != lost){
dfs(i, lost);
}
}
}
int main(){
scanf("%d%d%d", &N, &M, &K);
int tempA, tempB;
for(int i = ; i < M; i++){
scanf("%d%d", &tempA, &tempB);
G[tempA][tempB] = G[tempB][tempA] = ;
}
for(int i = ; i < K; i++){
int lost;
scanf("%d", &lost);
for(int j = ; j <= N; j++)
visit[j] = ;
visit[lost] = ;
cnt = ;
for(int j = ; j <= N; j++){
if(visit[j] == ){
dfs(j, lost);
cnt++;
}
}
printf("%d\n", cnt - );
}
cin >> N;
return ;
}

总结:

1、题意:给出一个无向图G,再给出一个要去掉的节点,求图G在去掉该节点之后的连通分量个数。

2、要去掉lost的点,但在图G上直接将它与所有的点的边置0是不行的,因为不止一个查询,置0后无法恢复。可以在dfs传入lost的点,遍历的时候避开该点即可。

3、计数连通分量个数:利用visit数组,由于一次从点A开始的搜索会把所有和A联通的节点置1。所以可以从1到N每个节点使用一次dfs,当它的visit为0时,说明它和之前访问的节点不属于同一个连通分量。

4、还可以使用并查集:在输入每条边时,判断边上的两个点是否在同一个集合,如果在则不做改变,如果不在,则对两个集合做并。需要路径压缩。

A1013. Battle Over Cities的更多相关文章

  1. PAT A1013 Battle Over Cities (25 分)——图遍历,联通块个数

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  2. PAT甲级——A1013 Battle Over Cities

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  3. PAT_A1013#Battle Over Cities

    Source: PAT A1013 Battle Over Cities (25 分) Description: It is vitally important to have all the cit ...

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

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

  5. PAT1013: Battle Over Cities

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  6. PAT-Top1001. Battle Over Cities - Hard Version (35)

    在敌人占领之前由城市和公路构成的图是连通图.在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通.修复的代价越大,意味着这个城市越重要. ...

  7. PAT 1013 Battle Over Cities

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

  8. PAT Battle Over Cities [未作]

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

  9. PTA (Advanced Level) 1013 Battle Over Cities

    Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If ...

随机推荐

  1. MyBaits全局配置文件的各项标签1

    ■dtd约束     <!DOCTYPE configuration           PUBLIC "-//mybatis.org//DTD Config 3.0//EN" ...

  2. python爬虫-1

    import resquests #import urllib.request from bs4 import BeautifulSoup from collections import Ordere ...

  3. Django--CRM--QueryDict, 模糊搜索, 加行级锁

    一 . QueryDict的修改 # QueryDict正常是不允许修改的,要想往里面添加内容,需要另mutable=True dic = request.GET print(dic) # <Q ...

  4. 使用mysqlbinlog恢复数据

    前提:mysql数据库开启了binlog日志,并且有对应的日志文件 起因:今天由于同事对数据库的误操作不小心删除了一条数据 方法一:通过binlog日志文件恢复数据 通过mysqlbinlog恢复My ...

  5. 初识Anrdiod SDK

    概念 SDK:(software development kit)软件开发工具包.被软件开发工程师用于为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件的开发工具的集合. 因此,Android ...

  6. 魔术方法:__set、__get

    __set: 在设置对象里边不能直接设置(或没有)的属性值的时候,自动去被调用 class Track { private $track_name; public function __set($na ...

  7. C#使用MemoryStream类读写内存

    MemoryStream和BufferedStream都派生自基类Stream,因此它们有很多共同的属性和方法,但是每一个类都有自己独特的用法.这两个类都是实现对内存进行数据读写的功能,而不是对持久性 ...

  8. controller层负责创建类传递类给service;service层负责逻辑编写调用dao层 将编写后的类传递到dao层,保证事务的正确性;dao层负责数据的持久化

    controller层负责创建类传递类给service:service层负责逻辑编写调用dao层 将编写后的类传递到dao层,保证事务的正确性:dao层负责数据的持久化

  9. poj2739(尺取法+质数筛)

    题意:给你一个数,问这个数能否等于一系列连续的质数的和: 解题思路:质数筛打出质数表:然后就是尺取法解决: 代码: #include<iostream> #include<algor ...

  10. [离散时间信号处理学习笔记] 3. 一些基本的LTI系统

    首先我们需要先对离散时间系统进行概念上的回顾: $y[n] = T\{ x[n] \}$ 上面的式子表征了离散时间系统,也就是把输入序列$x[n]$,映射称为$y[n]$的输出序列. 不过上述式子也可 ...