并查集判断连通性。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
using namespace std; const int maxn=;
struct Edge
{
int u,v;
}e[maxn*maxn];
int n,m,k;
int f[maxn]; int Find(int x)
{
if(x!=f[x]) return f[x]=Find(f[x]);
return f[x];
} int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=m;i++)
scanf("%d%d",&e[i].u,&e[i].v); for(int i=;i<=k;i++)
{
int id; scanf("%d",&id);
int sz=n-;
for(int j=;j<=n;j++) f[j]=j;
for(int j=;j<=m;j++)
{
if(e[j].u==id) continue;
if(e[j].v==id) continue;
int fx=Find(e[j].u);
int fy=Find(e[j].v);
if(fx!=fy)
{
f[fx]=fy;
sz--;
}
}
printf("%d\n",sz-);
}
return ;
}

PAT (Advanced Level) 1013. Battle Over Cities (25)的更多相关文章

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

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

  2. PTA (Advanced Level) 1013 Battle Over Cities

    Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If ...

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

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

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

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

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

  6. 【PAT Advanced Level】1013. Battle Over Cities (25)

    这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图). 在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问.一共进行了多少次这 ...

  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. PAT A 1013. Battle Over Cities (25)【并查集】

    https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...

  9. PAT甲题题解-1013. Battle Over Cities (25)-求联通分支个数

    题目就是求联通分支个数删除一个点,剩下联通分支个数为cnt,那么需要建立cnt-1边才能把这cnt个联通分支个数求出来怎么求联通分支个数呢可以用并查集,但并查集的话复杂度是O(m*logn*k)我这里 ...

随机推荐

  1. 学习PHP函数:preg_match_all

    <?php $str = '10.10.10.10, 10.10.10.11'; preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', ...

  2. 在Service服务中请求网络

    一.startservice方式启动 第一次startservice启动服务的时候,会走oncreate和onstart方法, 第二次startservice启动服务的时候,会走onstart方法, ...

  3. psy

    本文的重点是讲解如何运用心理线指标看盘,运用周线月线的心理线来抓住大盘的顶部和底部的研究.分析研究的材料都来源于沪市历史上的顶部和底部的历史数据.从psy数据所得出的结论大多数是有效的,只有个别时期的 ...

  4. Unable to chmod

    不能改变权限 Unable to chmod /system/build.prop.: Read-only file system 解决方式: before chmod: Code: mount -o ...

  5. C#后台绑定ComboBox

    C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...

  6. 1.2 eclipse使用 :working set

    working set可以是相当于文件夹~~有多个project时, 分别存放在不同的 workingset下,可以方便管理 新建或编辑 working set时,需要记住选择project *可参照 ...

  7. 【jsp exception】如何处理jsp页面的错误

    根据jsp对错误的处理方式不同可以将其分为局部异常处理和全局异常处理.局部异常处理适用于个别jsp页面,当这些页面发生错误后,采取特殊的处理方式:全局异常处理适用于所有jsp页面,当所有页面发生某些指 ...

  8. python2.x 使用protobuf

    1.在windows下配置protobuf 1.1 下载对应的包,包含两个:protoc.exe和源码文件(protoc也可以自己生成) 下载地址1 --- google code最近在迁移,也许以后 ...

  9. OpenGL—Android 开机动画源码分析二

    引自http://blog.csdn.net/luoshengyang/article/details/7691321/ BootAnimation类的成员函数的实现比较长,我们分段来阅读: 第三个开 ...

  10. 解决 UNMOUNTABLE_BOOT_VOLUME 蓝屏【转载】

    现象:一台XP系统的机器,开机在滚动条阶段蓝屏,蓝屏代码大概是“UNMOUNTABLE_BOOT_VOLUME”. 用PE进系统后发现C盘的文件格式变为RAW,总容量等变为0 解决方法一:将故障机的硬 ...