1013 Battle Over Cities (25)(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 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

Each input file contains one test case. Each case starts with a line containing 3 numbers N (&lt1000), 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

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
需要修的路个数就是连通分量的个数减一。
 #include <iostream>
#include <vector>
using namespace std; int N, M, K;
int G[][]; int connectCnt(int city);
void dfs(int s, bool marked[]); int main()
{
int i, j, v, w, c;
cin >> N >> M >> K;
for (i = ; i < M; i++)
{
cin >> v >> w;
G[v][w] = ;
G[w][v] = ;
}
for (i = ; i < K; i++)
{
cin >> c;
vector<int> vec;
for (j = ; j <= N; j++)
{
if (G[c][j] == )
{
G[c][j] = ;
G[j][c] = ;
vec.push_back(j);
}
}
cout << connectCnt(c) << endl;
for (int j : vec)
{
G[c][j] = ;
G[j][c] = ;
}
}
return ;
} int connectCnt(int city)
{
int v, cnt = ;
bool marked[] = {};
for (v = ; v <= N; v++)
{
if (!marked[v] && v != city)
{
dfs(v, marked);
cnt++;
}
}
return cnt - ;
} void dfs(int s, bool marked[])
{
marked[s] = true;
for (int v = ; v <= N; v++)
{
if (!marked[v] && G[s][v])
dfs(v, marked);
}
}

但是查看别人的博客发现不需要每次把和被占领的城市相连的道路标记为0再标记为1,只需要把marked数组被占领的城市标记为1即可。。

 #include <iostream>
using namespace std; int N, M, K;
int G[][]; int connectCnt(int city);
void dfs(int s, bool marked[]); int main()
{
int i, v, w, c;
scanf("%d%d%d", &N, &M, &K);
for (i = ; i < M; i++)
{
scanf("%d%d", &v, &w);
G[v][w] = ;
G[w][v] = ;
}
for (i = ; i < K; i++)
{
scanf("%d", &c);
printf("%d\n", connectCnt(c));
}
return ;
} int connectCnt(int city)
{
int v, cnt = ;
bool marked[] = {};
marked[city] = true;
for (v = ; v <= N; v++)
{
if (!marked[v])
{
dfs(v, marked);
cnt++;
}
}
return cnt - ;
} void dfs(int s, bool marked[])
{
marked[s] = true;
for (int v = ; v <= N; v++)
{
if (!marked[v] && G[s][v])
dfs(v, marked);
}
}
 

pat甲级1013的更多相关文章

  1. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  2. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

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

  4. PAT甲级1013题解——并查集+路径压缩

    题目分析: 本题初步浏览题目就知道是并查集的模板题,数据输入范围N为1~1000,则M的范围为0~1000^2,通过结构体记录每一对连线的关系,p[]数组记录每个节点的跟,对于k次查询,每次都要重新维 ...

  5. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  6. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  7. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  8. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  9. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

随机推荐

  1. Android xUtils框架(一) DbUtils

    在DbUtils中,只支持4中数据类型: public enum ColumnDbType { INTEGER("INTEGER"), REAL("REAL") ...

  2. Docker基本用法

    基本操作命令 列举镜像 # docker images 列举容器 # docker ps 运行容器 # docker run -t --name ubuntu -i ubuntu:14.04 /bin ...

  3. [WIP]php入門

    创建: 2019/06/19 安装  MAMP   变量与运算符  php标签  <?php ... ?> <?php ... ?> ● 在文件最后的 ?> 通常省略, ...

  4. ZOJ - 3777(状压dp)

    The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...

  5. 为什么要把系统拆分成分布式的,为啥要用Dubbo?

    阅读本文大概需要 6 分钟. 作者:yanglbme 1.面试题 为什么要进行系统拆分?如何进行系统拆分?拆分后不用 dubbo 可以吗? 2.面试官心里分析 从这个问题开始就进行分布式系统环节了,好 ...

  6. android 拖拽图片&拖动浮动按钮到处跑

    来自老外: 拖拽图片效果 方法一: 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  7. Educational Codeforces Round 48 (Rated for Div. 2) B 1016B Segment Occurrences (前缀和)

    B. Segment Occurrences time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  8. ldap第一天 编译安装LDAP + ldapadmin

    此文整理学习此大神的博客:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26168435&id=5746284 一.环 ...

  9. EcmaScript学习

    1.eval: ts: declare function eval(x: string): any; js: /** @param {*} x @return {Object} */ eval = f ...

  10. 什么是obj文件?

    百度百科: 程序编译时生成的中间代码文件.目标文件,一般是程序编译后的二进制文件,再通过链接器(LINK.EXE)和资源文件链接就成可执行文件了.OBJ只给出了程序的相对地址,而可执行文件是绝对地址. ...