这题用并查集或者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并查集)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 1013 Battle Over Cities (25分) DFS | 并查集

    1013 Battle Over Cities (25分)   It is vitally important to have all the cities connected by highways ...

  4. 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 ...

  5. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

  6. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. PTA 朋友圈 (25 分) 代码详解 (并查集)

    1.题目要求: 某学校有N个学生,形成M个俱乐部.每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈.一个学生可以同时属于若干个不同的俱乐部.根据"我的朋友的朋友也是我的朋友" ...

随机推荐

  1. An error occurred during the installation of assembly 'Microsoft.VC90.ATL or 'Microsoft.VC80.ATL'

    An error occurred during the installation of assembly 'Microsoft.VC90.ATL or 'Microsoft.VC80.ATL' 下载 ...

  2. TDD: 解除依赖

    1  A类依赖B 类,可以把B类提取成IB接口,解除AB 之间的依赖关系. 通过创建实现了IB接口的BStub 装代码,可以模拟B类进行测试. 这是针对接口编程的典型.适合构造代价大,变化多的情况.应 ...

  3. Java虚拟机垃圾回收(二) :垃圾回收算法(转载)

    1.标记-清除算法 标记-清除(Mark-Sweep)算法是一种基础的收集算法. 1.算法思路 "标记-清除"算法,分为两个阶段: (A).标记 首先标记出所有需要回收的对象: 标 ...

  4. 从$emit 到 父子组件通信 再到 eventBus

    故事还是得从$emit说起,某一天翻文档的时候看到$emit的说明 触发当前实例上的事件?就是自身组件上的事件呗,在父子组件通信中,父组件通过props传递给子组件数据(高阶组件可以用provide和 ...

  5. Configuration 中无法自动注入依赖于component的bean

    出现问题时我这样使用依赖注入 @Configuration public class WebServiceConfig { @Autowired private IMessageWebService ...

  6. http状态码(status_codes)

    首先:1XX 接受的请求正在处理,2XX请求正常处理完毕,3XX需要进行附加操作以完成请求(重定向?),4XX服务器无法处理请求(也就是客户端请求错误),5XX服务器处理请求出错. 当然不仅仅是一张图 ...

  7. PHP创建MySQL并引入后执行sql语句

    一:创建sql.php文件 <?php function sqlMethod($sql){ $servername = "localhost"; $username = &q ...

  8. react native 踩坑之 SectionList state更新 不执行render重新渲染页面

    官方文档中指出 SectionList 本组件继承自PureComponent而非通常的Component,这意味着如果其props在浅比较中是相等的,则不会重新渲染.所以请先检查你的renderIt ...

  9. PHP Laravel 5.4 环境搭建

    1.php运行环境搭建 在win10系统上进行搭建的,使用的是wamp环境  wampserver3_x86_apache2.4.17_mysql5.7.9_php5.6.15.exe,安装包中集成了 ...

  10. java8lambda表达式初识

    一.函数式接口 只有一个 抽象方法 的 接口 叫函数式接口 /** * @auther hhh * @date 2018/12/24 22:20 * @description 函数式接口:只有 一个 ...