1013 Battle Over Cities (25 分)
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 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 分)的更多相关文章
- 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 ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- 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 ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT A 1013. Battle Over Cities (25)【并查集】
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- iOS8 定位失败问题
iOS7升级到iOS8后,百度地图 iOS SDK 中的定位功能不可用,给广大开发者带来了不便,在此向大家分享一个方法来解决次问题.(官方的适配工作还在进行中,不久将会和广大开发者见面) 1.在inf ...
- PyCharm设置远程虚拟环境
1. 创建项目 2. 配置解释器 1. 先打开srttings 2. 选择配置解释器选项 3. 配置远端虚拟环境 4. 成功提示 5. 查看pip列表信息 经过以上操作,PyCharm设置远程虚拟环境 ...
- POJ 1879
栈和队列的综合应用,利用栈和队列分别模拟分,5分,时槽,以及小球队列 利用求出一天后的置换可以求出周期,进而求出最大公约数(可以利用矩阵的角度,也许可以简化,因为每次都是乘上一个相同的置换矩阵) 要注 ...
- Scikit-learn之特征抽取
一.安装包 pip install Scikit-learn 二.字典特征抽取 1.字典特征抽取 #!/usr/bin/env python # -*- coding: utf-8 -*- #auth ...
- 页面高度自适应方法(PC、移动端都适用)
有个项目移动端的首页需要自适应. 宽度已经自适应了 , 高度也要自适应 ,先总结一下方法,PC端也适用. $(function(){ var h = window.innerHeight; $(&qu ...
- 交换机三种模式Access、Hybrid和Trunk
[端口介绍] 种链路类型:access.trunk.hybird 个VLAN,一般用于连接计算机端口: Trunk类型端口:可以允许多个VLAN通过,可以接收和发送多个VLAN 报文, 一般用于交换机 ...
- 网络安全从入门到精通 (第二章-4) 后端基础PHP—简介及基本函数-上
本文内容 什么是PHP PHP的基础语法 运算符 条件分支语句 1,什么是PHP? PHP(超文本预处理器)是一种通用开源语言,(是动态语言中的一种,动态语言还有ASP,ASPX,JSP). PHP语 ...
- shell编程学习之使用jq对json数据进行提取
shell编程学习之使用jq对json提取 jq命令允许直接在命令行下对JSON进行操作,包括分片.过滤.转换等 ,jq是用C编写,没有运行时依赖,所以几乎可以运行在任何系统上.预编译的二进制文件可以 ...
- Postman从入门到入门
1.Postman简介 Postman是一个接口测试工具,在做接口测试的时候, Postman相当于一个客户端,可以发送请求到服务器端.Postman分为Chrome插件版本(不再更新维护)和Nati ...
- nop 配置阿里cdn 联通4g 页面显示不全 查看源代码发现被截断
开发中遇见特别诡异的问题, 项目使用nop框架,pavilion主题,之后配置阿里cdn,然后在联通4g的情况下苹果手机网页显示不完全,nop首页和产品详情页都是如此,排查过程: 1.阿里cdn设置了 ...