这题用并查集或者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. Joker Xue

    大家好,我是LJ,来自于美丽的魏源故乡——隆回,从小被爸妈带到大,但是现在,我脱离了爸妈的管理,来到了远离家乡的长沙,大学生活当然美好,但是我们在做出每一个决定的同时,可能很少有他们的建议了,不过没有 ...

  2. Mysql跨数据库(在同一IP地址中)复制表

    数据库表间数据复制分类 在利用数据库开发时,常常会将一些表之间的数据互相导入.当然可以编写程序实现,但是,程序常常需要开发环境,不方便.最方便是利用sql语言直接导入.既方便而修改也简单.以下就是导入 ...

  3. triplet loss

    因为待遇低,因为工作不开心,已经严重影响了自己的工作积极性和工作效率,这几天发觉这样对自己实在是一种损失,决定提高工作效率,减少工作时间. 说说最近做的tracking, multi-object t ...

  4. box-shadow的应用技巧

    一.box-shadow的参数解析 box-shadow:none; box-shadow: h-shadow v-shadow blur spread color inset; box-shadow ...

  5. linux保留旧版本python,安装python3

    1.备份老版本 mv /usr/bin/python /usr/bin/python.bak 2. 下载python3 wget https://www.python.org/ftp/python/3 ...

  6. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--L-用来作弊的药水

    链接:https://www.nowcoder.com/acm/contest/90/L 来源:牛客网 1.题目描述 -- 在一个风雨交加的夜晚,来自异世界的不愿透露姓名的TMK同学获得了两种超强药水 ...

  7. kubeadm常见报错和解决方法

    k8s随着社区不断壮大国内使用率现在也是比较高的,常用的部署方式主要还是以二进制和kubeadm为主,当然1.13之前大部分人还是以二进制,但是随着版本更新kubeadm已经逐步适用于生成环境,由于k ...

  8. 浅谈css蒙版效果

    我们进网站浏览时经常看到当鼠标悬浮在图片上或者某一个地方时,会出现一层朦胧现象覆盖着悬浮位置,简单的理解为“蒙版效果”.下面简单列举实现过程: HTML: CSS:

  9. 利用nodejs实现商品管理系统(一)

    一.界面分类:用户登录界面,商品管理界面(包含商品编辑,创建,删除,列表界面) 功能实现:1.用户输入用户名与密码,通过加密,与数据库校验,如果正确,则跳转到商品管理界面,否则一直停留在用户界面. 2 ...

  10. (数据科学学习手册28)SQL server 2012中的查询语句汇总

    一.简介 数据库管理系统(DBMS)最重要的功能就是提供数据查询,即用户根据实际需求对数据进行筛选,并以特定形式进行显示.在Microsoft SQL Serve 2012 中,可以使用通用的SELE ...