Sparse Graph
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1192 Accepted Submission(s): 420
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.
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
【题意】给你一个n个结点,m条边的无向图G,再给你G的补图H上的一个点S,要求出在补图H上,点S到其他n-1个结点的最短路
【解题方法】某一年四川省赛原题了。做法是:由于起点S是给出的,我们可以从点S开始,进行广搜,对于点u和点v,若原图中,两点直接相连,那么补图中的这两点必定没有直接相连,但如果我们对每一点都要判断一遍有哪些点在原图中没有直接相连,这个复杂度还是很高的,所以我们可以采用std::set维护还没BFS过的点,当要扩展点u的时候,遍历一次还没访问过的点v,如果u和v之间没边,那么将v入队;否则将v留在未扩展点中。
【时间复杂度】O(n+m)
【AC 代码】
【解题方法】某一年四川省赛原题了。做法是:由于起点S是给出的,我们可以从点S开始,进行广搜,对于点u和点v,若原图中,两点直接相连,那么补图中的这两点必定没有直接相连,但如果我们对每一点都要判断一遍有哪些点在原图中没有直接相连,这个复杂度还是很高的,所以我们可以采用std::set维护还没BFS过的点,当要扩展点u的时候,遍历一次还没访问过的点v,如果u和v之间没边,那么将v入队;否则将v留在未扩展点中。
【时间复杂度】O(n+m)
【AC 代码】
//
//Created by just_sort 2016/9/12 16:27
//Copyright (c) 2016 just_sort.All Rights Reserved
//
#include <set>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 200010;
int head[maxn],ans[maxn],edgecnt,n,m;
struct edge{
int v,next;
}E[maxn*2];
void init(){
edgecnt=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v){
E[edgecnt].v=v,E[edgecnt].next=head[u],head[u]=edgecnt++;
}
void bfs(int s)
{
queue<int>que;
ans[s]=0;
que.push(s);
set<int>s1,s2;//s1未被访问
for(int i=1; i<=n; i++){
if(i!=s) s1.insert(i);
}
while(!que.empty()){
int u=que.front();
que.pop();
for(int i=head[u]; ~i; i=E[i].next){
int v=E[i].v;
if(!s1.count(v)) continue;
s1.erase(v);
s2.insert(v);
}
for(auto it=s1.begin(); it!=s1.end(); it++){
que.push(*it);
ans[*it]=ans[u]+1;
}
s1.swap(s2);
s2.clear();
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d",&n,&m);
for(int i=1; i<=m; i++){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
int s;
scanf("%d",&s);
bfs(s);
for(int i=1; i<n; i++){
if(i!=s) printf("%d ",ans[i]);
}
printf("%d\n",ans[n]);
}
return 0;
}