HDU 5876 - Sparse Graph

54 篇文章 0 订阅
17 篇文章 0 订阅

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
 

题意:给出一个完全图G和一个图H,给出起点,求补图中每个点的最短路,按照顶点序号输出路径,不能到达输出“-1”。

从起点开始BFS,找到到达所有点的最短路径。

#include <cstdio>
#include <iterator>
#include <queue>
#include <set>
using namespace std;

set<int> g[200000 + 5];
set<int> p;
int vis[200000 + 5];

int main()
{
    int T;
    scanf("%d", &T);

    while (T--)
    {
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; ++i)
        {
            p.insert(i);
            g[i].clear();
            vis[i] = -1;
        }
        for (int i = 1; i <= m; ++i)
        {
            int a, b;
            scanf("%d %d", &a, &b);
            g[a].insert(b);
            g[b].insert(a);
        }
        int start;
        scanf("%d", &start);
        queue<int> Q;
        Q.push(start);
        p.erase(start);

        vis[start] = 0;
        while (!Q.empty())
        {
            int temp = Q.front();
            Q.pop();
            vector<int> v;
            set<int>::iterator it;
            for (it = p.begin(); it != p.end(); ++it)
            {
                if (!g[temp].count(*it))
                {
                    vis[*it] = vis[temp] + 1;
                    v.push_back(*it);
                    Q.push(*it);
                }
            }
            int len = v.size();
            for (int i = 0; i < len; ++i)
                p.erase(v[i]);
        }
        bool flag = true;
        for (int i = 1; i <= n; ++i)
        {
            if (i != start)
            {
                if (flag == true)
                {
                    printf("%d", vis[i]);
                    flag = false;
                }
                else
                    printf(" %d", vis[i]);
            }
        }
        printf("\n");
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值