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. C# EventHandler委托事件小结--百度

    最近遇到一个委托的问题,+=这个符号 this.Activated += new EventHandler(Form1_Activated);//Form1_Activated为方法名12 这个语句拆 ...

  2. 2019前端面试之html5

    html5新增特性 语义化标签 表单新特性 视频和音频canva画布 svg画图 地理位置 为鼠标提供的拖放api webwroker storage websocker 常见块级标签 div,p,h ...

  3. lyd的旅行

    lyd的旅行 众所周知,lyd是一个人赢.他有很多很多的妹子.某天,他带着他的众多妹子进行了一次旅(dou)行(feng),由于lyd的车上妹子太多超重了,所以车速每秒最多只能改变d个单位,lyd在出 ...

  4. 洛谷P2380 狗哥采矿

    P2380 狗哥采矿 题目背景 又是一节平静的语文课 狗哥闲来无事,出来了这么一道题 题目描述 一个n*m的矩阵中,每个格子内有两种矿yeyenum和bloggium,并且知道它们在每个格子内的数量是 ...

  5. 帝都Day6——图论

    //P2O5呢? 一.图的存储: 邻接矩阵:邻接表. 邻接矩阵:n*n的[][],[i][j]节点有边记1没边0 缺点 空间复杂度O(n^2) 占用内存较大(我为什么要把这些东西写到这里呢???) 邻 ...

  6. AT2044 Teleporter

    传送门 这个是真的简单题,随便手玩一下就可以发现最优策略一定是给\(1\)加上自环 然后就可以dfs一下看哪些点子树里深度最深的点到当前点的距离会等于\(k-1\),然后将当前点连向\(1\)(当然特 ...

  7. DHCP原理和配置

    在大型网络中,会有大量的主机和设备需要获取ip地址和网络参数,为了解决手动配置的工作量大.ip冲突问题,因此需要使用DHCP(dynamic host configuration protocol). ...

  8. Sharepoint 根据文件相对路径获取、操作SPFolder

    public AjaxResult LoadDocInfo(HttpContext httpContext) { var result = new ArrayList(); try { var org ...

  9. Microsoft JDBC Driver 使用 getParameterMetaData 会报错?

    不知道为何使用 Microsoft JDBC Driver for SQL Server 驱动时,sql语句不带参数没有问题,但是如果带参数且使用 getParameterMetaData 就会提示某 ...

  10. 安全性测试入门 (五):Insecure CAPTCHA 验证码绕过

    本篇继续对于安全性测试话题,结合DVWA进行研习. Insecure Captcha不安全验证码 1. 验证码到底是怎么一回事 这个Captcha狭义而言就是谷歌提供的一种用户验证服务,全称为:Com ...