Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1563    Accepted Submission(s): 549

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
 
思路:边的长度均为1,用bfs。遍历补图中与u相连接的结点v,并将其在全部结点的集合中删除。删除结点用set较快。
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const int MAXN = ;
int n, m, s;
set<int> arc[MAXN];
int d[MAXN];
void bfs(int src)
{
memset(d, , sizeof(d));
set<int> vec;
for(int i = ; i <= n; i++)
{
vec.insert(i);
}
queue<int> que;
que.push(src);
vec.erase(src);
while(!que.empty())
{
int u = que.front(); que.pop();
for(set<int>:: iterator it = vec.begin(); it != vec.end(); it++)
{
int v = *it;
if(arc[u].find(v) == arc[u].end())
{
que.push(v);
d[v] = d[u] + ;
vec.erase(v);
}
}
if(vec.empty())
{
break;
}
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++) arc[i].clear();
for(int i = ; i < m; i++)
{
int u, v;
scanf("%d %d", &u, &v);
arc[u].insert(v);
arc[v].insert(u);
}
scanf("%d", &s);
bfs(s);
for(int i = ; i <= n; i++)
{
if(i == s)
{
continue;
}
else
{
if(d[i] == )
{
printf("-1");
}
else
{
printf("%d", d[i]);
}
}
if(i != n)
{
printf(" ");
}
}
printf("\n");
}
return ;
}

HDOJ5876(补图的最短路)的更多相关文章

  1. HDU - 5876 :Sparse Graph (完全图的补图的最短路 -BFS&set)

    In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinc ...

  2. Sparse Graph---hdu5876(set+bfs+补图求最短路)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5876 题意:有一个含有n个点的无向图,已知图的补图含有m条边u, v:求在原图中,起点s到 ...

  3. HDU 5876 补图 单源 最短路

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

  4. 图论 - Travel

    Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...

  5. 【ZJOI 2018】线图(树的枚举,hash,dp)

    线图 题目描述 九条可怜是一个热爱出题的女孩子. 今天可怜想要出一道和图论相关的题.在一张无向图 $G$ 上,我们可以对它进行一些非常有趣的变换,比如说对偶,又或者说取补.这样的操作往往可以赋予一些传 ...

  6. BZOJ 1137: [POI2009]Wsp 岛屿 半平面交

    1137: [POI2009]Wsp 岛屿 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 165  Solved: ...

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

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

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

  9. SCU 4444 Travel (补图最短路)

    Travel The country frog lives in has \(n\) towns which are conveniently numbered by \(1, 2, \dots, n ...

随机推荐

  1. idea调节字体大小

    这是调节前的 这是调节后的 步骤

  2. Android中获取屏幕高度和宽度

    有时我们需要获取当前屏幕的高度和宽度,只需要在一个Activity的onCreate()方法中写上如下代码即可: //定义DisplayMetrics 对象 DisplayMetrics metric ...

  3. Spring -- 如何为applicationContext.xml 添加 util 的 *.xsd文件

  4. android视图概述

    android视图概述 一.简介 数据和控件分开的作用: 便于引用 便于修改:修改的时候直接改一次数据就可以了

  5. 51nod-1201-数位dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1201 1201 整数划分 基准时间限制:1 秒 空间限制:131072 ...

  6. mongodb停止遇到shutdownServer failed: unauthorized: this command must run from localhost when running db without auth解决方法

    停止mongodb use admin db.shutdownServer(); mongos> db.shutdownServer(); assert failed : unexpected ...

  7. JavaScript: Where to Insert JavaScript and output

    1.Javescript in <head> <!DOCTYPE html> <html> <head> <script> function ...

  8. react-redux: modal

    1.actionTpye export const INCREMENT = 'INCREMENT'; export const DECREMENT = 'DECREMENT'; export cons ...

  9. js 设置日期函数

    前三十天: var now = new Date(); var prev = now.setDate( now.getDate() - 30 ) vm.sDate = comm.getFormatDa ...

  10. IOS对存放对象的数组排序

    我们开发的每个程序都会使用到一些数据,而这些数据一般被封装在一个自定义的类中.例如一个音乐程序可能会有一个Song类,聊天程序则又一个 Friend类,点菜程序会有一个Recipe类等.有时候我们希望 ...