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

思路

这道题的本质问题其实就是求去掉某一点后的图有几个连通子图。那么需要重修的道路就是——连通子图数-1(保证余下节点全连通)。所以只要在去掉一个点后dfs下这个图确定连通子图个数就行。那么:

1.用一个数组表示点是否被遍历。

2.对于一个被入侵的城市city,只需要在dfs之前将这个城市标记为已遍历就行,这样dfs相当于只遍历去掉了这个点的图,即模拟了city从图中被去掉的情况。

代码

#include<iostream>
#include<vector>
using namespace std; vector<vector<int>> graph(,vector<int>(,-)); void dfs(const int city,const int num,vector<bool>& visits)
{
visits[city] = true;
for(int i = ;i <= num;i++)
{
if(city == i)
continue;
if(graph[city][i] == && !visits[i])
{
dfs(i,num,visits);
}
}
} int main()
{
int N,M,K;
while(cin >> N >> M >> K)
{
while(M--)
{
int a,b;
cin >> a >> b;
graph[a][b] = graph[b][a] = ;
} while(K--)
{
int countRoad = ;
vector<bool> visits(N + ,false);
int city;
cin >> city;
visits[city] = true;
for(int i = ;i <= N;i++)
{
if(!visits[i])
{
countRoad++;
dfs(i,N,visits);
}
}
cout << countRoad - << endl;
}
}
}

PAT1013: Battle Over Cities的更多相关文章

  1. pat1013. Battle Over Cities (25)

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

  2. PAT-1013 Battle Over Cities (25 分) DFS求连通块

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

  3. PAT1013. Battle Over Cities(邻接矩阵、邻接表分别dfs)

    //采用不同的图存储结构结构邻接矩阵.邻接表分别dfs,我想我是寂寞了吧,应该试试并查集,看见可以用并查集的就用dfs,bfs代替......怕了并查集了 //邻接矩阵dfs #include< ...

  4. PAT---1013. Battle Over Cities (25)

    这道题目的意思是:在战争时代,如果一个城市被敌人占领了,那么和该城市相连的道路都必须关闭,我们必须把剩下的城市(即不包括被敌人占领的城市)连接起来. 举个例子,我们有3个城市,C1,C2,C3,C1和 ...

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

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

  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. 【Qt编程】QWT在QtCreator中的安装与使用

    由于导师项目的需要,需要画图,二维+三维.三维图我用的是Qt+opengl,二维图我决定使用qwt工具库来加快我的项目进展,毕竟还有期末考试.关于Qt+opengl的使用有时间的话以后再介绍.     ...

  2. 虚拟机安装Ubuntu14.04打开FireFox提示Server not found

    虚拟机安装Ubuntu14.04打开FireFox提示Server not found 我采用VMware安装ubuntu14.04的,VMware的网络是配置采用NAT模式(用于共享主机的IP地址) ...

  3. Swift之GCD 使用指南2

    Grand Central Dispatch大中枢派发:joy: 或俗称 GCD 是一件极其强大的武器.它为你提供了很多底层工具(比如队列和信号量),你可以组合这些工具来实现自己想要的多线程效果.不幸 ...

  4. android 热修补之andfix实践

    首先有这方面需要的请到https://github.com/xiangzhihong/andfixDemo/tree/master下载例子 对于网上提供的热补丁修复就不多说了,主要有这3种方式可以实现 ...

  5. SharePoint 门户网站的图片轮播-页面定制

    这个想法是自己突然的一个想法,想想我们经常用SharePoint做门户网站,不知道你们多数项目都是怎么完成的,我们客户要求的效果都还是很严格的,所有展现起来,还是很漂亮的,但是很多时候的效果,还是难以 ...

  6. Markdown语法及编辑器

    宗旨 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文件应该可以直接以纯文本发布,并且看起来不会像是由许多标签或是格式指令所构成. ...

  7. Github上的原文XMPP环境搭建步骤,英语能力差不多的可以看看

    Getting started using XMPPFramework on iOS Here is a post on StackOverflow describing how to install ...

  8. obj-c编程17:键值观察(KVO)

    说完了前面一篇KVC,不能不说说它的应用KVO(Key-Value Observing)喽.KVO类似于ruby里的hook功能,就是当一个对象属性发生变化时,观察者可以跟踪变化,进而观察或是修正这个 ...

  9. 如何检测被锁住的Oracle存储过程

    今天遇到了这个情况,然后在网上找了到了这篇文章,借鉴过来做参考吧! 1.查看是哪一个存储过程被锁住 查V$DB_OBJECT_CACHE视图 select * from V$DB_OBJECT_CAC ...

  10. JS实现鼠标滚动事件,兼容IE9,FF,Chrome.

    <!-- 废话不多说,直接贴代码 --><script type="text/javascript" src="jquery.min.js"& ...