PAT_A1013#Battle Over Cities
Source:
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 city1-city2 and city1-city3. Then if city1is 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
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的更多相关文章
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT1013: Battle Over Cities
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- PAT-Top1001. Battle Over Cities - Hard Version (35)
在敌人占领之前由城市和公路构成的图是连通图.在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通.修复的代价越大,意味着这个城市越重要. ...
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT Battle Over Cities [未作]
1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highw ...
- 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 ...
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- pat1013. Battle Over Cities (25)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
随机推荐
- C++ primer chapter 13
拷贝 赋值 销毁 拷贝构造函数 如果一个构造函数第一个参数是自身的引用,而且任何额外参数都有默认值,则此构造函数是拷贝构造函数拷贝构造函数的第一个类型必须是引用:如果参数不是引用类型,那么调用不会成功 ...
- POJ River Hopscotch 二分搜索
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully ...
- Nginx配置httpsserver
配置HTTPS主机.必须在server配置块中打开SSL协议,还须要指定服务器端证书和密钥文件的位置: server { listen 443; #要加密的域名 server_name www.te ...
- 《Java课程实习》日志(周二)
import java.awt.EventQueue; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.sw ...
- 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检測
在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...
- 插入1000万条数据到mysql数据库表
转自:https://www.cnblogs.com/fanwencong/p/5765136.html 我用到的数据库为,mysql数据库5.7版本的 1.首先自己准备好数据库表 其实我在插入100 ...
- 纯CSS 实现关闭图标 icon
本文介绍关闭 icon 的实现.具体如下 1.html部分 <span id="close"></span> 2.css部分 #close { displa ...
- 希尔shell排序——java实现
希尔排序是对插入排序的优化,将插入排序的交换步长由1增加到h. 希尔排序的思想是使数组中任意间隔为h的元素有序.步长调幅为h = 3*h + 1, 也就是1,4,13,40,121,364, 1003 ...
- caffe介绍
- Ubuntu1804安装Postgresql【转】
转载自:https://huur.cn/course/yw/1591.html 关系数据库管理系统是许多网站和应用程序的关键组成部分.它们提供了一种结构化的方式来存储,组织和访问信息. Postgre ...