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. springMVC数据回显

    1.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

  2. Global.asax详解

    在网上找了N多相关的东西总说的不够细,现在终于找到了.可以了解web.cofig和Global.asax之间的关系以及执行的顺序. 在Global.asax.cs文件中 protected void ...

  3. Kubernetes容器运行时(CRI)简介

    Kubernetes节点的底层由一个叫做“容器运行时”的软件进行支撑,它负责比如启停容器这样的事情.最广为人知的容器运行时当属Docker,但它不是唯一的.事实上,容器运行时这个领域发展迅速.为了使K ...

  4. springmvc+rest整合redis

    最近在做一个项目需要用到关系数据库mysql和缓存redis,以及非关系型数据库mongoDB.昨天下午到今天上午一直在搞springmvc整合redis,期间出现的错误一直让人抓狂,在网上搜索的结果 ...

  5. Oracle中 如何用一个表的数据更新另一个表中的数据

    准备阶段 1.建表语句: create table table1( idd varchar2(10) , val varchar2(20) ); create table table2( idd va ...

  6. ctci1.8

    bool isSub(string str0, string str1){     if(str0.length() != str1.length())         return false;   ...

  7. 20165332 2017-2018-2《Java程序设计》课程总结

    20165332 2017-2018-2<Java程序设计>课程总结 一.每周作业及实验报告链接汇总 我期望的师生关系 学习基础和c语言基础调查 Linux安装及命令入门 第一周学习总结 ...

  8. cf 833 A 数论

    A. The Meaningless Game time limit per test 1 second memory limit per test 256 megabytes input stand ...

  9. Android面试二之Fragment

    基本概念 Fragment,简称碎片,是Android 3.0(API 11)提出的,为了兼容低版本,support-v4库中也开发了一套Fragment API,最低兼容Android 1.6. F ...

  10. 【待填坑】ajax问题

    原生xhr怎么写? 怎么处理回调? 问:http状态码常见有哪些? 问:302是啥?304是啥?什么时候会返回304?你刚刚说浏览器缓存,具体缓存机制是怎么样的? 问:你刚刚说的是发起一个get请求, ...