题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876

题意:给定一个图(n个顶点m条边),求其补图最短路

思路:集合a表示当前还未寻找到的点,集合b表示本次bfs之后仍未寻找到的点

#include<cstdio>
#include<set>
#include<queue>
#include<cstring>
using namespace std;
const int N = 2e5 + 5;
set <int> G[N],a,b;
int ans[N];
void bfs(int s)
{
queue <int> q;
set<int>::iterator it;
q.push(s);
while(!q.empty())
{
int t = q.front();
q.pop();
for(it = G[t].begin() ;it != G[t].end() ;it++)
{
if(a.count(*it))//如果当前还未寻找到
{
a.erase(*it);//该点本次可以寻找到,从a中删去
b.insert(*it);//本次仍不能找到
}
}
for(it = a.begin() ;it != a.end() ;it++)
{
ans[*it] = ans[t] + 1;
q.push(*it);
}
a.swap(b);//b传递给a
b.clear();//...
}
for(it = a.begin() ;it != a.end() ;it++)
ans[*it] = -1;//剩余的点
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,num = 0;
scanf("%d %d",&n,&m);
memset(ans,0,sizeof(ans));
a.clear();
for(int i = 1 ;i <= n ;i++)
G[i].clear();
while(m--)
{
int u,v;
scanf("%d %d",&u,&v);
G[u].insert(v);
G[v].insert(u);
}
int s;
scanf("%d",&s);
for(int i = 1 ;i <= n ;i++)
if(i != s) a.insert(i);
bfs(s);
for(int i = 1 ;i <= n ;i++)
{
if(i != s)
{
num++;
if(num != n-1)
printf("%d ",ans[i]);
else
printf("%d\n",ans[i]);
}
}
}
return 0;
}

HDU 5876 (大连网赛1009)(BFS + set)的更多相关文章

  1. 团 大连网赛 1007 Friends and Enemies

    //大连网赛 1007 Friends and Enemies // 思路:思路很棒! // 转化成最大二分图 // 团:点集的子集是个完全图 // 那么朋友圈可以考虑成一个团,原题就转化成用团去覆盖 ...

  2. HDU 5876:Sparse Graph(BFS)

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

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

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

  4. 2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5869 Problem Description This is a simple probl ...

  5. 2016 大连网赛---Function(单调栈)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5875 Problem Description The shorter, the simpl ...

  6. 2016 大连网赛---Weak Pair(dfs+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...

  7. hdu 4474 大整数取模+bfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...

  8. HDU 5876 大连网络赛 Sparse Graph

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

  9. Hdu 5489 合肥网络赛 1009 Removed Interval

    跳跃式LIS(nlogn),在普通的转移基础上增加一种可以跨越一段距离的转移,用一颗新的树状数组维护,同时,我们还要维护跨越完一次后面的转移,所以我用了3颗树状数组.. 比赛的时候一句话位置写错了,然 ...

随机推荐

  1. noi 666 放苹果

    题目链接:http://noi.openjudge.cn/ch0206/666/ 和ural 1114题意类似,但是有顺序,5,1,1和1,5,1是同一种序列.不能直接枚举 d(i,j) 前 i 个盘 ...

  2. 用jxl解析excel内容

    需要导入jxl.jar 下方表格为excel中内容: 序号 姓名 性别 生日 地址 1 测试1 男 1990-1-1 北京朝阳区 2 测试2 女 1998-2-2 北京海淀 3 测试3 男 1999- ...

  3. Xcode7中,如何新建category分类

    易忘,所以留存: 1, 2, 3, 结果如下: 补充: http://tech.meituan.com/DiveIntoCategory.html

  4. C#:类和结构的区别

    第一.引用类型和值类型 类属于引用类型,而结构属于值类型. 结构在赋值时进行复制. 将结构赋值给新变量时,将复制所有数据,并且对新副本所做的任何修改不会更改原始副本的数据. 第二.继承性 类可以继承类 ...

  5. 来自苹果的编程语言——Swift简介转载】

    关于 这篇文章简要介绍了苹果于WWDC 2014发布的编程语言——Swift. 原文作者: Lucida Blog 新浪微博 豆瓣 转载前请保留出处链接,谢谢. 前言 在这里我认为有必要提一下Brec ...

  6. Hello World for U

    题目描述: Given any ) characters, you are asked to form the characters into the shape of U. For example, ...

  7. 使用Nexus创建Maven私服

    Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问.利用Nexus你可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个Artifact.Nexus ...

  8. git github 异常

    git :版本控制工具 github:项目托管 git clone failed:git是否安装正确 github commit failed:github 是否账号 / 密码是否正确(密码错误也可以 ...

  9. 如何让IE8的菜单栏调到最上方

    如何让IE8的菜单栏调到最上方 运行gpedit.msc,在"用户配置"/"管理模板"/"Windows Components"/" ...

  10. gameui-for-phaser-js更新到最新版本

    Hola Cantk是一个功能强大的游戏引擎,它拥有丰富的GUI控件,配合Hola Studio可以快速开发出游戏的UI界面.Phaser是一个流行而且强大的游戏引擎,但是它的GUI控件很少,而且缺乏 ...