题目链接: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)的更多相关文章

  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)

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

  3. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  4. hdu 5876 Sparse Graph 无权图bfs求最短路

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

  5. hdu_5876_Sparse Graph(补图BFS)

    题目链接:hdu_5876_Sparse Graph 附上叉姐的题解: 1009 Sparse Graph [by ftiasch] 题意:n 个点的无向完全图中删除 m 条边,问点 s 到其他点的最 ...

  6. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  7. HDU 5876 Sparse Graph

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

  8. HDU 5876 大连网络赛 Sparse Graph

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

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

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

随机推荐

  1. 图解SQL 2008数据库复制

    为了达到数据及时备份,一般采用完整备份+差异备份即可,或者再加上日志备份,本文介绍使用数据库复制技术同步数据: PS:文章以图片为主,图片更能直观的看出操作步骤和配置方法! 1.首先创建一个测试的数据 ...

  2. CUBRID学习笔记 22 插入数据

    CREATE TABLE auto_tbl(id INT AUTO_INCREMENT, name VARCHAR); 自增长的列可以插入null, 同时一次可以插入多条记录.别的和其他的sql数据库 ...

  3. JAVA运算符和优先级

    1.算术运算符: ++ 和 -- 既可以出现在操作数的左边,也可以出现在右边,但结果是不同,如: ①int a=5: int b=a++: #先把a赋给b,a再自增 ②int a=5: int b=+ ...

  4. check running processes in Ubuntu

    Check processes If you want to see what processes are running use the command ps -ef If you want to ...

  5. C# 超时类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. Codeforces Round #288 (Div. 2) C. Anya and Ghosts 模拟

    C. Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. VS2013常用快捷方式

    [原文出处]http://blog.csdn.net/lushuner/article/details/23688629 1.回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合 ...

  8. iOS - OC NSString 字符串

    前言 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> @interface NSM ...

  9. iOS常用define宏定义

    1. 屏幕宽高及常用尺寸 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)#define SCREEN_HEIGHT ([U ...

  10. [转载] 对象存储(2):OpenStack Swift——概念、架构与规模部署

    原文: http://www.testlab.com.cn/Index/article/id/1085.html#rd?sukey=fc78a68049a14bb228cb2742bdec2b9498 ...