Sparse Graph hdu-5876(求补图的最短路)


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   N1  other vertices.
 

Input
There are multiple test cases. The first line of input is an integer   T(1T<35)  denoting the number of test cases. For each test case, the first line contains two integers   N(2N200000)  and   M(0M20000). The following   M  lines each contains two distinct integers   u,v(1u,vN)  denoting an edge. And   S (1SN)  is given on the last line.
 

Output
For each of   T  test cases, print a single line consisting of   N1  space separated integers, denoting shortest distances of the remaining   N1  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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<queue>
using namespace std;

const int maxm=2e4+100;
const int maxn=2e5+10;
const int inf=0x3f3f3f3f;


/*
思路真的是很强的,在我们的范围之内,但是要是想突破我们的固有思维
一看见最短路,脑子里就是各种模版,那真的是以前,交给我们模版,
到后来我们就没有模板了,怎样可以怎样来,大胆的猜想加上我们的验证
这就是我们要达到的高度,而且是必须达到的高度
*/


struct AE{
    int cnt;
    int head[maxn];

    struct Edge{
        int v,nt;
    }edge[maxm*2];

    void init(){
        this->cnt=0;
        memset(head,-1,sizeof(head));
    }

    void add_edge(int u,int v){
        edge[cnt].nt=head[u];
        edge[cnt].v=v;
        head[u]=cnt++;
    }
}ae;

int dis[maxn];

void bfs(int st,int n){
    queue<int> q;
    set<int> has_edge,no_edge;
    for(int i=1;i<=n;i++){
        dis[i]=inf;
        if(i==st)continue;
        no_edge.insert(i);
    }
    dis[st]=0;
    q.push(st);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=ae.head[u];i!=-1;i=ae.edge[i].nt){
            int v=ae.edge[i].v;
            if(!no_edge.count(v))continue;
            no_edge.erase(v);
            has_edge.insert(v);
        }
        for(set<int>::iterator it=no_edge.begin();it!=no_edge.end();it++){
            dis[*it]=dis[u]+1;
            q.push(*it);
        }
        no_edge=has_edge;
        has_edge.clear();
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        ae.init();
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i=1;i<=m;i++){
            int u,v;
            scanf("%d %d",&u,&v);
            ae.add_edge(u,v);
            ae.add_edge(v,u);
        }
        int st;
        scanf("%d",&st);
        bfs(st,n);
        int flag=0;
        for(int i=1;i<=n;i++){
            if(i==st)continue;
            if(!flag)
                 printf("%d",dis[i]==inf?-1:dis[i]);
            else
                printf(" %d",dis[i]==inf?-1:dis[i]);
            flag++;
        }
        printf("\n");
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/80239696
今日推荐