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. web端log4net输出错误日志到mysql

    1.引用log4net 2.配置log4net.config文件 <?xml version="1.0" encoding="utf-8" ?> & ...

  2. @SpringBootApplication注解

    @SpringBootApplication注解表明了SpringBoot的核心功能,即自动配置. @SpringBootApplication(主配置类): @SpringBootConfigura ...

  3. 组合数们&&错排&&容斥原理

    最近做了不少的组合数的题这里简单总结一下下 1.n,m很大p很小 且p为素数p要1e7以下的 可以接受On的时间和空间然后预处理阶乘 Lucas定理来做以下是代码 /*Hdu3037 Saving B ...

  4. 最短路--Dijkstra&&Floyed&&SPFA

    最短路径是一个很常见的问题,这里有3种方法,可供参考. 一.Dijkstra#include<iostream> #include<cstdio> #include<cs ...

  5. Spring配置事务中的 transactionAttributes 各属性含义及XML配置

    转自:https://blog.csdn.net/z69183787/article/details/17161393 transactionAttributes 属性: PROPAGATION 事务 ...

  6. Linux下查看操作系统的位数和系统名称版本信息

    Linux下如何明确地查看操作系统的位数 如何知晓操作系统是32位还是64位?这里介绍一种简单的方式: [plain] [root@localhost mysql-5.1.57]# getconf L ...

  7. leetcode矩阵与动态规划相关

    目录 54/59螺旋矩阵 62不同路径 64最小路径和 120三角形最小路径和 695岛屿的最大面积 547朋友圈 718最长重复数组 221最大正方形 121/122/123/714/188买卖股票 ...

  8. python 46 css组合选择器 及优先级 、属性选择器

    一:css组合选择器 特性:每个选择器位可以为任意基本选择器或选择器组合 选择器分为以下几类: 群组选择器,子代(后代)选择器,相邻(兄弟)选择器,交集选择器,多类名选择器 1.群组选择器:    d ...

  9. 记录一下Junit测试MongoDB,获取MongoTemplate

    只是自己记录一下,测试MongoDB帮助类时,没有配置文件的测试 public class HelperTest { MongoTemplate template; @Before public vo ...

  10. SyntaxError: EOL while scanning string literal的解决

    2281 python中字符串的最后一个字符是斜杠会导致出错:SyntaxError: EOL while scanning string literal [背景] Python 2.7.2 中想要通 ...