HDU 5876Sparse Graph (补图上BFS)

Sparse Graph
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 524    Accepted Submission(s): 172


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


 


Source

 2016 ACM/ICPC Asia Regional Dalian Online 

题意:给出n个点的无向图,M条边,求补图中S点到各点的距离。

在比赛中感觉这题是能做的,想到了用set存边。就是在求距离时,出现了问题, ,,后来看别人CE代码(有时会有奇效)看到用BFS的,可惜BFS写的不好。。

题解: 用set存边,注意补图中,set中!count的才是有边的(补图!!!),然后BFS,最后注意清空set

代码:

#include <cstdio>
#include<iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <string.h>
#define LL long long
using namespace std;
const int N=200010;
set<int> s[N];
int n,m,x,y,qi,t;
int vis[N],dis[N];
void bfs(int x)
{
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    int sum=1;
    queue<int> q;
    vis[x]=1;
    for(int i=1; i<=n; i++)
    {
        if(!s[x].count(i)&&i!=x)
        {
            q.push(i);
            dis[i]=1;
            vis[i]=1;
            sum++;
        }
    }
    while(!q.empty())
    {
        int y=q.front();
        q.pop();
        for(int i=1; i<=n; i++)
        {
            if(!vis[i])
            {
                if(!s[y].count(i))
                {
                    q.push(i);
                    vis[i]=1;
                    dis[i]=dis[y]+1;
                    sum++;
                }
            }
            if(sum==n)
                break;
        }
        if(sum==n)
            break;
    }
}
int main()
{
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d%d",&n,&m);
            while(m--)
            {
                scanf("%d%d",&x,&y);
                if(!s[x].count(y))
                    s[x].insert(y);
                if(!s[y].count(x))
                    s[y].insert(x);
            }
            scanf("%d",&qi);
            bfs(qi);
            int flag=0;
            for(int i=1;i<=n;i++)
            {
                 if(i==qi)
                    continue;
                 if(!flag)
                 {
                     flag=1;
                     printf("%d",dis[i]==0?-1:dis[i]);
                 }
                 else
                    printf(" %d",dis[i]==0?-1:dis[i]);
            }
            printf("\n");
            for(int i=1;i<=n;i++)
                s[i].clear();
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值