题干:

In graph theory, the complementcomplement of a graph GG is a graph HH on the same vertices such that two distinct vertices of HH are adjacent if and only if they are notnotadjacent in GG.

Now you are given an undirected graph GG of NN nodes and MM bidirectional edges of unitunit length. Consider the complement of GG, i.e., HH. For a given vertex SS on HH, you are required to compute the shortest distances from SS to all N−1N−1 other vertices.

Input

There are multiple test cases. The first line of input is an integer T(1≤T<35)T(1≤T<35)denoting the number of test cases. For each test case, the first line contains two integers N(2≤N≤200000)N(2≤N≤200000) and M(0≤M≤20000)M(0≤M≤20000). The following MM lines each contains two distinct integers u,v(1≤u,v≤N)u,v(1≤u,v≤N) denoting an edge. And S (1≤S≤N)S (1≤S≤N)is given on the last line.

Output

For each of TT test cases, print a single line consisting of N−1N−1 space separated integers, denoting shortest distances of the remaining N−1N−1 vertices from SS (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

题目大意:

给一个N个点M条边的无向图,给你一个圆点,让你在对应图的补图上求出起点S到每个点的最短路。

解题报告:

正常bfs是对于当前点来说枚举当前点的每一条边,看该点是否遍历过来决定是否进行入队操作。而对于这个题,因为补图的边的数量非常大,所以我们可以从点的角度入手,因为一共需要更新1e5个点,所以我们可以直接在每一次循环中都先枚举每一个点,看他和当前点是否有连边,如果原图中没有这条边的话那就可以直接更新这个点,并且把他放到队列中。通过枚举边变成枚举点,减少时间复杂度。也就是正常bfs是枚举每一个边看是否出点被更新过,这个题因为边数很多但是点数不多,所以可以枚举每一个没出现过的点,看这条边是否存在。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int MAX = 2e5 + 5;
set<int> ss;
set<int> vv[MAX];
int st,n,m;
int dis[MAX];
void bfs(int st) {for(int i = 1; i<=n; i++) dis[i] = INF;dis[st] = 0;ss.erase(st);queue<int> q;q.push(st);while(q.size()) {vector<int> tmp;int cur = q.front();q.pop();for(auto it = ss.begin();it!=ss.end(); ++it) {int v = *it;if(vv[cur].find(v) == vv[cur].end()) {dis[v] = dis[cur]+1;tmp.pb(v); q.push(v);} }for(auto v : tmp) ss.erase(v);}
}
int main()
{int T;cin>>T;while(T--) {scanf("%d%d",&n,&m);ss.clear();for(int i = 1; i<=n; i++) ss.insert(i);for(int i = 1; i<=n; i++) vv[i].clear();for(int u,v,i = 1; i<=m; i++) scanf("%d%d",&u,&v),vv[u].insert(v),vv[v].insert(u);scanf("%d",&st);bfs(st);int cnt = 1;for(int i = 1; i<=n; i++) {if(i == st) continue;if(dis[i] == INF) printf("%d%c",-1,cnt == n-1 ? '\n' :' ') ;else printf("%d%c",dis[i],cnt == n-1 ? '\n' :' ');cnt++;}}return 0 ;
}

【HDU - 5876】Sparse Graph(补图bfs,STLset)相关推荐

  1. HDU - 5876 Sparse Graph 2016 ACM/ICPC 大连网络赛 I题 bfs+set+补图最短路

    题目链接 题意:给的补图,让你求一个源点到其他点的最短距离,因为图太稠密了, 用dij以及spfa根本不得行,这里只能用一种我不会方法来进行,这里用了bfs的方法以及set来维护,分别set维护一个未 ...

  2. HDU - 5876 Sparse Graph(bfs+set)

    题目链接:点击查看 题目大意:给定一个无向图G,规定H为G的补图,求在H上关于点s的单源最短路 题目分析:因为H是G的补图,所以可以用整个图减去G得到H,然后对H跑一边迪杰斯特拉,可是整个图最多能达到 ...

  3. HDU 5876 关于补图的bfs

    1.HDU 5876  Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...

  4. 专题训练二 搜索进阶 HDU - 3085 Nightmare Ⅱ (双向BFS + 曼哈顿距离)

    HDU - 3085 Nightmare Ⅱ (双向BFS + 曼哈顿距离) Problem Description Last night, little erriyue had a horrible ...

  5. 论文翻译 SGCN:Sparse Graph Convolution Network for Pedestrian Trajectory Prediction 用于行人轨迹预测的稀疏图卷积网络

    SGCN:Sparse Graph Convolution Network for Pedestrian Trajectory Prediction 用于行人轨迹预测的稀疏图卷积网络 行人轨迹预测是自 ...

  6. HDU 5836 Rubik's Cube BFS

    Rubik's Cube 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5836 Description As we all know, Zhu is ...

  7. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

  8. HDU(1175),连连看,BFS

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...

  9. HDU-5876 Sparse Graph

    题目大意: 给你一个完全图让你删除给出的这些边形成新的图,问你在新的图上的s点到其它所有点的距离. 解题思路: BFS乱搞... 补图的BFS的问题虽然很经典= =不过确实还是第一次做.很方. 用一个 ...

最新文章

  1. 无线红外探测器04:产品测试及验证
  2. 在Linux中某些程序无法运行,为何linux下的程序不能在windows下运行,不是“废话”那么简单...
  3. 多线程小抄集(新编三)
  4. toj 4597 字符识别?
  5. sqlserver命令行修改用户登录密码
  6. 111 《深入理解Java虚拟机》读书笔记
  7. 品运维自动化之cobbler的安装序
  8. 两年经验,尽然斩获多家巨头offer,如:蚂蚁、头条、PingCAP~
  9. Linux中级之ansible配置(playbook)
  10. linux脚本打印变量的值,linux利用read命令获取变量中的值
  11. 2021-12-08 根据XPS 价带谱计算价带谱中心
  12. explore进程和linux,解析Svchost.exe和Explorer.exe两大系统进程(zt)
  13. 在AI里怎么把一行字拆成单个的字,并且可编辑
  14. 牛客网 8.2 网易2017校招 Java 第一题 下厨房
  15. kubectl logs和docker logs输出日志不同的问题
  16. java ant解压缩_java ant包中的org.apache.tools.zip实现压缩和解压缩实例详解
  17. 嵌入式硬件抽象层HAL的设计实现
  18. Swift5 10.初始化Initialization(待深究)
  19. 苹果xr黑屏转圈圈解决方法_iPhonexr黑屏转圈怎样解决?
  20. [LGOJ5558]心上秋(倍增)

热门文章

  1. Linq 学习笔记(二)
  2. 解决“A problem has been encountered while loading the setup components. Canceling setup.”的问题...
  3. h5禁用浏览器下载视频_【必备】 一键视频下载器插件,非常好用的浏览器插件!...
  4. normalize函数_提取棋盘格角点函数解析
  5. mysql分库一致性_分库分表带来的完整性和一致性问题
  6. 1359C. Mixing Water
  7. C#多线程时对同一资源加锁实现互斥访问
  8. linux远程虚拟桌面,2020-07-23 Linux 远程连接虚拟桌面
  9. redmine两个mysql_Redmine3.4.2安装记(Win10+MySql)
  10. UE4 多个static mesh合并成一个static mesh