HDU 5876 Sparse Graph (补图找最短路/BFS)
2016-09-12 19:35
633 查看
#include<map> #include<queue> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int maxn=1e6; int head[maxn],le,n,m; int dis[maxn]; int step[maxn]; map<int,int>e[maxn]; void bfs(int s) { queue<int>Q; Q.push(s); dis[s]=1; step[s]=0; int sum=1; while(!Q.empty()) { int now=Q.front(); Q.pop(); for(int i=1; i<=n ; i++) { if(e[now].count(i)==0) { if(dis[i]==0) { sum++; Q.push(i); dis[i]=1; step[i]=step[now]+1; } } if(sum==n) break; } if(sum==n) break; } for(int i=1; i<=n; i++) { if(s==i) continue; else if(i!=n) printf("%d ",step[i]); else printf("%d",step[i]); } printf("\n"); } int main() { int t; scanf("%d",&t); while(t--) { le=1; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); memset(dis,0,sizeof(dis)); for(int i=0; i<m; i++) { int a,b; scanf("%d%d",&a,&b); e[a]=1; e[b][a]=1; } int s; scanf("%d",&s); bfs(s); for(int i=1;i<=n;i++) e[i].clear(); } }
Sparse Graph[b]Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1183 Accepted Submission(s): 419 Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are notadjacent in G. 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. Input There are multiple test cases. The first line of input is an integer T(1≤T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2≤N≤200000) and M(0≤M≤20000). The following M lines each contains two distinct integers u,v(1≤u,v≤N) denoting an edge. And S (1≤S≤N) is given on the last line. Output For each of T test cases, print a single line consisting of N−1 space separated integers, denoting shortest distances of the remaining N−1 vertices from S (if a vertex cannot be reached from S, output ``-1" (without quotes) instead) in ascending order of vertex number. Sample Input 1 2 0 1 Sample Output 1 Source 2016 ACM/ICPC Asia Regional Dalian Online Recommend wange2014 | We have carefully selected several similar problems for you: 5877 5875 5874 5873 5872 Statistic | Submit | Discuss | Note | ||
|
相关文章推荐
- HDU 5876 Sparse Graph(bfs求解补图中的单源最短路)——2016 ACM/ICPC Asia Regional Dalian Online
- HDU 5876 Sparse Graph (另类BFS -- 补图的最短路)
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5876 Sparse Graph(补图+BFS最短路)
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5876 Sparse Graph(补图 最短路 BFS map)
- Hdu 5876 Sparse Graph bfs 变型最短路
- HDU 5876 Sparse Graph(bfs求解补图中的单源最短路)——2016 ACM/ICPC Asia Regional Dalian Online
- hdu 5876 Sparse Graph 无权图bfs求最短路
- HDU 5876 Sparse Graph(补图的最短路)
- 【2016-大连赛区网络赛-I】补图最短路(Sparse Graph,hdu 5876)
- hdu_5876_Sparse Graph(补图BFS)
- HDU 5876 Sparse Graph (补图BFS) 2016 ACM/ICPC Asia Regional Dalian Online
- hdu 5876 Sparse Graph(补图最短路)
- hdu 5876 Sparse Graph 完全图补图最短路
- hdu 5876 - Sparse Graph(2016大连网络赛) bfs
- hdu5876——Sparse Graph(补图的最短路)
- HDU 5876 Sparse Graph 补图BFS
- HDU 5876 Sparse Graph(补图中求最短路)
- HDU 5876 Sparse Graph(bfs)