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. cogs 290. [CTSC2000] 丘比特的烦恼

    290. [CTSC2000] 丘比特的烦恼 ★★★   输入文件:cupid.in   输出文件:cupid.out   简单对比时间限制:1 s   内存限制:128 MB 随着社会的不断发展,人 ...

  2. http协议的再次理解

    1.Tomcat是根据server.xml的配置启动的.根目录下conf/server.xml. 2.Tomcat是根据server.xml的配置启动的.根目录下conf/server.xml. 3. ...

  3. shell中eval命令

    原文:http://www.cnblogs.com/xdzone/archive/2011/03/15/1984971.html 语法:eval cmdLine eval会对后面的cmdLine进行两 ...

  4. [NOIP 2014] 生活大爆炸版石头剪刀布

    [题目链接] http://uoj.ac/problem/15 [算法] 按题意模拟即可[代码] #include<bits/stdc++.h> using namespace std; ...

  5. [POJ 2536] Gopher ||

    [题目链接] http://poj.org/problem?id=2536 [算法] 匈牙利算法解二分图最大匹配 [代码] #include <algorithm> #include &l ...

  6. codeforces round #424 div2

    A 暴力查询,分三段查就可以了 #include<bits/stdc++.h> using namespace std; ; int n, pos; int a[N]; int main( ...

  7. Anaconda/kickstart

    http://fedoraproject.org/wiki/Anaconda/Kickstart/zh-cn

  8. Struts2 中 result type=”json” 的参数解释

    转自:http://wangquanhpu.iteye.com/blog/1461750 1, ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction 继承 ...

  9. es优化收藏

    Elasticsearch常用优化 https://www.cnblogs.com/zlslch/p/6478773.html Elasticsearch 基础理论 & 配置调优 http:/ ...

  10. Multipart/form-data POST文件上传

    简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form method="post"action=&qu ...