HDU - 5876(100/600)

In graph theory, the complementcomplement of a graph GG is a graph HH on the same vertices such that two distinct vertices of HH are adjacent if and only if they are notnot adjacent in GG.

Now you are given an undirected graph GG of NN nodes and MM bidirectional edges of unitunit length. Consider the complement of GG, i.e., HH. For a given vertex SS on HH, you are required to compute the shortest distances from SS to all N−1N−1 other vertices.
Input
There are multiple test cases. The first line of input is an integer T(1≤T<35)T(1≤T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2≤N≤200000)N(2≤N≤200000) and M(0≤M≤20000)M(0≤M≤20000). The following MM lines each contains two distinct integers u,v(1≤u,v≤N)u,v(1≤u,v≤N) denoting an edge. And S (1≤S≤N)S (1≤S≤N) is given on the last line.
Output
For each of TT test cases, print a single line consisting of N−1N−1 space separated integers, denoting shortest distances of the remaining N−1N−1 vertices from SS (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

这玩意因为最多松弛两次…
完全图嘛最多两个点距离就是2

#include<queue>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<set>
using namespace std;
int daon[200001];
bool bj[200001];
//bool mp[200001][200001];
set<int>mp[200001];
int n, m, q, w, s;
int T;
//int time[1];
void spfa(int x)
{
    memset(bj, 0, sizeof(bj));
    memset(daon, 0, sizeof(daon));
    int sum = 1;
    queue<int> q;
    bj[x] = 1;
    for (int i = 1; i <= n; i++)
    {
        if (!mp[x].count(i) && i != x)
        {
            q.push(i);
            daon[i] = 1;
            bj[i] = 1;
            sum++;
        }
    }
    while (!q.empty())
    {
        int y = q.front();
        q.pop();
        for (int i = 1; i <= n; i++)
        {
            if (!bj[i])
            {
                if (!mp[y].count(i))
                {
                    q.push(i);
                    bj[i] = 1;
                    daon[i] = daon[y] + 1;
                    sum++;
                }
            }
            if (sum == n)
                break;
        }
        if (sum == n)
            break;
    }
}
struct p
{
    int d, b;
    bool operator < (const p&a)const
    {
        return b > a.b;
    }
};
int main()
{
    cin >> T;
    while (T--)
    {
        memset(daon, 0x3f, sizeof(daon));
        memset(bj, 0, sizeof(bj));
        cin >> n >> m;
        for (int a = 1; a <= n; a++)mp[a].clear();
        for (int a = 1; a <= m; a++)
        {
            scanf("%d%d", &q, &w);
            mp[q].insert(w);
            mp[w].insert(q);
        }
        cin >> s;
        spfa(s);
        int jc = 0;
        for (int a = 1; a <= n; a++)
        {
            if (a == s)continue;
            if (!jc)
            {
                printf("%d", daon[a] == 0x3f3f3f3f ? -1 : daon[a]);
                jc = 1;
                continue;
            }
            printf(" %d", daon[a] == 0x3f3f3f3f ? -1 : daon[a]);
        }
        cout << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值