HDU 5876 Sparse Graph(补图最短路)

Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2746    Accepted Submission(s): 942


 

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 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

题目大意:给你一个图和一个起点,要你求出这个图的补图中起点与各个点的最短路

首先把补图求出来是不现实的,注意到给出的边很少,所以从边来做文章,从起点开始,先用一个set存下所有的点,对于每一个邻接点,将它从set中移除,因为它在补图是不存在这条边的,再加一个set把这个点存进去,放止出现松弛不到它的情况,然后跑一边bfs就可以了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
using namespace std;
const int maxn=2e5+7;
const int maxm=2e4+7;
const int inf=0x3f3f3f3f;
int n,m;
struct Node
{
	int to;
	int next;
}edge[maxm<<1];
int cnt;
int source;
int head[maxn];
int dis[maxn];
void init()
{
	memset(head,-1,sizeof(head));
	cnt=0;
	return;
}
void add(int u,int v)
{
	edge[cnt].to=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
	return;
}
void bfs()
{
	queue<int> que;
	set<int> se;
	set<int> sb;
	set<int>::iterator it;
	memset(dis,inf,sizeof(dis));
	for(int i=1;i<=n;i++)
	{
		if(i==source) continue;
		se.insert(i);
	}
	dis[source]=0;
	que.push(source);
	while(!que.empty())
	{
		int node=que.front();
		que.pop();
		for(int i=head[node];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(!se.count(v))
			{
                continue;
			}
			else
			{
				se.erase(v);
				sb.insert(v);
			}
		}
		for(it=se.begin();it!=se.end();++it)
		{
			dis[*it]=dis[node]+1;
			que.push(*it);
		}
		se.clear();
		se.swap(sb);
	}
	return;
}
int main()
{
	//freopen("in.txt","r",stdin);
	int test;
	scanf("%d",&test);
	while(test--)
	{
		init();
		scanf("%d%d",&n,&m);
		for(int i=0;i<m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			add(u,v);
			add(v,u);
		}
		scanf("%d",&source);
		bfs();
		bool flag=true;
		for(int i=1;i<=n;i++)
		{
			if(i==source) continue;
			if(!flag) putchar(' ');
			if(dis[i]!=inf) printf("%d",dis[i]);
			else printf("-1");
			if(flag) flag=false;
		}
		puts("");
	}
	return 0;
}
/*
1
5 4
1 2
1 3
2 4
4 5
1
*/

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/81325490