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 by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output Specification:

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input:

  1. 3 2 3
  2. 1 2
  3. 1 3
  4. 1 2 3

Sample Output:

  1. 1
  2. 0
  3. 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define maxnum 100005
  5. int n,m,k;
  6. int mp[][];
  7. int temp[][];
  8. int vis[] = {};
  9.  
  10. int findnotvis(){
  11. for(int i=;i <= n;i++){
  12. if(!vis[i]) return i;
  13. }
  14. return -;
  15. }
  16.  
  17. void dfs(int x){
  18. vis[x] = ;
  19. for(int i=;i <= n;i++){
  20. if(!vis[i]&&mp[x][i]){
  21. dfs(i);
  22. }
  23. }
  24. }
  25.  
  26. int main(){
  27. cin >> n >> m >> k;
  28. memset(mp,, sizeof(mp));
  29. for(int i=;i < m;i++){
  30. int x,y;scanf("%d%d",&x,&y);
  31. mp[x][y] = mp[y][x] = ;
  32. }
  33.  
  34. while(k--){
  35. int num;cin >> num;
  36. vis[num] = ; //只要把点置为访问过的就可以了,不需要删去边
  37. int cnt = ;
  38. for(int i=;i <= n;i++){ //这个写法很好,学习了。
  39. if(!vis[i]){
  40. dfs(i);
  41. cnt++;
  42. }
  43. }
  44. cout << cnt- << endl;
  45. memset(vis,, sizeof(vis));
  46. }
  47.  
  48. return ;
  49. }

果然用cin输入数据会超时。。

其实这是考察图的问题,删除图的一个节点,是其他节点成为连通图,至少需要添加多少条线

添加最少的路线,就是连通分量数-1(例:n个互相独立的连通分量组成一个连通图,只需要连n-1条线就可以)

这道题最重要就是求除去图的一个节点后 剩余的连通分量的数目

利用邻接矩阵v存储路线,用visit数组表示城市是否被遍历过

对于每个被占领的城市,将其表示为遍历过的状态true即可

利用深度优先遍历dfs计算连通分量数目

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<string>
  5. #include<cstring>
  6. #include<queue>
  7. #include<cmath>
  8. #include<set>
  9. #include<map>
  10. #define ll long long
  11. #define lson l,m,rt<<1
  12. #define rson m+1,r,rt<<1|1
  13. using namespace std;
  14. const int N=;
  15. int n,m,a[N*N],b[N*N],pre[N];
  16.  
  17. int f(int x){
  18. if(pre[x]==x)return x;
  19. pre[x]=f(pre[x]);
  20. return pre[x];
  21. }
  22.  
  23. void link(int x,int y){
  24. int r1=f(x),r2=f(y);
  25. if(r1!=r2){
  26. pre[r2]=r1;
  27. }
  28. }
  29. void solve(int city){
  30. for(int i=;i<=n;i++){
  31. pre[i]=i;
  32. }
  33. for(int i=;i<m;i++){
  34. if(a[i]==city||b[i]==city)continue;
  35. link(a[i],b[i]);
  36. }
  37. int ans=;
  38. for(int i=;i<=n;i++){ //查找堆的个数
  39. if(i==city)continue;
  40. int ri=f(i);
  41. if(ri==i)ans++; //是个堆顶
  1. } printf("%d\n",ans-); } int main(){ int k; scanf("%d%d%d",&n,&m,&k); for(int i=;i<m;i++){ scanf("%d%d",&a[i],&b[i]); } int x; for(int i=;i<=k;i++){ scanf("%d",&x); solve(x); } }

用并查集也挺好的思想

 

  1.  

PAT 1013 Battle Over Cities的更多相关文章

  1. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  2. pat 1013 Battle Over Cities(25 分) (并查集)

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

  3. PAT 1013 Battle Over Cities (dfs求连通分量)

    It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...

  4. PAT 1013 Battle Over Cities DFS深搜

    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

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

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

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

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

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

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

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

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

随机推荐

  1. 【C#】C#学习笔记_1

    C#的程序入口为某一个类里面的static void Main(string[] args){}方法,如果一个工程有多个Main方法,那么需要在工程配置中选择一个作为程序入口. C#的输入.输出操作在 ...

  2. java 偏向锁,轻量锁,重量级锁

    synchronized的执行过程: 1. 检测Mark Word里面是不是当前线程的ID,如果是,表示当前线程处于偏向锁 2. 如果不是,则使用CAS将当前线程的ID替换Mard Word,如果成功 ...

  3. js为什么返回两个对象字符串 objcet objcet ?

    js中两个使用 toString() 对有个有对象的数组进行操作时,为什么返回两个对象字符串 objcet objcet ? [{}].toString(); 返回 "[object Obj ...

  4. 【译】第12节---数据注解-ConcurrencyCheck

    原文:http://www.entityframeworktutorial.net/code-first/concurrencycheck-dataannotations-attribute-in-c ...

  5. BZOJ 2648 SJY摆棋子(KD Tree)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2648 题意: 思路: KDtree模板题. 参考自http://www.cnblogs.com/ra ...

  6. 项目Alpha冲刺——代码规范、冲刺任务与计划

    作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Alpha冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及可视化分析平台 这个作业的 ...

  7. Java生成指定长度的随机数

    char[] str = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', ' ...

  8. JavaScript的案例(数据校验,js轮播图,页面定时弹窗)

    1.数据校验            步骤            1.确定事件(onsubmit)并绑定一个函数            2.书写这个函数,获取数据,并绑定id            3. ...

  9. Trailing Zeroes (I) LightOJ - 1028

    题意就是给你一个数让你找它的正因子个数(包括自身,不包括1),这个地方用到一个公式,如果不用的话按正常思路来写会TL什么的反正就是不容易写对. 求任意一个大于1的整数的正因子个数 首先任意一个数n,n ...

  10. UT, FT ,E2E 测试的意思

    前端实现自动化就要借助到unit和e2e端到端测试了 一.unit测试(FT 就是Fucntion Test 功能测试,  注意不是: funciton函数 ...fucntion功能   不一样哦  ...