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. CLOUD常用表

    采购采购订单(t_PUR_POOrder, t_PUR_POOrderEntry)-收料通知单(T_PUR_Receive,T_PUR_ReceiveEntry)-采购入库单(T_STK_INSTOC ...

  2. Excel文件读取的两种方式

    1.Pandas库的读取操作 from pandas import read_excel dr=read_excel(filename,header) dr#dataframe数据 dw=DataFr ...

  3. Django--权限信息操作

    一 . 权限控制 表结构的设计 rbca(Role Based Access Control)  基于角色的权限控制 3个model  5张表 class User(models.Model): # ...

  4. 通过view实现rest api接口

    Django rest framwork之view 基于Django的View实现Json数据的返回: # _*_ encoding:utf-8 _*_ __author__ = 'LYQ' __da ...

  5. python数学第七天【期望的性质】

  6. Yii2常用ActiveRecord用法

    1.多表连表查询与对象关联查询 public function getWmsCheck(){ return $this->hasOne(\core\models\WmsCheck::classN ...

  7. Java多线程之单例模式(线程安全)

    package org.study2.javabase.ThreadsDemo.sync; /** * @Auther:GongXingRui * @Date:2018/9/20 * @Descrip ...

  8. 百度编辑器UEditor使用方法

    http://www.cnblogs.com/lionden/archive/2012/07/13/ueditor.html 介绍图片上传:http://uikoo9.com/blog/detail/ ...

  9. java代码自动下载Spring Boot用户手册

    本示例演示Spring Boot 1.5.9.RELEASE版本的用户手册下载 pom.xml <?xml version="1.0" encoding="UTF- ...

  10. python之旅第八篇--异常

    判断类与对象关系 isinstance #判断对象obj是否是由cls类创建的 class Foo(object): pass obj = Foo() print isinstance(obj,Foo ...