hdu Sparse Graph(补集)
来源:互联网 发布:怎么样禁止软件联网 编辑:程序博客网 时间:2024/05/17 03:31
Sparse Graph
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2487 Accepted Submission(s): 386
Now you are given an undirected graph G of N nodes and M bidirectional edges of unit length. Consider the complement of G, i.e., H. For a given vertex S on H , you are required to compute the shortest distances from S to all N-1 other vertices.
12 01
1
这题是要求补集,创新度很大,,,容易超时,,,
#include<cstdio>#include<iostream>#include<cstring>#include<queue>#include<algorithm>#include<cmath>#include<map>#include<set>#include<vector>using namespace std;const int N = 4000010;int dis[N], visit[N], head[N];vector<int>G[N];const int inf = 2e6+7;typedef long long LL;void add(int u, int v);int cnt;struct node{ int next, to;}p1[N];int main(){ int t; scanf("%d", &t); while(t--) { int n, m; scanf("%d %d", &n, &m); memset(head,-1,sizeof(head)); cnt=0; for(int i=0;i<m;i++) { int u, v; scanf("%d %d", &u, &v); add(u,v); add(v,u); } int s; scanf("%d", &s); for(int i=1;i<=n;i++) { dis[i]=inf; } dis[s]=0; set<int>q1, q2; set<int>::iterator it; q1.clear(), q2.clear(); for(int i=1;i<=n;i++) { if(i!=s) { q1.insert(i); } } queue<int>q; while(!q.empty()) { q.pop(); } q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=p1[i].next) { int v=p1[i].to; if(!q1.count(v)) continue; q1.erase(v); q2.insert(v); } for(it=q1.begin();it!=q1.end();it++) { dis[*it]=dis[u]+1; q.push(*it); } q1.swap(q2),q2.clear(); } for(int i=1;i<=n;i++) { if(i!=s) { printf("%d%c",dis[i]==inf?-1:dis[i]," \n"[i==n]); } } } return 0;}void add(int u, int v){ p1[cnt].to=v; p1[cnt].next=head[u]; head[u]=cnt++; return ;}
- hdu Sparse Graph(补集)
- HDU 5876 Sparse Graph(补图+BFS最短路)
- HDU 5876 Sparse Graph 补图BFS
- HDU 5876 Sparse Graph (补图BFS+(链表||set))
- HDU 5876 Sparse Graph (补图BFS) 2016 ACM/ICPC Asia Regional Dalian Online
- HDU 5876 Sparse Graph (另类BFS -- 补图的最短路)
- HDU 5876 Sparse Graph(补图 最短路 BFS map)
- hdu 5876 Sparse Graph 补图的最短路
- HDU 5876 Sparse Graph【补图BFS+set】
- HDU 5876 Sparse Graph(补图的最短路)
- hdu 5876 Sparse Graph(BFS+并查集)
- HDU 5876Sparse Graph (补图上BFS)
- HDU-5876-Sparse Graph(BFS)
- hdu 5876 Sparse Graph(补图最短路)
- HDU 5876 Sparse Graph(bfs)
- Hdu 5876 Sparse Graph(补图最短路)
- HDU 5876 Sparse Graph
- hdu-5876-Sparse Graph
- MySQL数据类型
- Struts2_04_路径问题
- ucos学习2
- 基本类型与字符串之间的转换
- POJ 2385 Apple Catching
- hdu Sparse Graph(补集)
- 第六周项目2-带武器的游戏角色
- android view绘制源码分析
- wamp环境二次配置
- C++使用widnows.h创建带参数线程
- 总结
- C++开源库大全
- Kali2016终端字符串重叠解问题解决
- OC-UITableView&UITableViewController&UITableViewCell&单元格重用