您的位置:首页 > 其它

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

Home | TopHangzhou Dianzi University Online Judge 3.0
说好的多组数据输入呢。。   比赛的时候队长想到的用BFS 直接 找就行了。   啊啊啊 。 但没想到怎样求补图。  太菜了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: