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 ...
随机推荐
- CUBRID学习笔记 4 端口和win7下连接数据库cubrid教程
都是官方的文档 ,水平有限, 大家可以看原文. http://www.cubrid.org/wiki_tutorials/entry/connecting-to-a-remote-cubrid-dat ...
- 1.mybatis简介
mybatis简介 MyBatis 是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为My ...
- 【网摘】DICOM 基础简介
一 什么是DICOM?DICOM是Digital Imaging and Communication of Medicine的缩写,是美国放射学会(American College of Radiol ...
- html bootstrap 表头固定在顶部,表列 可以自由滚动的效果
该效果主要是依照 bootstrap 的一个样式,class="navbar-fixed-top"; 参考网址为:http://v3.bootcss.com/components/ ...
- Thinkphp 3.2 添加 验证码 如何添加。
1,在home模块indexController.class.php中,加入以下代码 <?php namespace Home\Controller; use Think\Controller; ...
- maven概念
1. 下载并解压Maven:Maven下载页2. 将环境变量M2_HOME设置为解压后的目录: 3. 将M2环境变量设置为M2_HOME/bin(在Windows上是%M2_HOME%/bin,在U ...
- 一切都是对象 Thinking in Java 第二章
2.1 用引用操作对象 1.对象和引用联系起来,就可以通过引用来操作对象: 2.引用亦可单独存在,即没有和任何对象联系起来:对没有和对象联系起来的引用操作,会报错: 2.2 必须由你创建所有对象 1. ...
- [转载] what's goole mock
原文: https://code.google.com/p/googlemock/wiki/V1_7_ForDummies 地址被墙了, 看起来费劲, 转载一份 Google C++ Mocking ...
- NTT【51nod】1514 美妙的序列
题意:1~n 的全排列中,有多少个排列满足任意从中间切成两段后,左边段的最大值大于右边段的最小值? 例如:n为3时有3种 2 3 1 3 1 2 3 2 1 解释:比如 2 3 1 (2) (3 1) ...
- 搭建LNMP环境
下载软件包 百度云地址下载地址:http://pan.baidu.com/s/1eSfWNoY 一共有17个包 [root@localhost lnmp]# ls /usr/local/src/lnm ...