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

  1. 3 2 3
  2. 1 2
  3. 1 3
  4. 1 2 3

Sample Output

  1. 1
  2. 0
  3. 0
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<vector>
  4. #include<queue>
  5. using namespace std;
  6. int G[][] = {};
  7. int visit[] = {}, cnt = ;
  8. int N, M, K;
  9. void dfs(int vt, int lost){
  10. visit[vt] = ;
  11. for(int i = ; i <= N; i++){
  12. if(G[vt][i] == && visit[i] == && i != lost && vt != lost){
  13. dfs(i, lost);
  14. }
  15. }
  16. }
  17. int main(){
  18. scanf("%d%d%d", &N, &M, &K);
  19. int tempA, tempB;
  20. for(int i = ; i < M; i++){
  21. scanf("%d%d", &tempA, &tempB);
  22. G[tempA][tempB] = G[tempB][tempA] = ;
  23. }
  24. for(int i = ; i < K; i++){
  25. int lost;
  26. scanf("%d", &lost);
  27. for(int j = ; j <= N; j++)
  28. visit[j] = ;
  29. visit[lost] = ;
  30. cnt = ;
  31. for(int j = ; j <= N; j++){
  32. if(visit[j] == ){
  33. dfs(j, lost);
  34. cnt++;
  35. }
  36. }
  37. printf("%d\n", cnt - );
  38. }
  39. cin >> N;
  40. return ;
  41. }

总结:

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. jQuery 事件 - triggerHandler() 方法

    定义和用法 triggerHandler() 方法触发被选元素的指定事件类型.但不会执行浏览器默认动作,也不会产生事件冒泡. triggerHandler() 方法与 trigger() 方法类似.不 ...

  2. java内部类 和外部类的区别

    java 内部类和静态内部类的区别  详细连接https://www.cnblogs.com/aademeng/articles/6192954.html 下面说一说内部类(Inner Class)和 ...

  3. MyBatis的demo

    把以前写的关于mybatis的demo放在这边,以便查看. 目录结构: package com.test.mybatis.util; import java.io.IOException; impor ...

  4. /proc/diskstats

    读取磁盘统计信息,如下所示: linux-HpdBKE:~ # cat /proc/diskstats sda sda1 sda2 dm- dm- dm- sda为整个硬盘的统计信息,sda1为第一个 ...

  5. Ajax 长轮询

    长轮询:客户端向服务器发送Ajax请求,服务器接到请求后hold住连接,直到有新消息才返回响应信息并关闭连接,客户端处理完响应信息后再向服务器发送新的请求. 优点:在无消息的情况下不会频繁的请求. 缺 ...

  6. CSS3之box-sizing属性

    box-sizing本身有三个属性:content-box(默认).border-box和padding-box. content-box:border与padding均不算入width中: bord ...

  7. Thread的其他属性方法

    from threading import Thread,currentThread,active_count import time def task(): print('%s is running ...

  8. HttpWebRequest using Basic authentication

    System.Net.CredentialCache credentialCache = new System.Net.CredentialCache(); credentialCache.Add( ...

  9. poj-1236(强连通分量)

    题意:给你n个点,每个点可能有指向其他点的单向边,代表这个点可以把软件传给他指向的点,然后解决两个问题, 1.问你最少需要给几个点,才能使所有点都能拿到软件: 2.问你还需要增加几条单向边,才能使任意 ...

  10. bzoj4152-[AMPPZ2014]The_Captain

    Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. Input 第一行包含一个正整数n(2 ...