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个俱乐部.每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈.一个学生可以同时属于若干个不同的俱乐部.根据"我的朋友的朋友也是我的朋友" ...
随机推荐
- 【luogu P3372 线段树1】 模板
线段树的模板题 题目链接:https://www.luogu.org/problemnew/show/P3372 update区间修改,query区间求和 #include <iostream& ...
- HTML5之表单新增类型介绍
1.html5的input标签的type类型新增介绍: 2.表单新增属性介绍: 3.代码示例: <!doctype html> <html> <head></ ...
- Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition Kaiming He, Xiangyu Zh ...
- ZooKeeper下载安装(Windows版本)
进入Apache ZooKeeper官方网站进行下载,https://zookeeper.apache.org/releases.html 这里我们选择zookeeper-3.4.12版本进行下载 百 ...
- [HP-UNIX]bdf命令总结
(1)bdf命令的效果 lijiaman$[/home/oracle]bdf Filesystem kbytes used avail %used Mounted on /dev/vg00/lvol3 ...
- oracle快速添加用户及授权
--Oracle使用的是用户管理模式--意味着,Oracle的数据使用用户来分割 --以后开发,我们需要每个项目都需要使用一个用户 --所以:一个数据文件是可以放多个用户的数据的.但是我们开发从数据的 ...
- [HAOI2007]上升序列(最长上升子序列)
题目描述 对于一个给定的 S=\{a_1,a_2,a_3,…,a_n\}S={a1,a2,a3,…,an} ,若有 P=\{a_{x_1},a_{x_2},a_{x_3},…,a_{x_m}\ ...
- Java集合类——Set、List、Map、Queue接口
目录 Java 集合类的基本概念 Java 集合类的层次关系 Java 集合类的应用场景 一. Java集合类的基本概念 在编程中,常需要集中存放多个数据,数组是一个很好的选择,但数组的长度需提前指定 ...
- 爬虫——BeautifulSoup4解析器
BeautifulSoup用来解析HTML比较简单,API非常人性化,支持CSS选择器.Python标准库中的HTML解析器,也支持lxml的XML解析器. 其相较与正则而言,使用更加简单. 示例: ...
- PHP实现SMTP邮件的发送实例
当你还在纠结php内置的mail()函数不能发送邮件时,那么你现在很幸运,此时的这篇文章可以帮助到你! php利用smtp类来发邮件真是屡试不爽,我用过很久了,基本上没出过问题.本博客后台,当博主回复 ...