Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1563    Accepted Submission(s): 549

Problem Description
In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are notadjacent in G.

Now you are given an undirected graph G of N nodes and M bidirectional edges of unit length. Consider the complement of G, i.e., H. For a given vertex S on H, you are required to compute the shortest distances from S to all N−1 other vertices.

 
Input
There are multiple test cases. The first line of input is an integer T(1≤T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2≤N≤200000) and M(0≤M≤20000). The following M lines each contains two distinct integers u,v(1≤u,v≤N) denoting an edge. And S (1≤S≤N) is given on the last line.
 
Output
For each of T test cases, print a single line consisting of N−1 space separated integers, denoting shortest distances of the remaining N−1 vertices from S (if a vertex cannot be reached from S, output ``-1" (without quotes) instead) in ascending order of vertex number.
 
Sample Input
1
2 0
1
 
Sample Output
1
 
思路:边的长度均为1,用bfs。遍历补图中与u相连接的结点v,并将其在全部结点的集合中删除。删除结点用set较快。
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <set>
  5. using namespace std;
  6. const int MAXN = ;
  7. int n, m, s;
  8. set<int> arc[MAXN];
  9. int d[MAXN];
  10. void bfs(int src)
  11. {
  12. memset(d, , sizeof(d));
  13. set<int> vec;
  14. for(int i = ; i <= n; i++)
  15. {
  16. vec.insert(i);
  17. }
  18. queue<int> que;
  19. que.push(src);
  20. vec.erase(src);
  21. while(!que.empty())
  22. {
  23. int u = que.front(); que.pop();
  24. for(set<int>:: iterator it = vec.begin(); it != vec.end(); it++)
  25. {
  26. int v = *it;
  27. if(arc[u].find(v) == arc[u].end())
  28. {
  29. que.push(v);
  30. d[v] = d[u] + ;
  31. vec.erase(v);
  32. }
  33. }
  34. if(vec.empty())
  35. {
  36. break;
  37. }
  38. }
  39. }
  40. int main()
  41. {
  42. int T;
  43. scanf("%d", &T);
  44. while(T--)
  45. {
  46. scanf("%d %d", &n, &m);
  47. for(int i = ; i <= n; i++) arc[i].clear();
  48. for(int i = ; i < m; i++)
  49. {
  50. int u, v;
  51. scanf("%d %d", &u, &v);
  52. arc[u].insert(v);
  53. arc[v].insert(u);
  54. }
  55. scanf("%d", &s);
  56. bfs(s);
  57. for(int i = ; i <= n; i++)
  58. {
  59. if(i == s)
  60. {
  61. continue;
  62. }
  63. else
  64. {
  65. if(d[i] == )
  66. {
  67. printf("-1");
  68. }
  69. else
  70. {
  71. printf("%d", d[i]);
  72. }
  73. }
  74. if(i != n)
  75. {
  76. printf(" ");
  77. }
  78. }
  79. printf("\n");
  80. }
  81. return ;
  82. }

HDOJ5876(补图的最短路)的更多相关文章

  1. HDU - 5876 :Sparse Graph (完全图的补图的最短路 -BFS&set)

    In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinc ...

  2. Sparse Graph---hdu5876(set+bfs+补图求最短路)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5876 题意:有一个含有n个点的无向图,已知图的补图含有m条边u, v:求在原图中,起点s到 ...

  3. HDU 5876 补图 单源 最短路

    ---恢复内容开始--- Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (J ...

  4. 图论 - Travel

    Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...

  5. 【ZJOI 2018】线图(树的枚举,hash,dp)

    线图 题目描述 九条可怜是一个热爱出题的女孩子. 今天可怜想要出一道和图论相关的题.在一张无向图 $G$ 上,我们可以对它进行一些非常有趣的变换,比如说对偶,又或者说取补.这样的操作往往可以赋予一些传 ...

  6. BZOJ 1137: [POI2009]Wsp 岛屿 半平面交

    1137: [POI2009]Wsp 岛屿 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 165  Solved: ...

  7. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  8. HDU 5876 Sparse Graph(补图中求最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 在补图中求s到其余各个点的最短路. 思路:因为这道题目每条边的距离都是1,所以可以直接用bfs来做 ...

  9. SCU 4444 Travel (补图最短路)

    Travel The country frog lives in has \(n\) towns which are conveniently numbered by \(1, 2, \dots, n ...

随机推荐

  1. Bellman-Ford算法 - 有向图单源最短路径

    2017-07-27  08:58:08 writer:pprp 参考书目:张新华的<算法竞赛宝典> Bellman-Ford算法是求有向图单源最短路径的,dijkstra算法的条件是图中 ...

  2. GridView右键菜单

    一.添加右键菜单 1.在VS工具箱中的“菜单和工具栏”找到ContextMenuStrip控件,双击添加. 2.点击ContextMenuStrip右上方的小三角形,打开编辑项,可以添加菜单项.至于菜 ...

  3. JDK__下载地址

    1. http://www.oracle.com/technetwork/java/archive-139210.html ZC: 貌似 从JDK7开始,有for ARM的版本,类似 : “Linux ...

  4. Matlab操作矩阵的相关方法

    Matlab操作矩阵的相关方法 下面这篇文章主要是对吴恩达老师机器学习中matlab操作的一个整理和归纳 一.基本操作 1.生成矩阵(ones.zeros) A = [1 2;3 4;5 6]    ...

  5. 关于EventBus3.0使用,你看这篇就够了

    作为一枚Android开发者,关于EventBus相信应该都听说过.要是用过就请忽略本文,本文讲得比较基础. 要是没用过,建议你花两分钟看看. 目前EventBus最新版本是3.0,本demo基于3. ...

  6. 异步编程——promise

    异步编程--promise 定义 Promise是异步编程的一个解决方案,相比传统的解决方法--回调函数,使用Promise更为合理和强大,避免了回调函数之间的层层嵌套,也使得代码结构更为清晰,便于维 ...

  7. selenium学习笔记(智能等待)

    博主在尝试对百度首页用selenium完成自动登录的功能 反复多次尝试元素定位方法也未写错.最后发现问题原因: 脚本运行速度快于页面加载速度 如百度首页登录例子.脚本已经开始寻找登录弹窗 但是页面仍在 ...

  8. python学习笔记(excel+requests)

    已经可以对excel简单的操作后 可以开始通过excel写测试用例 读取用例 执行用例 提前写好execl 如图: 下面是代码: #!/usr/bin/env python # -*- coding: ...

  9. Netty原理

    1. Netty简介Netty是一个高性能.异步事件驱动的NIO框架,基于JAVA NIO提供的API实现.它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都 ...

  10. WPF Invoke和BeginInvoke

    在WPF中Invoke和BeginInvoke和Winform中的是差不多的,只是一个用Control的一个用Dispatcher的. 而Invoke和BeginInvoke的区别嘛 就是一个是同步的 ...