题意:

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

trick:

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

AAAAAccepted code:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[],b[];
  4. int fa[];
  5. int find_(int x){
  6. if(fa[x]==x)
  7. return x;
  8. else
  9. return fa[x]=find_(fa[x]);
  10. }
  11. int main(){
  12. int n,m,k;
  13. cin>>n>>m>>k;
  14. for(int i=;i<=m;++i)
  15. cin>>a[i]>>b[i];
  16. for(int i=;i<=k;++i){
  17. int x;
  18. cin>>x;
  19. for(int j=;j<=n;++j)
  20. fa[j]=j;
  21. for(int j=;j<=m;++j){
  22. if(a[j]==x||b[j]==x)
  23. continue;
  24. int t=find_(a[j]);
  25. int tt=find_(b[j]);
  26. if(t!=tt)
  27. fa[t]=tt;
  28. }
  29. int cnt=-;
  30. for(int j=;j<=n;++j){
  31. if(j==x)
  32. continue;
  33. if(fa[j]==j)
  34. cnt++;
  35. }
  36. cout<<cnt<<"\n";
  37. }
  38. return ;
  39. }

【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. 计算机二级-C语言-程序填空题-190109记录-对二维字符串数组的处理

    //给定程序,函数fun的功能是:求出形参ss所指字符串数组中最长字符串的长度,将其余字符串右边用字符*补齐,使其与最长的字符串等长.ss所指字符串数组中共有M个字符串,且串长<N. //重难点 ...

  2. eclipse 自定义代码块设置(代码模板)

    eclipse设置自定义代码模板 window -> show View -> other -> Templates 原来main模板 修改模板 再次插入

  3. BETWEEN

    选取介于两个值之间的数据范围内的值.这些值可以是数值.文本或者日期. 选取 alexa 介于 1 和 20 之间的所有网站: SELECT * FROM table ; NOT BETWEEN 显示不 ...

  4. Cookie API介绍

    一.Java提供的操作Cookie的API Java中的javax.servlet.http.Cookie类用于创建一个Cookie Cookie类的主要方法 No. 方法 类型 描述 Cookie( ...

  5. Mybatis空指针

    查询数据,返回的字段要在 resultMap 中定义, <resultMap id="BaseResultMap" type="com...." > ...

  6. Jekyll+Github个人博客构建之路

    请参考: http://robotkang.cc/2017/03/HowToCreateBlog/

  7. QWidget::setLayout: Attempting to set QLayout "" on xxx "", which already has a layout

    QLayout是Qt应用开发中一个非常重要的组件,然而平时使用的时候不小心经常会发现控制台有类似如下的警告: QWidget::setLayout: Attempting to set QLayout ...

  8. 01 认识python

    python介绍 python的创始⼈为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决⼼ 开发⼀个新的脚本解释程序,作为ABC语 ...

  9. IDEA 下的 github 创建提交与修改

    本章假定你已经安装了 git 客户端,本文仅仅使用与 Mac 环境下,未在 Window下实验,但 IDEA 在 Window 和 Mac 下软件的使用方法是一致的. 1 配置账号 IDEA 需要配置 ...

  10. linux和windows系统的区别

    在21世纪的今天,互联网可以说是当代发展最为迅速的行业,举个很简单的例子,现在的我们不论什么年龄阶层,几乎人手都有一部手机,上面的某博,某音,末手等软件,更是受到多数人的热爱,并且人们不仅仅用其来消遣 ...