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 cit**y1-cit**y2 and cit**y1-cit**y3. Then if cit**y1 is occupied by the enemy, we must have 1 highway repaired, that is the highway cit**y2-cit**y3.

Input Specification:

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

题目大意 给一个图,查询删掉某一个点之后还有几个连通块

思路分析 第五个例子太迷了,写邻接表和前向星都有段错误,不知道前向星是不是开小了,这题点只有1000个,所以还是用最传统的矩阵存储比较好,第五个测试样例接近完全图了都.这种点比较少的图很容易出这种恶心的样例

  1. #include<bits/stdc++.h>
  2. #define de(x) cout<<#x<<" "<<(x)<<endl
  3. #define each(a,b,c) for(int a=b;a<=c;a++)
  4. using namespace std;
  5. const int maxn=1000+5;
  6. const int maxm=1e6+5;
  7. const int inf=0x3f3f3f3f;
  8. //vector<int>G[maxn];
  9. /*
  10. int head[maxn];
  11. struct edge
  12. {
  13. int v;
  14. int next;
  15. }edge[maxm];
  16. int cnt;
  17. void addEdge(int u,int v)
  18. {
  19. edge[cnt].v=v;
  20. edge[cnt].next=head[u];
  21. head[u]=cnt++;
  22. }*/
  23. int a[maxn][maxn];
  24. ///段错误?????
  25. /*
  26. 3 2 3
  27. 1 2
  28. 1 3
  29. 1 2 3
  30. */
  31. int ban=0;
  32. bool vis[maxn];
  33. void dfs(int x,int n)
  34. {
  35. vis[x]=true;
  36. for(int i=1;i<=n;i++)
  37. {
  38. if(!vis[i]&&a[x][i]==1)
  39. {
  40. dfs(i,n);
  41. }
  42. }
  43. return;
  44. }
  45. int main()
  46. {
  47. int n,m,q;
  48. cin>>n>>m>>q;
  49. //memset(head,0,sizeof(head));
  50. //cnt=1;
  51. memset(a,0,sizeof(a));
  52. while(m--)
  53. {
  54. int aa,b;
  55. cin>>aa>>b;
  56. a[aa][b]=1;
  57. a[b][aa]=1;
  58. //G[a].push_back(b);
  59. //G[b].push_back(a);///别写vector和map了
  60. }
  61. while(q--)
  62. {
  63. int query;
  64. cin>>query;
  65. memset(vis,0,sizeof(vis));
  66. vis[query]=true;
  67. int cnt=0;
  68. for(int i=1;i<=n;i++)
  69. {
  70. if(vis[i])continue;
  71. dfs(i,n);
  72. cnt++;
  73. }
  74. printf("%d\n",cnt-1);
  75. }
  76. }

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

  4. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

  5. 1013. Battle Over Cities (25)(DFS遍历)

    For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city ...

  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. 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(并查集)

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

  9. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

随机推荐

  1. secureCRT操作redis-cli时, 不断追加ip:port

    Session Options-->Terminal-->Emulation-->Terminal为Linux

  2. 使用IOCP完成端口队列做任务队列

    使用IOCP完成端口队列做任务队列 与其自己费力设计异步任务队列,不如使用WINDOWS内核级的IOCP完成端口队列做任务队列. 1)引用单元 uses windows; 2)定义完成端口句柄 var ...

  3. 使用Expression动态创建lambda表达式

    using System;using System.Linq.Expressions;using System.Reflection; namespace Helper{ public class L ...

  4. unittest 的用法

    一.discover方法 discover方法可以根据标准加载用例,并将结果返回给测试套件(suite),start_dir:待测试的目录,pattern:测试用例文件名的匹配规. 如: start_ ...

  5. pytorch基础学习(一)

    在炼丹师的路上越走越远,开始入手pytorch框架的学习,越炼越熟吧... 1. 张量的创建和操作 创建为初始化矩阵,并初始化 a = torch.empty(, ) #创建一个5*3的未初始化矩阵 ...

  6. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  7. Framework7 + Angular 开发问题解决汇总

    本篇主要汇总一下使用Framework7 + Angular 开发中遇到的一些难点及我的解决方法,以后再遇到会在这里继续更新. 一.页面表格按需加载 情况描述:默认加载10条,在用户上拉页面是再进行下 ...

  8. osg create shape

    osg::ref_ptr<osg::Node> OSG_Qt_::createSimple() { osg::ref_ptr<osg::Geode> geode = new o ...

  9. 如何让在panel里的子窗体随panel的大小改变而变化?(转)

            private void Form1_Load(object sender, EventArgs e)         {             frm=new Form2();   ...

  10. LeetCode_122. Best Time to Buy and Sell Stock II

    122. Best Time to Buy and Sell Stock II Easy Say you have an array for which the ith element is the ...