HDU
来源:互联网 发布:yum安装ruby最新版本 编辑:程序博客网 时间:2024/06/05 17:40
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 notnot adjacent 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
这玩意因为最多松弛两次…
完全图嘛最多两个点距离就是2
#include<queue>#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<map>#include<vector>#include<set>using namespace std;int daon[200001];bool bj[200001];//bool mp[200001][200001];set<int>mp[200001];int n, m, q, w, s;int T;//int time[1];void spfa(int x){ memset(bj, 0, sizeof(bj)); memset(daon, 0, sizeof(daon)); int sum = 1; queue<int> q; bj[x] = 1; for (int i = 1; i <= n; i++) { if (!mp[x].count(i) && i != x) { q.push(i); daon[i] = 1; bj[i] = 1; sum++; } } while (!q.empty()) { int y = q.front(); q.pop(); for (int i = 1; i <= n; i++) { if (!bj[i]) { if (!mp[y].count(i)) { q.push(i); bj[i] = 1; daon[i] = daon[y] + 1; sum++; } } if (sum == n) break; } if (sum == n) break; }}struct p{ int d, b; bool operator < (const p&a)const { return b > a.b; }};int main(){ cin >> T; while (T--) { memset(daon, 0x3f, sizeof(daon)); memset(bj, 0, sizeof(bj)); cin >> n >> m; for (int a = 1; a <= n; a++)mp[a].clear(); for (int a = 1; a <= m; a++) { scanf("%d%d", &q, &w); mp[q].insert(w); mp[w].insert(q); } cin >> s; spfa(s); int jc = 0; for (int a = 1; a <= n; a++) { if (a == s)continue; if (!jc) { printf("%d", daon[a] == 0x3f3f3f3f ? -1 : daon[a]); jc = 1; continue; } printf(" %d", daon[a] == 0x3f3f3f3f ? -1 : daon[a]); } cout << endl; }}
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- Linux进程入门学习(八)-消息队列通信
- OPENCV编辑问题汇总
- spring boot写单元测试(测试service层)
- 组合数学学习笔记(二)
- C语言知识框架
- HDU
- Python3学习(18)--偏函数(Partial)
- 360在线编程2017秋招——粉笔装盒子
- java 对象克隆 及深克隆 浅克隆 及Cloneable接口
- CSS—常用选择器
- POJ 2455 Secret Milking Machine 最大流建模
- 并发容器及典型源码分析
- 如何在生产环境使用Btrace进行调试
- 运维-安装系列文件之操作步骤