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

Input

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), 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

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
    题目大意:给出n个城市之间有相互连接的m条道路,当删除一个城市和其连接的道路的时候,
    问其他几个剩余的城市至少要添加多少个路线才能让它们重新变为连通图,其实就是求连通分支数
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. int graph[][];
  6. int visited[];
  7. int n,m,k;
  8. void dfs( int a)
  9. {
  10. int i;
  11. visited[a]=;
  12. for( i=; i<=n; i++)
  13. {
  14. if( visited[i]== && graph[a][i]==)
  15. dfs(i);
  16. }
  17. }
  18. int main()
  19. {
  20. int cnt=,temp;
  21. int i,j;
  22. int a,b;
  23. scanf("%d%d%d",&n,&m,&k);
  24. for( i=; i<m; i++) //创建图
  25. {
  26. scanf("%d%d",&a,&b);
  27. graph[a][b]=graph[b][a]=;
  28. }
  29. for( i=; i<k ; i++)
  30. {
  31. cnt=;
  32. memset( visited,,sizeof(visited)); //每次都将visited全置0
  33. scanf("%d",&temp);
  34. visited[temp]=;
  35. for( j=; j<=n; j++)
  36. {
  37. if( visited[j]==)
  38. {
  39. dfs(j);
  40. cnt++; //连通分支数
  41. }
  42. }
  43. printf("%d\n",cnt-); //需要建的高速为连通分支数减1
  44. }
  45. return ;
  46. }
  1.  

1013. Battle Over Cities (25)(DFS遍历)的更多相关文章

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

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

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

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

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

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

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

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

  7. 1013. Battle Over Cities (25)

    题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...

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

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

  9. 1013. Battle Over Cities 用dfs计算联通分量

    使用一个标记数组,标记 节点是否已访问 int 连通度=0 dfs(node i) {标记当前节点为以访问 for(每一个节点) {if(当前几点未访问 并且 从i到当前节点有直接路径) dfs(当前 ...

随机推荐

  1. 143 Reorder List 重排链表

    给定一个单链表L:L0→L1→…→Ln-1→Ln,重新排列后为: L0→Ln→L1→Ln-1→L2→Ln-2→…必须在不改变节点的值的情况下进行原地操作.例如,给定链表 {1,2,3,4},按要求重排 ...

  2. .net core跨域设置

    services.AddCors(options => options.AddPolicy("AllowSameDomain", builder => builder. ...

  3. sys模块详解

    1.sys.argv argv是「argument variable」参数变量的简写形式,一般在命令行调用的时候由系统传递给程序.这个变量其实是一个List,argv[0] 一般是“被调用的脚本文件名 ...

  4. Android RxJava小结

    一.如何使用 在build.gradle中添加依赖 dependencies { api 'io.reactivex:rxandroid:1.2.1' api 'io.reactivex:rxjava ...

  5. RecyclerView 缓存机制学习笔记2

    RecyclerView 初始化所有的视图后,调用 去缓存(StaggeredGridLayoutManager), 而不是初始化一次缓存一次 存储后系统又会去调用tryGetViewHolderFo ...

  6. PMP项目管理学习笔记(10)——范围管理之收集需求

    一个星期没看书,没记录笔记,没能坚持下来,感觉好罪过.现在我要重新上路! 收集需求 收集需求就是与项目的所有干系人坐在一起,得出他们的需求是什么,这就是收集需求过程中要做的事情.你的项目要想成功,你就 ...

  7. vijos 1053 Easy sssp

    描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 要求你写一个程序, 判断这个有向图中是否存在负权回路. 如果从一 ...

  8. Window.Event.KeyCode的含义

    Window.Event.KeyCode=13的含义(转载) 2011-04-16 09:41:18|  分类: html |  标签:keycode  event  realkey  var  do ...

  9. Python3简明教程(十四)—— Collections模块

    collections 是 Python 内建的一个集合模块,提供了许多有用的集合类. 在这个实验我们会学习 Collections 模块.这个模块实现了一些很好的数据结构,它们能帮助你解决各种实际问 ...

  10. Vue的 $parent,并不能准确找到上一层的控件,所以如果需要,需要填坑这个 bug,递归寻找下上级

    Vue的 $parent,并不能准确找到上一层的控件,所以如果需要,需要填坑这个 bug,递归寻找下上级 // Find components upward function findComponen ...