题目链接: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. Mayor's posters(线段树+离散化POJ2528)

    Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...

  2. 深入理解CSS网页布局-理论篇

    在CSS网页开发布局中,需要对浮动和定位有深刻的理解才能在开发中游刃有余. 基于此,在博客园中做了本篇总结,这些总结来自实践经验和阅读一些书籍后的理解总结,主要内容为浮动,清除浮动,定位. (可点击屏 ...

  3. 关于Python中输出中文的一点疑问

    #encoding=gb2312 import urllib import re def getHtml(url): page = urllib.urlopen(url) html = page.re ...

  4. Jqplot 使用总结之一(线条及节点颜色)

    好不容易抽出时间将Jqplot做下最后的总结,下面通过四个例子来学习Jqplot的一些常见技巧:示例1. 设置线条颜色(包括背景色及线条颜色的批量赋值) <!DOCTYPE html> & ...

  5. APP成功上线前的bug解决方案

    首先测试用例设计阶段,设计并维护一个各个功能入口的说明文档.其实这个文档的作用很大,一方面对于bug回归阶段的人来说,这是用于提醒的;另外一个方面,在随机测试的时候,随机程度也能有所提高,测试人员能够 ...

  6. HBase之show table

    import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import or ...

  7. C++类的交叉引用

    对于C++中,两个类中相互引用对方,当然只能是在指针的基础上,于是我们知道.也就是说在A类的有一个指针引用B类的成员函数或成员对象,而B类中又有一个指针来访问A中的成员函数或对象.这就是C++中类的交 ...

  8. python爬虫(1)

    了解python的基本语法后就可以开始了,边学边巩固. 学爬虫那什么是网络爬虫,以下是百度百科的定义:网络爬虫(又被称为网页蜘蛛,网络机器人, 在FOAF社区中间,更经常的称为网页追逐者),是一种按照 ...

  9. CentOS7 安装RabbitMQ

    第一.下载erlang和rabbitmq-server的rpm: http://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos. ...

  10. codeforces 725D . Contest Balloons(贪心+优先队列)

    题目链接:codeforces 725D . Contest Balloons 先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给 ...