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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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");
        }
    }
}

你可能感兴趣的:(图图图图图)