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#面向对象三大特性之二:继承

    面向对象的三大特性之一的封装,解决了将对同一对象所能操作的所有信息放在一起,实现统一对外调用,实现了同一对象的复用,降低了耦合. 但在实际应用中,有好多对象具有相同或者相似的属性,比如有一个对象 果树 ...

  2. linux 内核与用户空间通信机制netlink初探

      1.Linux进程概述 Linux中的进程间通信机制源自于Unix平台上的进程通信机制.Unix的两大分支AT&T Unix和BSD Unix在进程通信实现机制上各有所不同,前者形成了运行 ...

  3. foreach 加 &

  4. AIM Tech Round 5 (rated, Div. 1 + Div. 2) D(SET,思维)

    #include<bits/stdc++.h>using namespace std;const long long mod = 1e9+7;char s[370007][27];long ...

  5. CAS客户端整合(四)-- Cacti

    Cacti 是一套纯 lnmp 搭建的服务器监控系统,用 SNMP 抓取数据,RRDTool 绘制表格 登录流程 Cacti 的登录同样是先判断session,再尝试从 cookie 读取 sessi ...

  6. [转]SE43 修改SAP标准菜单、登陆界面、背景图片

    1.事务码se43 复制标准菜单S000 到 ZS000  2.按实际需要修改 ZS000  3.在事务码SSM2中用ZS000 代替 S000  4.注销后重新登陆 o 修改SAP登陆界面(在本博客 ...

  7. LeetCode 671. Second Minimum Node In a Binary Tree二叉树中第二小的节点 (C++)

    题目: Given a non-empty special binary tree consisting of nodes with the non-negative value, where eac ...

  8. CentOS 6.6 x64安装TensorFlow

    CentOS 6.6 x64安装TensorFlow升级Python到2.7(系统自带Python版本为2.6) // 安装编译工具 $ yum -y install gcc automake aut ...

  9. 使用Collectd + InfluxDB + Grafana进行JMX监控

    我们已经看到使用Collectd监控CPU /内存利用率(本文).但它没有提供所有信息来确定性能问题的瓶颈.在本文中,我们将使用Collectd Java插件来使用其JMX技术来监视和管理Java虚拟 ...

  10. 查询rabbitmq

    package com.yunda.app.service; import java.io.InputStream; import java.net.HttpURLConnection; import ...