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. Android 相关重难点知识整理

    [原文] 集合 对 HashMap 进行排序: HashMap 本身无序,但其子类 LinkedHashMap 使用链表结构,实现了有序.通过 HashMap#entrySet() 方法可以将 Map ...

  2. SQL编码规范

    1        目的 为了保证所每个项目组编写出的程序都符合相同的规范,便于理解和维护,便于检查.减少出错概率,有助于成员间交流,保证一致性.统一性而建立的SQL程序编码规范. 2        范 ...

  3. 使用VirtualBox安装Android 4.2.2 x86 .

    http://blog.csdn.net/kunoy/article/details/8768205 virtual box 安装 android x86 不显示鼠标, --> 控制 --> ...

  4. phalcon: 独立的映射,字段名名别名

    class ProductsController extends \Phalcon\Mvc\Controller { public function saveAction() { //Obtain t ...

  5. 分享知识-快乐自己:List 集合去重合并 , 多种方法演示

    最近空闲时间去面试 , 被问了一个问题list如何去重合并 , 想了半天只想到了最繁琐的循环方法 , 顿觉丢人. 整理一下资料供大家参考: List<String> a = new Arr ...

  6. Selenium学习笔记(1) - 自动化测试体系与原理

    技术体系 基于代码的测试(Code-Based Testing) 基于协议的测试(Protocol-Based Testing) 基于界面的测试(GUI-Based Testing) 工作原理 基于代 ...

  7. datagrid中用tooltip

    function msgFormat(value,row){ value = value.replace(/ /g," "); return '<span title='+ ...

  8. 【河南第十届省赛-B】情报传递

    题目描述 抗日战争时期,在国共合作的大背景下,中共不断发展壮大,其情报工作也开始由获取警报性.保卫性信息,向获取军政战略性情报转变.各系统情报组织遵循"荫蔽精干,长期埋伏,积蓄力量,以待时机 ...

  9. 面向对象设计原则-SOLID

    SOLID的意思是: Single responsibility principle 单一职责原则 Open/close principle 开放/封闭原则 Liskov substitution p ...

  10. TCP与UDP(实时通讯)

    1.TCP使用 导入AsyncSocket资源文件夹,此文件是arc混编,加入库文件,如下图: #import "ViewController.h" #import "A ...