HDU 5876 Sparse Graph (补图找最短路/BFS)

来源:互联网 发布:cdn域名加速 编辑:程序博客网 时间:2024/04/28 17:02
#include<map>#include<queue>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn=1e6;int head[maxn],le,n,m;int dis[maxn];int step[maxn];map<int,int>e[maxn];void bfs(int s){    queue<int>Q;    Q.push(s);    dis[s]=1;    step[s]=0;    int sum=1;    while(!Q.empty())    {        int now=Q.front();        Q.pop();        for(int i=1; i<=n ; i++)        {            if(e[now].count(i)==0)            {                if(dis[i]==0)                {                    sum++;                    Q.push(i);                    dis[i]=1;                    step[i]=step[now]+1;                }            }            if(sum==n)  break;        }        if(sum==n)  break;    }    for(int i=1; i<=n; i++)    {        if(s==i)    continue;        else if(i!=n)   printf("%d ",step[i]);        else printf("%d",step[i]);    }    printf("\n");}int main(){    int t;    scanf("%d",&t);    while(t--)    {        le=1;        memset(head,-1,sizeof(head));        scanf("%d%d",&n,&m);        memset(dis,0,sizeof(dis));        for(int i=0; i<m; i++)        {            int a,b;    scanf("%d%d",&a,&b);            e[a][b]=1;  e[b][a]=1;        }        int s;        scanf("%d",&s);        bfs(s);        for(int i=1;i<=n;i++)   e[i].clear();    }}

  


Sparse Graph

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


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
12 01
 

Sample Output
1
 

Source
2016 ACM/ICPC Asia Regional Dalian Online
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5877 5875 5874 5873 5872 
 

Statistic | Submit | Discuss | Note
Home | TopHangzhou Dianzi University Online Judge 3.0


说好的多组数据输入呢。。   比赛的时候队长想到的用BFS 直接 找就行了。   啊啊啊 。 但没想到怎样求补图。  太菜了。

0 0