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. spring-cloud-feign使用@RequetParam错误:QueryMap parameter must be a Map: int

    错误: QueryMap parameter must be a Map: int spring-cloud-feign处理@RequestParam和Spring MVC的不一样,Spring MV ...

  2. Unreal Engine 4 Camera Lag(摄影机延迟)

    以官方的Third Person Template为样例,Character蓝图中的USpringArmComponent就实现了摄影机和场景碰撞和交互等大部分的功能了. 要实现摄影机延时,仅仅须要改 ...

  3. 我们的一个已投产项目的高可用数据库实战 - mongo 副本集的搭建具体过程

    我们的 mongo 副本集有三台 mongo 服务器:一台主库两台从库. 主库进行写操作,两台从库进行读操作(至于某次读操作到底路由给了哪台,仲裁决定).实现了读写分离.这还不止,假设主库宕掉,还能实 ...

  4. Oracle数据库软件标准版的一个限制:仅仅能用一个rman channel

    Oracle数据库软件标准版的一个限制:仅仅能用一个rman channel Restrictions in "Standard Edition" Rman channel all ...

  5. ExtJs4.1布局具体解释

    Border布局: Ext.onReady(function(){     Ext.QuickTips.init();     Ext.create('Ext.container.Viewport', ...

  6. 刚開始学习的人非常有用之chm结尾的參考手冊打开后无法正常显示

    从网上下载了struts2的參考手冊.chm(本文适用全部已.chm结尾的文件)不能正常打开使用. 如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/ ...

  7. linux端口号与PID的互相查询

    最近用linux在玩Tomcat,启动的时候总是会报错(8080/8009/8005) 于是整理了一下网上零乱的查看PID和端口的命令,以备记录. 1.由端口号查询PID号 首先myeclipse报错 ...

  8. B1024 生日快乐 递归。。。

    bzoj1024叫生日快乐,其实很简单,但是没看出来就很尴尬... Description windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕.现在包括win ...

  9. bzoj 1191 [ HNOI 2006 ] 超级英雄Hero —— 二分图匹配

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1191 就是二分图匹配的裸题: 注意题目要求是第一次匹配失败就退出!没仔细看题差点丢失1A. ...

  10. bzoj2503 相框——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2503 思路题: 首先,这种问题应该注意到答案只跟度数有关,跟其他什么连接方法之类的完全无关: ...