HDU

来源:互联网 发布:yum安装ruby最新版本 编辑:程序博客网 时间:2024/06/05 17:40

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;    }}
原创粉丝点击