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. 时间和日期控件(Calendar1)

    取得选择的: taskItem["data"] = Calendar1.SelectedDate.ToShortDateString();

  2. POJ 2421(prim)

    http://poj.org/problem?id=2421 这个题和poj1258是一样的,只要在1258的基础上那么几行代码,就可以A,水. 题意:还是n连通问题,和1258不同的就是这个还有几条 ...

  3. poj 3984

    http://poj.org/problem?id=3984 题目很简单,就是简单的BFS吧,主要的难点在于坐标的问题 这个呢,可以反其道而行之,就是你从(1,1)到(5,5),你肯定走过一次 走过一 ...

  4. Sql Server事务简单用法

    var conStr = "server=localhost;database=Data;user=sa;pwd=123456"; using (var connection = ...

  5. WIZnet官方网盘

    之前使用过 WIZnet 的TCP/IP 解决方案,资源较少, 偶然发现此网盘,不敢独享,访问 请戳此处.

  6. Metro各种流转换

    Ibuffer转byte[] ,(int)buffer.Length); Byte[]转Ibuffer WindowsRuntimeBufferExtensions.AsBuffer(bytes,,b ...

  7. [ 转] [Android]多式样ProgressBar

    多式样ProgressBar 普通圆形ProgressBar 该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中. 一般只要在XML布局中定义就可以了. < ...

  8. js中我的注释规范

    模块功能描述说明: /** * ------------------------------------------------------------------ * 模块描述说明 * ------ ...

  9. java Http原生 Get 和Post 支持代理认证

    import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import ...

  10. SQL语句简介

    1.Top.Distinct Top 获取前几条数据,top一般都与order by连用 获得年纪最小的5个学生  select top 5 * from A order by classesId 获 ...