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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值