Source:

PAT A1013 Battle Over Cities (25 分)

Description:

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 city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<), 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 Specification:

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

Keys:

Attention:

  • 逻辑上删除结点即可,即遍历到w时直接return

Code:

 /*
Data: 2019-05-16 21:26:01
Problem: PAT_A1013#Battle Over Cities
AC: 24:40 题目大意:
给一个图,拿掉一个顶点及其边,问至少添加几条边,可以保证图的连通
输入:
第一行给出:结点数N<1e3,边数M,查询次数K
接下来M行,给出V1和V2,表示结点间存在边
接下来一行,依次给出破坏顶点序号(1~N),输出需要添加的边数 */
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=1e3+,INF=1e9;
int grap[M][M],vis[M],n,m,k,w; void DFS(int u)
{
if(u==w)
return;
vis[u]=;
for(int v=; v<=n; v++)
if(vis[v]== && grap[u][v]==)
DFS(v);
} int Travel()
{
int cnt=-;
fill(vis,vis+M,);
for(int i=; i<=n; i++)
{
if(vis[i]== && i!=w)
{
DFS(i);
cnt++;
}
}
return cnt;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d%d%d", &n,&m,&k);
fill(grap[],grap[]+M*M, INF);
for(int i=; i<m; i++)
{
int v1,v2;
scanf("%d%d",&v1,&v2);
grap[v1][v2]=;
grap[v2][v1]=;
}
for(int i=; i<k; i++)
{
scanf("%d", &w);
printf("%d\n", Travel());
} return ;
}

PAT_A1013#Battle Over Cities的更多相关文章

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

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

  2. PAT1013: Battle Over Cities

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

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

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

  4. PAT 1013 Battle Over Cities

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

  5. PAT Battle Over Cities [未作]

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

  6. 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 ...

  7. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  8. PAT 1013 Battle Over Cities(并查集)

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

  9. pat1013. Battle Over Cities (25)

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

随机推荐

  1. 大学,助你成长or 让你堕落?

    不管是论坛.贴吧.还是博客,都或多或少能够看到诸如对大学教育的反思.抨击之类的文章.至于什么是大学,大学又该怎样度过.大学是助你成长还是让你堕落了?我想这应该是一个见仁见智的问题.作为一个过来人,结合 ...

  2. 【你你你你在开玩笑吧】什么叫凭借纯兴趣搞ACM?涨姿势了

        好长时间不扯淡了,今天扯个玩玩,吐个槽.     在上海回济南的列车上,回顾起这两天在携程codingtrip颁奖仪式上大牛们的种种心得,姿势涨了不少,着实涨了不少啊.我这样的渣渣毕竟图样图森 ...

  3. DBI(i80)/DPI(RGB)/DSI【转】

    本文转载自:http://blog.csdn.net/liuxd3000/article/details/17437317 (1)DBI接口 A,也就是通常所讲的MCU借口,俗称80 system接口 ...

  4. E20170816-mk

    deque 即双端队列.是一种具有队列和栈的性质的数据结构. revert  vi. 恢复; 重提; 回到…上; <律>归还;   n. 归属; 恢复原来信仰的人; Indicator   ...

  5. 原生JS---3

    原生js学习笔记3——数组 定义数组 两种方式定义一个数组: 1. var array1 = new array(1, 2, 3, 4); 2. var array2 = [1, 2, 3, 4]; ...

  6. python-day2 切片,格式化输出,函数

    1.切片:取元素  格式;变量名[M:N:K]  M 表示开始元素索引值,   N  表示结束元素索引值(不包含索引值本身)   K 表示步长,隔几个切一次 例子:a='hello python' p ...

  7. Django day01 web应用程序 , http协议

    一:web应用程序1.什么是web应用程序 是一种可以通过web访问的应用程序,最大的好处就是, 只要有浏览器,用户就能很容易访问到应用程序 2. web应用程序的优缺点 缺点: 应用程序强调了浏览器 ...

  8. DIV+CSS设计时浏览器兼容性

          近期用Div+css做了个企业网站,在浏览器中测试的时候确发现在IE7中显示正常的页面,在ie6中非常混乱,当时第一感觉就想到了兼容问题,可是百思不得其解应该从哪下手,经过一两天的查资料, ...

  9. Redis(四)-配置

    Redis 配置 Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过 CONFIG 命令查看或设置配置项. 语法 Redis CONFIG 命令格式如下: ...

  10. Spark Streaming概述

    Spark Streaming是一种构建在Spark上的实时计算框架,它扩展了Spark处理大规模流式数据的能力. 其中包括:资源管理框架,Apache YARN.Apache Mesos:基于内存的 ...