1013. Battle Over Cities (25)
来源:互联网 发布:cydia找不到软件源 编辑:程序博客网 时间:2024/05/16 04:53
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 city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.
Input
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), 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 Input3 2 31 21 31 2 3Sample Output
100
#include <iostream>#include <cstdio>#include <vector>using namespace std;void dfs(int c, vector<bool>& used, vector<vector<bool>>& graph){used[c] = true;for(size_t i = 1; i < graph.size(); ++i){if(graph[c][i] && !used[i]){dfs(i, used, graph);}}}int main(){int n, m, k;scanf("%d%d%d", &n, &m, &k);vector<vector<bool>> graph(n + 1, vector<bool>(n + 1, false));for(int i = 0; i < m; ++i){int c1, c2;scanf("%d%d", &c1, &c2);graph[c1][c2] = graph[c2][c1] = true;}for(int i = 0; i < k; ++i){int c;scanf("%d", &c);vector<bool> used(n + 1, false);used[c] = true;int count = 0;for(int j = 1; j <= n; ++j){if(!used[j]){dfs(j, used, graph);++count;}}printf("%d\n", count - 1);}return 0;}
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 1013. Battle Over Cities (25)
- 简单的java Socket 例子
- Java 入门 之 聊天室项目思路 服务器端
- iOS开发多线程---GCD的介绍和简单使用
- iOS 【iOS-PresentViewController切换界面 Modal】
- 高斯金字塔 matlab程序实现
- 1013. Battle Over Cities (25)
- 使用Cookie进行会话管理
- C++ in the modern world(为什么C++不会衰老)
- CodeForces 589G 线段树
- Redis教程(十四):内存优化介绍
- swift-学习笔记一《基础》
- Android Binder设计与实现 – 设计篇
- 使用codepush进行ReactNative热部署
- IOS流媒体播放