Sparse Graph

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

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
 
Source
 
 
 
解析:补图上的最短路。维护一个set,里面保存尚未访问的结点。BFS的扩展结点u的时候,把与u直接相连的结点排除,得到的set在补图中都与u有一条权为1的边,访问完之后把这些点从set中删除,开始下一次扩展。即在扩展结点u的时候,只扩展与u没有边相连的结点,有边相邻的结点等下一次再扩展。
 
 
 
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
using namespace std; const int INF = 0x3f3f3f3f;
const int MAXN = 2e5+5;
vector<int> e[MAXN];
int dis[MAXN];
int n, m, s; void bfs()
{
set<int> s1; //保存还未被访问过的结点
set<int> s2; //保存与节点u相连的结点
set<int>::iterator it;
for(int i = 1; i <= n; ++i){ //s1初始化为除结点s外的所有结点
if(i != s)
s1.insert(i);
}
queue<int> q;
q.push(s);
dis[s] = 0;
while(!q.empty()){
int u = q.front();
q.pop();
int len = e[u].size();
for(int i = 0; i < len; ++i){
int v = e[u][i];
if(s1.find(v) == s1.end())
continue;
s1.erase(v); //把与结点u之间有边的点从s1排除
s2.insert(v); //并加入到s2
}
for(it = s1.begin(); it != s1.end(); ++it){
q.push(*it);
dis[*it] = dis[u]+1;
}
s1.swap(s2); //s1中的结点已经全部访问,s2中的结点尚未访问,二者交换
s2.clear(); //清空
}
} void solve()
{
memset(dis, INF, sizeof(dis));
for(int i = 1; i <= n; ++i)
e[i].clear();
int u, v;
while(m--){
scanf("%d%d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
scanf("%d", &s);
bfs();
for(int i = 1; i <= n; ++i){
if(i == s)
continue;
if(dis[i] >= INF)
dis[i] = -1;
printf("%d", dis[i]);
if(i != n)
printf(" ");
}
printf("\n");
} int main()
{
int t;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
solve();
}
return 0;
}

  

  

HDU 5876 Sparse Graph的更多相关文章

  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 无权图bfs求最短路

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

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

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

  5. HDU 5876 Sparse Graph(补图上BFS)

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

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

  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. hdoj 2204 Eddy's爱好

    原文链接:http://www.cnblogs.com/DrunBee/archive/2012/09/05/2672546.html 题意:给你一个正整数N,确定在1到N之间有多少个可以表示成M^K ...

  2. poj 3083 Children of the Candy Corn (广搜,模拟,简单)

    题目 靠墙走用 模拟,我写的是靠左走,因为靠右走相当于 靠左走从终点走到起点. 最短路径 用bfs. #define _CRT_SECURE_NO_WARNINGS #include<stdio ...

  3. Sublime Text 3 搭建 React.js 开发环境

    sublime有很强的自定义功能,插件库很庞大,针对新语言插件更新很快,配合使用可以快速搭建适配语言的开发环境. 1. babel 支持ES6, React.js, jsx代码高亮,对 JavaScr ...

  4. 用cxSelect插件补充一下回显过滤项功能

    这个在DJANGO里,最好在过滤之后,让用户知道自己过滤的选择.所以要定位默认值. 1,在HTML文件里显示默认值: <form class="uk-form" name=& ...

  5. 大陆 Google play 开发者注册(2016)

    1:准备一个VPN, 如:  https://vpnso.com   收费的,使用一两年了,还不错,很稳定2:准备一张普通的银行卡或者信用卡就可以了,能正常绑定支付宝就行3:在全球付上面申请一个 虚拟 ...

  6. 【Linux高频命令专题(14)】nl

    概述 nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样,nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 ...

  7. python 时间处理(time和datetime介绍)

    python的有关时间的有哪几种呢?今天我们介绍两个:time和datetime time模块提供各种操作时间的函数 datetime模块定义了下面这几个类: datetime.date:表示日期的类 ...

  8. parent children

    class parent{ protected static int count=0; public parent() { count++; } } public class child extend ...

  9. Android ActionBar标题和渐变背景

    需要在AndroidManifest.xml中设置 android:theme="@style/Theme.AppCompat" 如果提示找不到,请按下图设置: 至于如何引入的方法 ...

  10. Android开发之“点9”

    “点九”是andriod平台的应用软件开发里的一种特殊的图片形式,文件扩展名为:.9.png智能手机中有自动横屏的功能,同一幅界面会在随着手机(或平板电脑)中的方向传感器的参数不同而改变显示的方向,在 ...