并查集判断连通性。

#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. 抛弃jQuery,拥抱原生JavaScript

    前端发展很快,现代浏览器原生 API 已经足够好用.我们并不需要为了操作 DOM.Event 等再学习一下 jQuery 的 API.同时由于 React.Angular.Vue 等框架的流行,直接操 ...

  2. 一篇很全面的freemarker教程 前端工程师必备

    FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1,文本:直接输出的部分 2,注释:<#-- ... -->格式部分,不会输 ...

  3. Linux 添加ssh 公钥访问

    登陆被管理的服务器,进入需要远程登陆的用户目录,把公钥放到用户目录的 .ssh 这个目录下(如果目录不存在,需要创建~/.ssh目录,并把目录权限设置为700),把公钥改名为authorized_ke ...

  4. git clone分支

    有时git clone下来会出现很多branch,更麻烦的是如果主分支没代码那你就只能看到.git目录了.如下面的这个: $ git clonegit://gitorious.org/android- ...

  5. 自定义switch开关

    自定义一个switch开关 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  6. 扫描局域网内的ip和主机名

    1. 目的 今天发现我配置的一台电脑ip被人占用了,所以准备写个程序扫描一下局域网内所有正在使用的ip和主机名 2. 实现--直接上代码 import time import threading im ...

  7. Simple But Useful Samples About 'grep' Command(简单实用的grep 命令)

    Do the following: grep -rnw '/path/to/somewhere/' -e "pattern" -r or -R is recursive, -n i ...

  8. [转]慎用InputStream的read()方法

    InputStream 此抽象类是表示字节输入流的所有类的超类. 我们从输入流中读取数据最常用的方法基本上就是如下 3 个 read() 方法了: 1 . read () 方法,这个方法 从输入流中读 ...

  9. Dom2016/4/20

    childNode:标准情况下:包括文本节点和元素节点 非标准下:只包括元素节点 在标准情况下:包含非法嵌套的子节点. 非标准下:ie7一下的版本不包含非法嵌套的子节点 DOm的节点类型:12种 元素 ...

  10. HDU4325--Flowers--树状数组,离散化

    Description As is known to all, the blooming time and duration varies between different kinds of flo ...