HDU 5876 (大连网赛1009)(BFS + set)
来源:互联网 发布:c语言接口 结构 编辑:程序博客网 时间:2024/05/19 05:04
题目链接: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;}
0 0
- HDU 5876 (大连网赛1009)(BFS + set)
- Sparse Graph(2016大连网赛)(hdu5876)(BFS+数据结构)
- hdu 5877 离散化+树状数组 (2016大连网赛)
- 大连网赛1006 hdu 5873
- HDU 5872 Seats 2016大连网赛
- HDU5875 - 大连网赛1008(单调栈)
- HDU 5874 Friends and Enemies 2016大连网赛
- hdu 1253 胜利大逃亡(BFS)
- HDU 1253 胜利大逃亡(BFS)
- HDU-1253 胜利大逃亡 (BFS)
- hdu 1253 胜利大逃亡(bfs)
- HDU 1253 胜利大逃亡(BFS)
- HDU 1253 胜利大逃亡(BFS)
- hdu 1253 胜利大逃亡(BFS)
- hdu 1253 胜利大逃亡 (BFS)
- HDU 1253 胜利大逃亡 (BFS)
- HDU 1253 胜利大逃亡( BFS )
- HDU 1253 胜利大逃亡(BFS)
- spark core 日志屏蔽
- 亚马逊AWS:正确设置FTP
- IOS三种多线程编程的技术
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- KVC底层实现
- HDU 5876 (大连网赛1009)(BFS + set)
- NSDateFormatter的格式
- 决策树-信息论
- cocos2d-x 3.x lua的api与C++的关系
- 计算2到10万有多少个素数(质数)
- JAVA环境变量Path、ClassPath、JAVA_HOME的作用
- Commons- BeanUtils学习笔记
- 比Picasso更强大的框架Glide
- 重构 改善既有代码的设计——在对象之间搬移特性