HDOJ5876(补图的最短路)

Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1563    Accepted Submission(s): 549


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 notadjacent 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 N1 other vertices.
 

 

Input
There are multiple test cases. The first line of input is an integer T(1T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2N200000) and M(0M20000). The following M lines each contains two distinct integers u,v(1u,vN) denoting an edge. And S (1SN) is given on the last line.
 

 

Output
For each of T test cases, print a single line consisting of N1 space separated integers, denoting shortest distances of the remaining N1 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
 
思路:边的长度均为1,用bfs。遍历补图中与u相连接的结点v,并将其在全部结点的集合中删除。删除结点用set较快。
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const int MAXN = 200005;
int n, m, s;
set<int> arc[MAXN];
int d[MAXN];
void bfs(int src)
{
    memset(d, 0, sizeof(d));
    set<int> vec;
    for(int i = 1; i <= n; i++)
    {
        vec.insert(i);
    }
    queue<int> que;
    que.push(src);
    vec.erase(src);
    while(!que.empty())
    {
        int u = que.front(); que.pop();
        for(set<int>:: iterator it = vec.begin(); it != vec.end(); it++)
        {
            int v = *it;
            if(arc[u].find(v) == arc[u].end())
            {
                que.push(v);
                d[v] = d[u] + 1;
                vec.erase(v);
            }
        }
        if(vec.empty())
        {
            break;
        }
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; i++) arc[i].clear();
        for(int i = 0; i < m; i++)
        {
            int u, v;
            scanf("%d %d", &u, &v);
            arc[u].insert(v);
            arc[v].insert(u);
        }
        scanf("%d", &s);
        bfs(s);
        for(int i = 1; i <= n; i++)
        {
            if(i == s)
            {
                continue;
            }
            else
            {
                if(d[i] == 0)
                {
                    printf("-1");
                }
                else
                {
                    printf("%d", d[i]);
                }
            }
            if(i != n)
            {
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}

 

 

posted on 2016-09-15 09:13  vCoders  阅读(173)  评论(0编辑  收藏  举报

导航