hdu5876 Sparse Graph(补图最短路 bfs)
题目链接:hdu5876 Sparse Graph
详见代码。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
using namespace std;
const int N = ;
const int inf = 0x3f3f3f3f;
int n, m;
vector<vector<int> >g;
int d[N];
void bfs(int s){
int u,v,i,j;
queue<int>q;
set<int>a; //不邻接的点
set<int>b; //未扩展的点
set<int>::iterator it;
for(i = ; i <= n; ++i)
a.insert(i);
a.erase(s);
q.push(s);
while(!q.empty()){
u=q.front();
q.pop();
for(j=;j<g[u].size();++j){
v=g[u][j];
if(!a.count(v))
continue;
b.insert(v);
a.erase(v);
}
for(it = a.begin(); it != a.end(); it++){
d[*it] = d[u] + ;
q.push(*it);
}
a.swap(b);
b.clear();
}
}
int main(){
int t, i, j, x, y, s, f;
scanf("%d", &t);
while(t--){
scanf("%d %d", &n, &m);
memset(d, inf, sizeof(d));
g.clear();
g.resize(N+);
for(i = ; i < m; ++i){
scanf("%d %d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
scanf("%d", &s);
d[s] = ;
bfs(s);
f = ;
for(i = ; i <= n; ++i){
if(i == s)continue;
if(d[i] == inf)
printf("-1\n");
else if(!f){
printf("%d", d[i]);
f = ;
}
else
printf(" %d",d[i]);
}
printf("\n");
}
return ;
}
hdu5876 Sparse Graph(补图最短路 bfs)的更多相关文章
- 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 ...
- HDU 5876 Sparse Graph(补图上BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- hdu 5876 Sparse Graph 无权图bfs求最短路
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- hdu_5876_Sparse Graph(补图BFS)
题目链接:hdu_5876_Sparse Graph 附上叉姐的题解: 1009 Sparse Graph [by ftiasch] 题意:n 个点的无向完全图中删除 m 条边,问点 s 到其他点的最 ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- HDU 5876 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5876 大连网络赛 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) T ...
- SCU 4444 Travel (补图最短路)
Travel The country frog lives in has \(n\) towns which are conveniently numbered by \(1, 2, \dots, n ...
随机推荐
- mysql以ROOT权限提权方法
今天feng问了一个问题,mysql root权限运行,直接root服务器吧,SSH登录 正好上网查一下相关的资料: mysql .x里面引入了一个system函数,这个函数可以执行系统命令,当mys ...
- 1,linux基础知识以及新建虚拟机
1,Windows图形界面只是在个人电脑上普及(dos界面):lunix是在服务器领域,服务器上绝对不出现图形化界面(命令行). 2,2.6.18 主板本,次版本,末版本 如果末版本积累到一定程序就 ...
- Codeforces Round #249 (Div. 2)B(贪心法)
B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- .htaccess文件 301重定向URL重写[NC][R][F][L]是什么意思
.htaccess中的[NC][R][F][L]几个标记是什么意思 NC: no case,就是说不区分大小写 R:redirect,重定向 F:forbidden,禁止访问 L:last,表示已经是 ...
- iOS - OC RunTime 运行时
1.运行时的使用 向分类中添加属性 // 包含运行时头文件 #import <objc/runtime.h> /* void objc_setAssociatedObject(id obj ...
- 学习日记day7:代码结构规范
1:绝对定位不是随便用的. 2:一定要用相对定位控制文档流,在相对定位里面使用绝对定位控制具体的位置. 3:代码结构尽量简化. 不要加不必要的span: 不要加不必要的类: 4:控制字体样式的类尽量写 ...
- ASP.NET MVC HtmlHelper用法集锦
ASP.NET MVC HtmlHelper用法集锦 在写一个编辑数据的页面时,我们通常会写如下代码 1:<inputtype="text"value='<%=View ...
- 网络粘贴---Xcode中可用到的快捷键
快捷键: 1.StoryBoard技巧 当你想直接在view中选择自己想要的元素时,但是又碍于一个view上叠加的元素太多很难直接选中,那么在这时,你同时按住键盘上的shift和 control键,然 ...
- 转:Jeff Atwood倾情推荐——程序员必读之书
Jeff Atwood倾情推荐——程序员必读之书 英文版:<Code Complete 2>中文版:<代码大全(第二版)>作者:Steve McConnell译者:金戈 汤凌 ...
- Android控件之CheckBox(复选框控件)
一.有两种状态: 选中状态(true).未选中状态(false) 二.属性 android:id = "@+id/checkbox" android:layout_width=&q ...