HDU 5876 Sparse Graph

Posted 月夜下

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5876 Sparse Graph相关的知识,希望对你有一定的参考价值。

Sparse Graph

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


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
 

 

Source
 
 
 
解析:补图上的BFS。
 
 
 
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 2e5+5;
vector<int> e[MAXN];
int dis[MAXN];
int n, m, s;

void bfs()
{
    set<int> s1, s2;
    set<int>::iterator it;
    for(int i = 1; i <= n; ++i){
        if(i != s)
            s1.insert(i);
    }
    queue<int> q;
    q.push(s);
    dis[s] = 0;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        int len = e[u].size();
        for(int i = 0; i < len; ++i){
            int v = e[u][i];
            if(s1.find(v) == s1.end())
                continue;
            s1.erase(v);
            s2.insert(v);
        }
        for(it = s1.begin(); it != s1.end(); ++it){
            q.push(*it);
            dis[*it] = dis[u]+1;
        }
        s1.swap(s2);
        s2.clear();
    }
}

void solve()
{
    memset(dis, INF, sizeof(dis));
    for(int i = 1; i <= n; ++i)
        e[i].clear();
    int u, v;
    while(m--){
        scanf("%d%d", &u, &v);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    scanf("%d", &s);
    bfs();
    for(int i = 1; i <= n; ++i){
        if(i == s)
            continue;
        if(dis[i] >= INF)
            dis[i] = -1;
        printf("%d", dis[i]);
        if(i != n)
            printf(" ");
    }
    printf("\n");
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &m);
        solve();
    }
    return 0;
}

  

以上是关于HDU 5876 Sparse Graph的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5876 Sparse Graph

hdu5876 Sparse Graph(补图最短路)

HDU 5876 Sparse Graph BFS 最短路

hdu 5876 Sparse Graph 无权图bfs求最短路

HDU 5876 Sparse Graph(补图上BFS)

Sparse Graph---hdu5876(set+bfs)