Sparse Graph HDU - 5876

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

題意:題目很快就看懂了。。毛估估他大概規定每條邊距離都是1吧=-=。。然後出發點s的距離是0,給你一張圖,問你補圖中s到各個點的距離是多少
思路:一開始想到的求補圖。。太暴力了。。不可取不可取2333看了看別人的用兩個set去維護,一個維護當前這步可以擴展到的,另一個維護還剩下哪些點,然後bfs到底就行了2333 用set還有點小刺激呢

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<map>
#include<set>
using namespace std;

//thanks to pyf ...

#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
typedef pair<int, int> PII;
typedef long long ll;

const int N = 1e6 + 5;

struct Edge
{
    int u, v, next;
} edge[N * 2];
int head[N];
int dis[N];
int tot = 0;
void init()
{
    CLR(dis, 0);
    CLR(head, -1);
    CLR(edge, 0);
    tot = 0;
}
void add_edge(int u, int v)
{
    edge[tot].u = u;
    edge[tot].v = v;
    edge[tot].next = head[u];
    head[u] = tot ++ ;
}

bool bfs(int s, int n)
{
    set<int>ret, temp; //ret 這次還剩下的 temp 作爲副本存放下次擴展的
    int cnt = 1;
    for (int i = 1; i <= n; i++)
        if (i != s)
            ret.insert(i);
    queue<int>q;
    dis[s] = 0;
    set<int>:: iterator it;
    q.push(s);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].v;
            if (!ret.count(v))
                continue;
            ret.erase(v);
            temp.insert(v);
        }
        for (it = ret.begin(); it != ret.end(); it++)
        {
            int v = *it;
            dis[v] = dis[u] + 1;
            cnt ++ ;
            q.push(v);
        }
        ret.swap(temp);
        temp.clear();
    }
    if (cnt == n)
        return true;
    else return false;
}
int main()
{
    int T;
    while (scanf("%d", &T) != EOF)
    {
        while (T--)
        {
            int n, m;
            scanf("%d%d", &n, &m);
            init();
            for (int i = 0; i < m; i++)
            {
                int u, v;
                scanf("%d%d", &u, &v);
                add_edge(u, v);
                add_edge(v, u);
            }
            int s;
            scanf("%d", &s);
            if (bfs(s, n))
            {
                int cnt = 0;
                for (int i = 1; i <= n; i++)
                {
                    if (i == s)
                        continue;
                    printf("%d", dis[i]);
                    cnt++;
                    if (cnt == n - 1)
                        printf("\n");
                    else
                        printf(" ");
                }
            }
            else
                printf("-1\n");
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章