Sparse Graph


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
 
 
题意:
  
  n 个点的无向完全图中删除 m 条边,问点 s 到其他点的最短路长度。
 
题解:
  
  

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 1e2+, mod = 1e9+, inf = 2e9; int T,n,m,vis[N],head[N],t,d[N],ans[N];
struct ss{int to,next;}e[N * ];
void add(int u,int v) {e[t].to=v;e[t].next=head[u];head[u]=t++;} int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
t =;memset(head,,sizeof(head));
for(int i = ; i <= m; ++i) {
int a,b;
scanf("%d%d",&a,&b);
add(a,b);add(b,a);
}
memset(vis,,sizeof(vis));
memset(d,-,sizeof(d));
int S;
scanf("%d",&S);
queue<int > q;
q.push(S);vis[S] = ;
d[S] = ;
set<int > s;
for(int i = ; i <= n; ++i) if(i != S) s.insert(i),vis[i] = ;
while(!q.empty()) {
int k = q.front();
q.pop();
for(int i = head[k]; i; i =e[i].next) {
int to = e[i].to;
if(s.count(to)) {
vis[to] = ;
}
}
for(set<int > ::iterator itt,it = s.begin();it != s.end(); ) {
if(vis[*it])
{
d[*it] = d[k] + ;
q.push(*it);
itt = it;
itt++;
s.erase(it);
it = itt;
} else {
it++;
}
}
for(set<int > ::iterator itt,it = s.begin();it != s.end(); it++) vis[*it] = ;
}
int cnt = ;
for(int i = ; i <= n; ++i) {
if(i != S)ans[++cnt] = d[i];
}
for(int i = ; i < cnt; ++i) printf("%d ",ans[i]);
printf("%d\n",ans[cnt]);
}
return ;
}

HDU 5876 Sparse Graph BFS 最短路的更多相关文章

  1. HDU 5876 Sparse Graph BFS+set删点

    Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices s ...

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

  3. hdu 5876 Sparse Graph 无权图bfs求最短路

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

  4. HDU 5876 Sparse Graph

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

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

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

  6. HDU 5876 Sparse Graph(补图上BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...

  7. hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路

    BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include&l ...

  8. HDU 5876 补图 单源 最短路

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

  9. HDU 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)

    题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...

随机推荐

  1. PHP带重试功能的curl

    2016年1月13日 10:48:10 星期三 /** * @param string $url 访问链接 * @param string $target 需要重试的标准: 返回结果中是否包含$tar ...

  2. Linux集群配置ntp时间同步服务

    集群中时间不同步有可能会让大数据的应用程序运行混乱,造成不可预知的问题,比如Hbase,当时间差别过大时就会挂掉,所以在大数据集群中,ntp服务,应该作为一种基础的服务,以下在演示在CentOS 7. ...

  3. FFmpeg frei0r water 滤镜

    FFmpeg frei0r water 滤镜, 在 linux 环境中很流畅,但在 XP 环境中抛出异常 研究一段时间修改了代码,能在 XP 里跑得动. sample.water.avi water. ...

  4. JS 基本语句

    1.循环中必备的条件: 初始值  循环条件  状态改变   循环体 for(初始值  循环条件  状态改变)    {       循环体     } for(var i=0;i<100;i++ ...

  5. Android 启动白屏或者黑屏闪现解决

    1.设置Style //1.设置背景图Theme <style name="Theme.AppStartLoad" parent="android:Theme&qu ...

  6. HDU 5884 Sort -2016 ICPC 青岛赛区网络赛

    题目链接 #include <iostream> #include <math.h> #include <stdio.h> #include<algorith ...

  7. iOS 系统消息

    转载自:http://momirror.blog.163.com/blog/static/48418184201361541235481/ 一.键盘 1.UIKeyboardWillShowNotif ...

  8. [Android Pro] Android 性能分析工具dumpsys的使用

    reference to : http://www.open-open.com/lib/view/open1405061994872.html Android提供的dumpsys工具可以用于查看感兴趣 ...

  9. oracle 的PACKAGE恢复过程

    SELECT obj# FROM obj$ AS OF TIMESTAMP TO_TIMESTAMP('2016-06-30', 'YYYY-MM-DD') WHERE NAME = 'PFWZ_AP ...

  10. 43个优秀的Swift开源项目

    作为一门集百家之长的新语言,Swift拥有着苹果先天的生态优势,而其在GitHub上各种优秀的开源项目也层出不穷.本文作者@SwiftLanguage从2014年6月苹果发布Swift语言以来,便通过 ...