HDU 5876 (大连网赛1009)(BFS + set)
题目链接: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)的更多相关文章
- 团 大连网赛 1007 Friends and Enemies
//大连网赛 1007 Friends and Enemies // 思路:思路很棒! // 转化成最大二分图 // 团:点集的子集是个完全图 // 那么朋友圈可以考虑成一个团,原题就转化成用团去覆盖 ...
- 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(补图上BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...
- 2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5869 Problem Description This is a simple probl ...
- 2016 大连网赛---Function(单调栈)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5875 Problem Description The shorter, the simpl ...
- 2016 大连网赛---Weak Pair(dfs+树状数组)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...
- hdu 4474 大整数取模+bfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...
- HDU 5876 大连网络赛 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) T ...
- Hdu 5489 合肥网络赛 1009 Removed Interval
跳跃式LIS(nlogn),在普通的转移基础上增加一种可以跨越一段距离的转移,用一颗新的树状数组维护,同时,我们还要维护跨越完一次后面的转移,所以我用了3颗树状数组.. 比赛的时候一句话位置写错了,然 ...
随机推荐
- 制作web字体:CSS3 @font-face
@font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体,另外@font- ...
- C#单独启动进程的几种方式
本文实例讲述了C#启动进程的几种常用方法.分享给大家供大家参考.具体如下: 1.启动子进程,不等待子进程结束 private void simpleRun_Click(object sender, S ...
- jsoup: Java HTML Parser (类似jquery)
jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extr ...
- js中的正则表达式
一.正则中的汉字 常见的:/[^\x00-\x7F]+?/ /^[\u2E80-\u9FFF]+$/ 过滤汉字即是:string.replace(/^[\u2E80-\u9FFF]+$/g, &quo ...
- Python之SQLAlchemy学习--外键约束问题
以下问题赞为解决: # class User(Base):# __tablename__ = 'user'# #表的结构# id = Column(String(20), primary_key=Tr ...
- (转)Uiautomator——API详解
原文链接:http://www.cnblogs.com/by-dream/p/4921701.html#3328376 以一个简单的例子开始吧.我们完成一个 " 打开QQ,进入QQ空间,然后 ...
- 如何让popWindow显示在view上方
看了bilibili的客户端搜索按钮,很喜欢大爱!自己也想做个类似的(相似度 10% 哈哈) popWin的出现退出动画也可以自己设定,用过其方法setAnimationStyle(R.style.x ...
- 002_kafka_相关术语详细解析
参考: http://www.cnblogs.com/likehua/p/3999538.html http://kafka.apache.org/documentation.html#getting ...
- Install PaddlePaddle (Parallel Distributed Deep Learning)
Step 1: Install docker on your linux system (My linux is fedora) https://docs.docker.com/engine/inst ...
- 已知GBK的某段码表,码表对应的字符
for i in range(0xA1A2,0xA1A5): ...