1013 Battle Over Cities (25 分)(图的遍历or并查集)

这题用并查集或者dfs都可以做
dfs
#include<bits/stdc++.h> using namespace std;
const int N=1e3+;
bool mp[N][N];
int n,m,k;
bool vis[N];
void dfs(int v)
{
vis[v]=true;
for(int i=;i<=n;i++){
if(mp[v][i]&&!vis[i]){
dfs(i);
}
}
}
int main()
{
fill(mp[],mp[]+N*N,false);
scanf("%d %d %d",&n,&m,&k);
for(int i=;i<m;i++){
int a,b;
scanf("%d %d",&a,&b);
mp[a][b]=mp[b][a]=true;
}
while(k--){
fill(vis,vis+N,false);
int x;
scanf("%d",&x);
vis[x]=true;
int sum=;
for(int i=;i<=n;i++){ if(!vis[i]){
sum++;
dfs(i);
}
}
printf("%d\n",sum-);
}
return ;
}
并查集
#include<bits/stdc++.h> using namespace std; vector<pair<int,int> > edge;
int f[];
int n,m;
int findth(int x)
{
if(x==f[x]) return x;
return f[x]=findth(f[x]);
}
void join(int x,int y)
{
int fx,fy;
fx = findth(x);
fy = findth(y);
if (fx != fy)
f[fx] = fy;
}
void solve(int p)
{
for (int i = ; i <= n ; i++) f[i] = i;
for (int i = ; i < edge.size() ; i++){
if (edge[i].first == p || edge[i].second == p) continue;
join(edge[i].first,edge[i].second);
}
int cnt = ;
for (int i = ; i <= n ; i++){
if (i == p)
continue;
if(f[i]==i) cnt++;
}
printf("%d\n",cnt-);
}
int main()
{
int k;
scanf("%d %d %d",&n,&m,&k);
edge.resize(m);
for (int i = ; i < m ; i++)
{
int x,y;
scanf("%d %d",&x,&y);
edge[i] = make_pair(x,y);
}
for (int i = ; i <= k ; i++)
{
int q;
scanf("%d",&q);
solve(q);
}
return ;
}
1013 Battle Over Cities (25 分)(图的遍历or并查集)的更多相关文章
- 1013 Battle Over Cities (25分) 图的连通分量+DFS
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 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 ...
- 1013 Battle Over Cities (25 分)
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 (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- 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 Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 1013. Battle Over Cities (25)
题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...
- 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 ...
- PTA 朋友圈 (25 分) 代码详解 (并查集)
1.题目要求: 某学校有N个学生,形成M个俱乐部.每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈.一个学生可以同时属于若干个不同的俱乐部.根据"我的朋友的朋友也是我的朋友" ...
随机推荐
- putty 启动 linux 下的oracle
没搞过linux ,仅作记录: 1 打开putty.exe 程序 ,选择 连接 2 输入linux 的用户名和密码后,按下图操作: 3 启动监听 4 命令总结: 1. sudo su - orac ...
- data-ng-show指令
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 表达式过滤器currency
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- ListItem Updating事件监视有没有上传附件
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using ...
- java使用JSCH连接FTP(Linux服务器)上传文件到Linux服务器
首先需要用到jsch-0.1.54.jar 包: 链接: https://pan.baidu.com/s/1kZR6MqwpCYht9Pp_D6NKQw 密码: gywx 直接上代码: package ...
- watch、computed、methods的区别
1. `computed`属性的结果会被缓存,除非依赖的响应式属性变化才会重新计算.主要当作属性来使用,要return出去一个值:2. `methods`方法表示一个具体的操作,主要书写业务逻辑:3. ...
- HTML5--应用网页模板
因为刚开始写博客,只想着把知识点记录在这,也想给你们一些参考,在布局上有些没有思考太多;回过头来看,实在是不忍直视,对不住之前阅读的100+,既然昨天的事无法挽回,那就从现在开始从新整改吧!也希望大家 ...
- 全文搜索引擎 Elasticsearch 安装踩坑记录
一.安装 Elastic 需要 Java 8 环境.如果你的机器还没安装 Java 安装完 Java,就可以跟着官方文档安装 Elastic.直接下载压缩包比较简单. $ wget https://a ...
- Windows无法安装到这个磁盘 选中的磁盘具有MBR分区表解决方法
在安装 win10的时候,会出现这种提示:Windows 无法安装到这个磁盘.选中的磁 盘具有 MBR 分区表.在 EFI 系统上, Windows 只能安装到 GPT 磁盘.出现这种 情况主要是因为 ...
- poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点
/** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...