PAT 1013 Battle Over Cities
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 Specification:
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<), 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 Specification:
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<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxnum 100005
int n,m,k;
int mp[][];
int temp[][];
int vis[] = {}; int findnotvis(){
for(int i=;i <= n;i++){
if(!vis[i]) return i;
}
return -;
} void dfs(int x){
vis[x] = ;
for(int i=;i <= n;i++){
if(!vis[i]&&mp[x][i]){
dfs(i);
}
}
} int main(){
cin >> n >> m >> k;
memset(mp,, sizeof(mp));
for(int i=;i < m;i++){
int x,y;scanf("%d%d",&x,&y);
mp[x][y] = mp[y][x] = ;
} while(k--){
int num;cin >> num;
vis[num] = ; //只要把点置为访问过的就可以了,不需要删去边
int cnt = ;
for(int i=;i <= n;i++){ //这个写法很好,学习了。
if(!vis[i]){
dfs(i);
cnt++;
}
}
cout << cnt- << endl;
memset(vis,, sizeof(vis));
} return ;
}
果然用cin输入数据会超时。。
其实这是考察图的问题,删除图的一个节点,是其他节点成为连通图,至少需要添加多少条线
添加最少的路线,就是连通分量数-1(例:n个互相独立的连通分量组成一个连通图,只需要连n-1条线就可以)
这道题最重要就是求除去图的一个节点后 剩余的连通分量的数目
利用邻接矩阵v存储路线,用visit数组表示城市是否被遍历过
对于每个被占领的城市,将其表示为遍历过的状态true即可
利用深度优先遍历dfs计算连通分量数目
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int N=;
int n,m,a[N*N],b[N*N],pre[N]; int f(int x){
if(pre[x]==x)return x;
pre[x]=f(pre[x]);
return pre[x];
} void link(int x,int y){
int r1=f(x),r2=f(y);
if(r1!=r2){
pre[r2]=r1;
}
}
void solve(int city){
for(int i=;i<=n;i++){
pre[i]=i;
}
for(int i=;i<m;i++){
if(a[i]==city||b[i]==city)continue;
link(a[i],b[i]);
}
int ans=;
for(int i=;i<=n;i++){ //查找堆的个数
if(i==city)continue;
int ri=f(i);
if(ri==i)ans++; //是个堆顶
} printf("%d\n",ans-); } int main(){ int k; scanf("%d%d%d",&n,&m,&k); for(int i=;i<m;i++){ scanf("%d%d",&a[i],&b[i]); } int x; for(int i=;i<=k;i++){ scanf("%d",&x); solve(x); } }
用并查集也挺好的思想
PAT 1013 Battle Over Cities的更多相关文章
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- 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 (dfs求连通分量)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT 1013 Battle Over Cities DFS深搜
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- 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 (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 ...
随机推荐
- Derek解读Bytom源码-启动与停止
作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...
- Latex 左右引号
参考: LaTeX技巧218:LaTeX如何正确输入引号:双引号""单引号'' Latex 左右引号 在latex中加引号时,使用""的输出为两个同向的引号: ...
- 3、python内置类型(0529)
python的内置对象类型以及支持的运算 python对象的相关术语 python程序中保存的所有数据都是围绕对象这个概念展开的 程序中存储的所有数据都是对象 每个对象都有一个身份.一个类型和一个值 ...
- ASP.NET MVC AntiForgeryToken
你开发一个网站,其中有个功能:新闻发布. 你是这样实现的: 1.保存新闻的方法是:/News/Save POST提交 2.接受两个参数:title和content 有一天,你登录网站(浏览器会保存相 ...
- JaveWeb 公司项目(5)----- Java获取当前时间的年月日以及同Thrift格式的转化
随着项目进度的逐步完成,数据传输和界面基本上已经搭建完成,下面就是一些细节部分的修改 今天博文的主要内容说的是获取当前的时间和同Thrift类型的转化 和C#类似,java也有一个时间类Date,加载 ...
- (一)PHP简介
什么是 PHP? PHP 是 "PHP Hypertext Preprocessor" 的首字母缩略词 PHP 是一种被广泛使用的开源脚本语言 PHP 脚本在服务器上执行 PHP ...
- 6-1 建立客户端与zk服务端的连接
6-1 建立客户端与zk服务端的连接 zookeeper原生java api使用 会话连接与恢复; 节点的增删改查; watch与acl的相关操作; 导入jar包;
- 【教程】手写简易web服务器
package com.littlepage.testjdbc; import java.io.BufferedReader; import java.io.FileReader; import ja ...
- VS2010:“error C2712: 无法在要求对象展开的函数中使用 __try”
ZC:这个错误是在使用 "__try{...} __except(EXCEPTION_EXECUTE_HANDLER){}"时 遇到的 http://blog.csdn.net/c ...
- laravel配置路由除了 / 都是404解决办法
1.php.ini开启phpopenssl 2.conf (nginx为例) location / { index index.html index.htm index.php l.php; #tr ...