HDU 5876 Sparse Graph (补图找最短路/BFS)
来源:互联网 发布:cdn域名加速 编辑:程序博客网 时间:2024/04/28 17:02
#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][b]=1; e[b][a]=1; } int s; scanf("%d",&s); bfs(s); for(int i=1;i<=n;i++) e[i].clear(); }}
Sparse Graph
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 not adjacent in G .
Now you are given an undirected graphG 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.
Now you are given an undirected graph
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
12 01
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
说好的多组数据输入呢。。 比赛的时候队长想到的用BFS 直接 找就行了。 啊啊啊 。 但没想到怎样求补图。 太菜了。
0 0
- HDU 5876 Sparse Graph (补图找最短路/BFS)
- HDU 5876 Sparse Graph(补图+BFS最短路)
- Hdu 5876 Sparse Graph bfs 变型最短路
- hdu 5876 Sparse Graph 补图单源最短路
- hdu 5876 Sparse Graph(补图最短路)
- hdu 5876 Sparse Graph 补图最短路
- Hdu 5876 Sparse Graph(补图最短路)
- hdu 5876 Sparse Graph 完全图补图最短路
- HDU 5876Sparse 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】(2016 ACM/ICPC Asia Regional Dalian Online)
- HDU 5876 Sparse Graph (另类BFS -- 补图的最短路)
- HDU 5876 Sparse Graph(补图 最短路 BFS map)
- hdu5876 Sparse Graph(bfs最短路)
- HDU 5876 Sparse Graph(bfs求解补图中的单源最短路)——2016 ACM/ICPC Asia Regional Dalian Online
- MVP模式
- servlet 监听器简单说名
- MFC----使用MFC操作EXCEL文件
- 第一个Java程序示例——Hello World!
- Android中Context详解 ---- 你所不知道的Context
- HDU 5876 Sparse Graph (补图找最短路/BFS)
- 欢迎使用CSDN-markdown编辑器
- 使用xinput_calibrator解决gtk程序无法校准的问题
- 【汇编学习】汇编语言读书笔记(第三章)
- tjut 3577
- HDU 5874 Friends and Enemies(二分图思想)——2016 ACM/ICPC Asia Regional Dalian Online
- opencv实现多图像读取并显示,sprintf_s函数,static_cast
- Bubble Cup 9 - Finals E. 搜索
- Android横竖屏切换