hdu Sparse Graph(补集)

来源:互联网 发布:怎么样禁止软件联网 编辑:程序博客网 时间:2024/05/17 03:31

Sparse Graph

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


Problem Description
In graph theory, the complement of a graph is a graph on the same vertices such that two distinct vertices of are adjacent if and only if they are not adjacent in

Now you are given an undirected graph of nodes and bidirectional edges of unit length. Consider the complement of G, i.e., H. For a given vertex on , you are required to compute the shortest distances from to all N-1 other vertices.
 

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

Output
For each of test cases, print a single line consisting of N-1 space separated integers, denoting shortest distances of the remaining N-1 vertices from (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

这题是要求补集,创新度很大,,,容易超时,,,

#include<cstdio>#include<iostream>#include<cstring>#include<queue>#include<algorithm>#include<cmath>#include<map>#include<set>#include<vector>using namespace std;const int N = 4000010;int dis[N], visit[N], head[N];vector<int>G[N];const int inf = 2e6+7;typedef long long LL;void add(int u, int v);int cnt;struct node{    int next, to;}p1[N];int main(){    int t;    scanf("%d", &t);    while(t--)    {        int n, m;        scanf("%d %d", &n, &m);        memset(head,-1,sizeof(head));        cnt=0;        for(int i=0;i<m;i++)        {            int u, v;            scanf("%d %d", &u, &v);            add(u,v);            add(v,u);        }        int s;        scanf("%d", &s);        for(int i=1;i<=n;i++)        {            dis[i]=inf;        }        dis[s]=0;        set<int>q1, q2;        set<int>::iterator it;        q1.clear(), q2.clear();        for(int i=1;i<=n;i++)        {            if(i!=s)            {                q1.insert(i);            }        }        queue<int>q;        while(!q.empty())        {            q.pop();        }        q.push(s);        while(!q.empty())        {            int u=q.front();            q.pop();            for(int i=head[u];i!=-1;i=p1[i].next)            {                int v=p1[i].to;                if(!q1.count(v))                    continue;                q1.erase(v);                q2.insert(v);            }            for(it=q1.begin();it!=q1.end();it++)            {                dis[*it]=dis[u]+1;                q.push(*it);            }            q1.swap(q2),q2.clear();        }        for(int i=1;i<=n;i++)        {            if(i!=s)            {                printf("%d%c",dis[i]==inf?-1:dis[i]," \n"[i==n]);            }        }    }    return 0;}void add(int u, int v){    p1[cnt].to=v;    p1[cnt].next=head[u];    head[u]=cnt++;    return ;}

0 0
原创粉丝点击