1013 Battle Over Cities (25)(25 point(s))
problem
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 (<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 Input
3 2 3
1 2
1 3
1 2 3
Sample Output
1
0
0
tips
answer
- 并查集
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define Max 1010
#define fi first
#define se second
int N, M, K;
int ci[Max], root[Max], rootTemp[Max];
void init(){
for(int i = 0; i < Max; i++) root[i] = i;
}
int find(int i){
if(root[i] != i) root[i] = find(root[i]);
return root[i];
}
void unionSet(int i, int j){
root[find(i)] = find(j);
}
int getNum(){
int num = 0;
for(int i = 1; i <= N; i++){
if(root[i] == i) num++;
}
return num-2;
}
typedef struct {
int f, t;
} Edge;
Edge e[Max*Max];
int main(){
// freopen("test.txt", "r", stdin);
scanf("%d%d%d", &N, &M, &K);
init();
for(int i = 0; i < M; i++){
scanf("%d%d", &e[i].f, &e[i].t);
}
for(int i = 0; i < K; i++){
init();
int t;
scanf("%d", &t);
for(int j = 0; j < M; j++){
if(e[j].f == t || e[j].t == t) continue;
unionSet(e[j].f, e[j].t);
}
printf("%d\n", getNum());
}
return 0;
}
experience
- cin cout超时……
1013 Battle Over Cities (25)(25 point(s))的更多相关文章
- pat 1013 Battle Over Cities(25 分) (并查集)
1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...
- 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 ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- 1003 Emergency (25)(25 point(s))
problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- MySQL5.7.25(解压版)Windows下详细的安装过程
大家好,我是浅墨竹染,以下是MySQL5.7.25(解压版)Windows下详细的安装过程 1.首先下载MySQL 推荐去官网上下载MySQL,如果不想找,那么下面就是: Windows32位地址:点 ...
- PAT 甲级 1006 Sign In and Sign Out (25)(25 分)
1006 Sign In and Sign Out (25)(25 分) At the beginning of every day, the first person who signs in th ...
随机推荐
- HDU 1017 A Mathematical Curiosity 数学题
解题报告:输入两个数,n和m,求两个数a和b满足0<a<b<n,并且(a^2+b^2+m) % (a*b) =0,这样的a和b一共有多少对.注意这里的b<n,并不可以等于n. ...
- windebug分析高cpu问题
分析高CPU的关键是找到哪个线程是持续运行,占用CPU时间. 可以隔上两分钟连续抓两个dump文件,使用 !runaway 查看线程运行的时间 通过对比两个dump文件的线程时间,看看哪个线程运行的时 ...
- Eclipse中各种编码格式及设置
操作系统:Windows 10(家庭中文版) Eclipse版本:Version: Oxygen.1a Release (4.7.1a) 刚看到一篇文章,里面介绍说Ascii.Unicode是编码,而 ...
- netstat-ll-grep-nohup-df-supervisord
============http://man.linuxde.net/=========== 0 vi / n是查找下一个,alt+n是上一个 u撤销上一步,回到上一步 1. 根据进程号(4974) ...
- dedecms自定义模型之独立模型在首页、列表页、内容调用内容
dedecms关于自定义模型(独立模型)的首页.列表页.内容怎么调用?在后台自定义模型(独立模型)的建立及自定义字段的添加比较简单,需要注意两点: (1)如果某个字段需要在前台列表页显示,则在前台参数 ...
- Unix IPC之Posix消息队列(1)
部分参考:http://www.cnblogs.com/Anker/archive/2013/01/04/2843832.html IPC对象的持续性:http://book.51cto.com/ar ...
- sqlserver sp_spaceused用法
sp_spaceused显示行数.保留的磁盘空间以及当前数据库中的表所使用的磁盘空间,或显示由整个数据库保留和使用的磁盘空间. 语法sp_spaceused [[@objname =] 'objnam ...
- sql 修改列名及表名 sp_rename
因需求变更要改表的列名,平常都是跑到Enterprise manager中选取服务器->数据库->表,然后修改表,这样太麻烦了,查了一下,可以用script搞定, 代码如下: EXEC s ...
- HBase 入门笔记-数据落地篇
一.前言 关于数据落地方面,HBase官网也有相关介绍.本文主要介绍一下实际工作中涉及的数据存储方面的一些经验和技巧,主要涉及表rowkey设计.数据落地方案 二.表设计 相对于MySQL等关系型数据 ...
- Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件
前言: 忙完了结婚乐APP的开发,终于可以花一定的时间放在博客上了.好了,废话不多说,今天我们要带来的效果是苹果版本的QQ下拉刷新.首先看一下目标效果以及demo效果: 因为此效果实现的步骤 ...