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

题目分析:这是一道利用图的遍历的题 将要去掉的城市节点的visit置为false 然后求出最大连通分量 这个最大连通分量减一就是我们要求的值
 #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int G[][];
bool visit[] = { };
int N, M, K;
void dfs(int v)
{
visit[v] = true;
for (int i = ; i <=N; i++)
{
if (!visit[i] && G[v][i])
dfs(i);
}
}
int main()
{ cin >> N >> M >> K;
int v1, v2;
int v;
int count = ;
for (int i = ; i < M; i++)
{
cin >> v1 >> v2;
G[v1][v2] = G[v2][v1] = ;
}
for (int i = ; i < K; i++)
{
fill(visit, visit + , false);
count = ;
cin >> v;
visit[v] = true;
for (int i = ; i <=N; i++)
{
if (!visit[i])
{
dfs(i);
count++;
}
}
cout << count-<<endl;
}
}

1013 Battle Over Cities (25 分)的更多相关文章

  1. PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

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

  2. 1013 Battle Over Cities (25分) DFS | 并查集

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

  3. 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 occup ...

  4. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

  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 A 1013. Battle Over Cities (25)【并查集】

    https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...

  7. 1013. Battle Over Cities (25)

    题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...

  8. 1013 Battle Over Cities (25)(25 point(s))

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

  9. 1013. Battle Over Cities (25)(DFS遍历)

    For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city ...

随机推荐

  1. 使用Navicat Premiun远程连接MySQL失败,报错(10038)

    远程连接MySQL失败,可能有一下原因: 1.小伙子/小姑凉注意一下你的ip是否输入正确了!! 2.网络或防火墙问题 1).排查网络问题 使用命令:ping 192.168.1.1 查看网络请求是否超 ...

  2. Java集合01——List 的几个实现类,了解一下?

    从本文起,我们将开始分享 Java 集合方面的知识,关注公众号「Java面典」了解更多 Java 知识点. List 是继承于 Collection 的接口,其实现类有 ArrayList,Linke ...

  3. <字符串匹配>KMP算法为何比暴力求解的时间复杂度更低?

    str表示文本串,m表示模式串; str[i+j] 和 m[j] 是正在进行匹配的字符; KMP的时间复杂度是O(m+n)  ,  暴力求解的时间复杂度是O(m*n) KMP利用了B[0:j]和A[i ...

  4. Intel优化Gen7驱动代码 Geekbench 5性能猛增

    Linux下缺少大量开发.支持是会导致各种奇葩的错误的,Intel日前就提交了一个代码修复,22nm Ivybridge及Haswell处理器的GeekBench 5的性能就提升了330%. 3.3倍 ...

  5. SpringBoot入门系列(七)Spring Boot整合Redis缓存

    前面介绍了Spring Boot 中的整合Mybatis并实现增删改查,.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/ ...

  6. 0318 guava并发工具

    并发是一个难题,但是可以通过使用强力简单的抽象来显著的简化,为了简化问题,guava扩展了Future接口,即 ListenableFuture (可以监听的Future).我强烈建议你在你的所有代码 ...

  7. 2016 Multi-University Training Contest 4 T9

    http://acm.hdu.edu.cn/showproblem.php?pid=5772 最大权闭合子图. 得到价值w[i][j]的条件是选了i,j这两个位置的字符.选择位置的i字符花费为 第一次 ...

  8. 深入理解计算机系统 (CS:APP) 缓冲区漏洞实验 – Buffer Lab 解析

    原文地址:https://billc.io/2019/05/csapp-cachelab/ 写在前面 这是 CSAPP 官网上的第 4 个实验 buflab,也是学校要求的第三个实验.这个实验比上一个 ...

  9. 正则匹配电话号码demo

    public static String doFilterTelnum(String sParam) { String result = sParam; if (sParam.length() < ...

  10. C#winform如何主窗口最大化最小化默认大小

    this.WindowState = FormWindowState.Minimized; bool b = true; private void button2_Click(object sende ...