题目链接: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. (2) 深入理解Java Class文件格式(一)

    好文转载:http://blog.csdn.net/zhangjg_blog/article/details/21486985 Class文件在Java体系结构中的位置和作用 在上一篇博客中, 大致讲 ...

  2. 【转】对硬盘进行分区时,GPT和MBR区别。

    在Windows 8或8.1中设置新磁盘时,系统会询问你是想要使用MBR还是GPT分区.GPT是一种新的标准,并在逐渐取代MBR. GPT带来了很多新特性,但MBR仍然拥有最好的兼容性.GPT并不是W ...

  3. php无限遍历目录-修正版

    最近在能php目录操作,搞了一个目录无限遍历: 使用的函数有: isset()判断某个变量是否定义 chdir() 将当前目录改变为指定的目录. opendir() 打开目录. readdir()读取 ...

  4. 阻抗计算(用SI9000如何计算微带线)

    用SI9000如何计算微带线                一.几个概念: 阻抗的定义:在某一频率下,电子器件传输信号线中,相对某一参考层,其高频信号或电磁波在传播过程中所受的阻力称之为特性阻抗,它是 ...

  5. RPM安装rabbitMQ

    系统使用的是centos 7 - minimal 建立用户和组: # groupadd rabbitmq # useradd rabbitmq -g rabbitmq 在安装rabbitMQ之前需要先 ...

  6. python_way ,day25 CMDB_models (数据库设计)

    from django.db import models from django.contrib.auth.models import User # Create your models here. ...

  7. Nginx 简介

    一.介绍 Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器. Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件代理服务器,并在一个BSD ...

  8. js之oop <二> 对象属性

    js中对象属性可以动态添加和删除.删除对象属性用delete关键字. function obj(){ } var oo = new obj(); oo.a = "a"; oo.b ...

  9. Greenplum 在Linux下的安装

    1.实验环境 1.1.硬件环境 Oracle VM VirtualBox虚拟机软件:三台Linux虚拟机:Centos 6.5:数据库:greenplum-db-4.3.9.1-build-1-rhe ...

  10. Python中的logging模块

    http://python.jobbole.com/86887/ 最近修改了项目里的logging相关功能,用到了python标准库里的logging模块,在此做一些记录.主要是从官方文档和stack ...