题意:

输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6)。有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公路全部不能使用,求增加多少条高速公路可以使剩下N-1个城市联通。(原本城市之间可能不联通,假设原本联通只能通过第0,4个数据)。

trick:

同一份代码交很多次,有几次会第4个点超时。(存疑)建议采用g++而不是clang++

AAAAAccepted code:

 #include<bits/stdc++.h>
using namespace std;
int a[],b[];
int fa[];
int find_(int x){
if(fa[x]==x)
return x;
else
return fa[x]=find_(fa[x]);
}
int main(){
int n,m,k;
cin>>n>>m>>k;
for(int i=;i<=m;++i)
cin>>a[i]>>b[i];
for(int i=;i<=k;++i){
int x;
cin>>x;
for(int j=;j<=n;++j)
fa[j]=j;
for(int j=;j<=m;++j){
if(a[j]==x||b[j]==x)
continue;
int t=find_(a[j]);
int tt=find_(b[j]);
if(t!=tt)
fa[t]=tt;
}
int cnt=-;
for(int j=;j<=n;++j){
if(j==x)
continue;
if(fa[j]==j)
cnt++;
}
cout<<cnt<<"\n";
}
return ;
}

【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)的更多相关文章

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

  2. PAT A 1013. Battle Over Cities (25)【并查集】

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

  3. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  4. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

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

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

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

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

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

随机推荐

  1. Mysql2docx自动生成数据库说明文档

    [需要python3.0以上] 首先安装Mysql2docx,如下: pip install Mysql2docx 然后打开pycharm,新建test.py # python from Mysql2 ...

  2. django admin后台(数据库简单管理后台)

    只需要简单的几行胆码就可以生成一个完整的管理后台 这个就是django魅力之一 创建超级用户 python manage.py createsuperuser     ----  之后会提示输入用慕名 ...

  3. 自编C++游戏

    背景 周末无聊,于是编了一个类似于cmd的小玩意. 可是越想越不对劲,所以把它改成了一个小游戏. 信息 语言:DEV_C++ 源代码已公布!! 打败GG 版本:1.0(正式版) 版本:1.5(番外) ...

  4. JAVA 中集合ConcurrentMap

    ConcurrentMap ConcurrentMap,它是一个接口,是一个能够支持并发访问的java.util.map集合 在原有java.util.map接口基础上又新提供了4种方法,进一步扩展了 ...

  5. 普及C组第二题(8.1)

    2000. [2015.8.6普及组模拟赛]Leo搭积木(brick) 题目: Leo是一个快乐的火星人,总是能和地球上的OIers玩得很high.         2012到了,Leo又被召回火星了 ...

  6. 去除移动端苹果手机(ios)的input默认样式与input禁止键盘出现的方式

    样式: input{-webkit-appearance: none;} 在iPhone plus点击input框出生日期时会出现如下图: 为了去掉下面那条苹果自带的,可以这样处理:在HTML中的in ...

  7. N个数求和(PTA)

    这题多输出了一个空格,卡了半天... leetcode刷多了,后遗症 这题可以用scanf("%lld/%lld"),直接读入,不过我用了stoll,也就是stoi,string ...

  8. 【visio】 图片

    1.背景页设置 新建背景页:新建页面>设计>页面设置>页属性      使用背景页:在页属性页的"背景" 选项里,选择需要的背景. 2.插入图片 支持插入本地图片 ...

  9. Django中url name

    花了好长时间才明白这个name参数的含义.便写下来了备忘 当我们在url的时候,一般情况下都是使用很明确的url地址.如在网页里面使用<a href="/login"> ...

  10. 5_5 集合栈计算机(UVa12096)<stack与STL其他容器的综合运用>

    有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始化为空的栈,并支持以下操作:( 维护 N(1≤N≤2000) 个操作, 可能的五种操作如下:) ■PUSH: 在栈顶加入一个空集合 A= ...