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

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,问s到其他所有点的距离 \\(2\le N\le 200000 \\) \\(0\le M\le 20000\\)
**思路:**因为边权值为1,所以直接BFS,再者就是考虑如何枚举点了,用vis标记的话,时间会不够(TLE了一发),在这里使用set维护点集(多方便)但是要注意把点从集合中去除时,不能直接边遍历边去,而是一遍遍历完一起去掉,因为使用的迭代器地址不会随删除而改变...

/** @Date    : 2016-11-14-17.20

* @Author : Lweleth (SoungEarlf@gmail.com)

* @Link : https://github.com/

* @Version :

*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <utility>

#include <vector>

#include <map>

#include <set>

#include <string>

#include <stack>

#include <queue>

#define pii pair<int , int>

#define MP(x, y) make_pair((x) ,(y))

#define ff first

#define ss second

#define LL long long

#define MMF(x) memset((x),0,sizeof(x))

#define MMI(x) memset((x), INF, sizeof(x))

using namespace std;



const int INF = 0x3f3f3f3f;

const int N = 2e5+20;



map<pii ,bool>mp;

set<int>st;

set<int>::iterator it;

int res[N];

int n, m;

void bfs(int s,int n)

{

queue<int>q, t;



res[s] = 0;

q.push(s);



st.erase(s);

while(!q.empty())

{

int nw = q.front();

//cout << nw;

q.pop();

for(it = st.begin(); it != st.end(); it++)

{

//cout << "~"<< *it << " " ;

if(!mp[MP(nw, *it)])

{

res[*it] = res[nw] + 1;

q.push(*it);

t.push(*it);

//st.erase(*it);//不能直接删会有问题

//if(st.empty())

//break;



}

}

while(!t.empty())

{

int x = t.front();

t.pop();

st.erase(x);

}

//cout << endl;

}



}



int main()

{



int T;

while(~scanf("%d", &T))

{

while(T--)

{

mp.clear();

st.clear();

scanf("%d%d", &n, &m);



for(int i = 1; i <= n; i++)

st.insert(i);



while(m--)

{

int x, y;

scanf("%d%d", &x, &y);

mp[MP(x, y)] = mp[MP(y, x)] = 1;

}



int s;

scanf("%d", &s);

bfs(s, n);

////

int flag = 0;

for(int i = 1; i <= n; i++)

{

if(i != s)

{

if(flag)

printf(" ");

if(res[i] != 0)

printf("%d", res[i]);

else printf("-1");

flag = 1;

}

}

printf("\n");

}







}

return 0;

}

/*

99

6 9

1 3

1 4

2 3

2 4

2 5

3 5

3 6

4 6

5 6

1

*/

HDU 5876 Sparse Graph BFS+set删点的更多相关文章

  1. HDU 5876 Sparse Graph BFS 最短路

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

  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(补图上BFS)

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

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

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

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

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

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

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

  9. HDU 5876 关于补图的bfs

    1.HDU 5876  Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...

随机推荐

  1. lintcode-152-组合

    152-组合 组给出两个整数n和k,返回从1......n中选出的k个数的组合. 样例 例如 n = 4 且 k = 2 返回的解为: [[2,4],[3,4],[2,3],[1,2],[1,3],[ ...

  2. 原生js移动端可拖动进度条插件

    该插件最初的想法来自网上的一篇文章,直达链接:https://www.cnblogs.com/libin-1/p/6220056.html 笔者因为业务需要寻找到这个插件,然后拿来用之,发现各种不方便 ...

  3. 【linux】- nohup 和 &

    &的意思是在后台运行, 什么意思呢? 意思是说,当你在执行 ./a.out & 的时候,即使你用ctrl C,那么a.out照样运行(因为对SIGINT信号免疫).但是要注意,如果你直 ...

  4. wine update错误 "the cache has no package" error when wine update is available

    网址:https://bugs.launchpad.net/pipelight/+bug/1318321/

  5. oracle怎样查询索引的使用情况

    查询用户的索引select index_name,table_name,tablespace_name, index_type,uniqueness , status from dba_indexes ...

  6. html超出不自动换行

    1.使用overflow: hidden把超出的内容进行隐藏: 2.然后使用white-space: nowrap设置内容不换行: 3.最后使用text-overflow: ellipsis设置超出内 ...

  7. Ehcache概念篇

    前言 缓存用于提供性能和减轻数据库负荷,本文在缓存概念的基础上对Ehcache进行叙述,经实践发现3.x版本高可用性不好实现,所以我采用2.x版本. Ehcache是开源.基于缓存标准.基于java的 ...

  8. 【bzoj1672】[USACO2005 Dec]Cleaning Shifts 清理牛棚 dp/线段树

    题目描述 Farmer John's cows, pampered since birth, have reached new heights of fastidiousness. They now ...

  9. 2016 China Final H - Great Cells

    /************************************************************************* > File Name: H.cpp > ...

  10. POJ2187:Beauty Contest——题解

    http://poj.org/problem?id=2187 题目大意:给n个点,求点对最大距离的平方. ———————————————————— 很容易证明最大距离的点对在最大凸包上. 那么就是旋转 ...