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 not adjacent 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.

InputThere 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.OutputFor 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

题意:给定一个无向图,求它的补图中S到每一点的最短路。

思路:我们BFS,长度从0,1,2...慢慢试探,由于是补图,显然试探的次数不会太多就可以弄完,所以我们可以暴力一点,一次BFS,我们取出队首u,其最短距离是dis[u],那么它没有被访问的点中(满足dis==-1),不与u相邻的点的最短距离是dis[u]+1,将其加入队首;  我们可以用set来表示未被访问的点。

由于S1.swap(S2)是两个set的指针交换,所以复杂度是O(1),比较快的。主要复杂度再S.clear那里,clear的复杂度是元素个数,由于是补图,所以可以假设放进去之后几个回合内就删完了,复杂度不会太高。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],dis[maxn],num[maxn];
int q[maxn],tot,cnt,S,N;
void add(int u,int v){
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
}
void BFS()
{
dis[S]=;
set<int>S1,S2;
set<int>::iterator it;
queue<int>q;
q.push(S);
rep(i,,N) if(i!=S) S1.insert(i);
while(!q.empty()){
int u=q.front(); q.pop();
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i]; if(S1.find(v)==S1.end()) continue;
S1.erase(v); S2.insert(v);
}
for(it=S1.begin();it!=S1.end();it++) dis[*it]=dis[u]+,q.push(*it);
S1.swap(S2); S2.clear();
}
}
int main()
{
int T,M,u,v;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
cnt=; rep(i,,N) Laxt[i]=,dis[i]=-;
rep(i,,M){
scanf("%d%d",&u,&v);
add(u,v); add(v,u);
}
scanf("%d",&S);
BFS();
rep(i,,N){
if(i!=S){
if(i!=N) printf("%d ",dis[i]);
else printf("%d\n",dis[i]);
}
}
}
return ;
}

HDU - 5876 :Sparse Graph (完全图的补图的最短路 -BFS&set)的更多相关文章

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

  2. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  3. HDU 5876 Sparse Graph

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

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

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

  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 BFS+set删点

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

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

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

随机推荐

  1. 使用FireFox插件RESTClient工具POST方法?

    下面尝试用Firefox的restclient,来调取api 当然需要打开火狐浏览器安装restclient的插件https://addons.mozilla.org/en-US/firefox/ad ...

  2. maven 本地仓库的配置

    <?xml version="1.0" encoding="UTF-8"?> <!--Licensed to the Apache Softw ...

  3. 《用 Python 学微积分》笔记 1

    <用 Python 学微积分>原文见参考资料 1. 1.多项式 f(x)=x3-5x2+9 def f(x): return x**3 - 5*x**2 + 9 print f(3) pr ...

  4. http & https & http2.0

    一.http状态码 1xx(信息性状态码,接受的请求正在处理) 2xx(成功状态码,请求正常处理完毕)200 OK204 No Content:请求成功但没有资源返回206 Partial Conte ...

  5. 设计模式--观察者模式C++实现

    观察者模式C++实现 1定义 Observer/Publish/subscribe发布订阅模式 定义对象间一种一对多的依赖关系,使得当一个对象改变状态时,所有依赖他的对象都能获得通知并被自动更新 2类 ...

  6. IOS-下载动画

    就2小时教会你抽丝剥茧CAAnimation核心动画之精美的下载动画 header 设计灵感 设计此效果的作者 Nick; images 开始之前你需要了解的 先上一张CAAnimation层次图: ...

  7. json与api- 天气api 博客词频分析

    一.json基础 1.1 json的介绍 json现在成为各种程序与语言之间交互的一种数据格式,本质是文本,字符串. json有两种格式: 1.  类似字典  {k:v,k,v} 2.  类似列表 { ...

  8. [Web UI]对比Angular/jQueryUI/Extjs:没有一个框架是万能的

    Angular不能做什么?对比Angular/jQueryUI/Extjs 框架就好比兵器,你得明白你手里拿的是屠龙刀还是倚天剑,刀法主要是砍,剑法主要是刺.对于那些职业喷子和脑残粉,小僧送你们两个字 ...

  9. Personal Introduction

    专业:计算机科学与技术 我是博客园的新人,虽然接触编程世界只有一年,基础知识比较差,编程能力差,但对于这个专业,我还是充满兴趣,希望有一天能独当一面,从今天起,我将分享一些在学习web前端和其他方面的 ...

  10. eureka-3-常用注解

    @EnableDiscoveryClient @EnableEurekaClient 上面两个注解都是用在应用的启动类上面,声明这是一个Eureka Client ,现在说明两个注解的区别. spri ...