HDU 5876 Sparase Graph(补图上的bfs)
2016-09-11 08:55
267 查看
// // main.cpp // Richard // // Created by 邵金杰 on 16/9/11. // Copyright © 2016年 邵金杰. All rights reserved. // #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<set> using namespace std; const int maxv=200000+10; const int maxn=40000+10; const int inf=99999999; struct edge{ int u,v,next; }edge[maxn*2]; int head[maxv],dist[maxv]; int n,e; int cnt; int s; void add_edge(int u,int v) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].next=head[u]; head[u]=cnt++; } void complement_graph_bfs() { set<int> ta,tb; set<int>::iterator it; for(int i=1;i<=n;i++) dist[i]=inf; for(int i=1;i<=n;i++) if(i!=s) ta.insert(i); dist[s]=0; queue<int> q; q.push(s); while(!q.empty()) { int now=q.front();q.pop(); for(int i=head[now];i!=-1;i=edge[i].next){ if(ta.find(edge[i].v)!=ta.end()){ ta.erase(edge[i].v); tb.insert(edge[i].v); } } for(it=ta.begin();it!=ta.end();it++) {dist[*it]=dist[now]+1;q.push(*it);} ta.swap(tb);tb.clear(); } for(int i=1;i<=n;i++) if(i!=s) {printf("%d%s",dist[i]==inf?-1:dist[i],i<n?" ":"");} printf("\n"); } int main() { int t; scanf("%d",&t); while(t--) { cnt=0; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&e); int u,v; for(int i=0;i<e;i++){ scanf("%d%d",&u,&v); add_edge(u,v); add_edge(v,u); } scanf("%d",&s); complement_graph_bfs(); } return 0; }
相关文章推荐
- hdu 5876 Sparse Graph 无权图bfs求最短路
- hdu 5876 Sparse Graph bfs 链表优化
- HDU 5876 (大连网赛1009)(BFS + set)
- HDU 5876 Sparse Graph (补图BFS+(链表||set))
- 补图BFS(hdu 5876)
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5876 Sparse Graph 补图BFS
- hdu 5876 - Sparse Graph(2016大连网络赛) bfs
- HDU 5876 Sparse Graph(补图上BFS)
- HDU 5876 Sparse Graph(补图+BFS最短路)
- HDU 5876 Sparse Graph(bfs+set)
- HDU 5876 Sparse Graph(bfs)
- HDU 5876 Sparse Graph (补图找最短路/BFS)
- HDU-5876-Sparse Graph(BFS)
- Hdu 5876 Sparse Graph bfs 变型最短路
- HDU 5876 Sparse Graph (补图BFS) 2016 ACM/ICPC Asia Regional Dalian Online
- HDU 5876 Sparse Graph【补图BFS+set】
- hdu_5876_Sparse Graph(补图BFS)
- 补图+BFS( 邻接链表 )——HDU 5876 ( 2016 ACM/ICPC Asia Regional Dalian Online 1009 )
- HDU 5876 Sparse Graph (求补图上最短路径、bfs、优化)