• 2016大连网络赛 Sparse Graph


    Sparse Graph

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)


    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 not adjacent 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
     
    分析:对于能够到达的点依次放入队列,暴力未放入的点,可行继续放入队列即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, rt<<1
    #define Rson mid+1, R, rt<<1|1
    const int maxn=2e5+10;
    const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
    int n,m,k,t,dp[maxn];
    set<int>a,b[maxn],c;
    queue<int>p;
    int main()
    {
        int i,j;
        scanf("%d",&t);
        while(t--)
        {
            bool flag=false;
            scanf("%d%d",&n,&m);
            memset(dp,-1,sizeof dp);
            rep(i,1,n)b[i].clear(),a.insert(i);
            while(m--)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                b[u].insert(v),b[v].insert(u);
            }
            scanf("%d",&m);
            p.push(m);dp[m]=0;a.erase(m);
            while(!p.empty())
            {
                c.clear();
                int u=p.front();p.pop();
                for(int x:a)if(b[x].find(u)==b[x].end())dp[x]=dp[u]+1,c.insert(x),p.push(x);
                for(int x:c)a.erase(x);
            }
            rep(i,1,n)if(i!=m)
            {
                if(flag)printf(" %d",dp[i]);
                else printf("%d",dp[i]),flag=true;
            }
            printf("
    ");
        }
        //system("pause");
        return 0;
    }
  • 相关阅读:
    行坐标HDU1175:连连看(DFS)
    hibernatesqlHibernate中createQuery与createSQLQuery两者的区别
    代码窗口九种方式实现 javascripts 弹出对话框窗口代码
    函数线程VC++多线程函数调用
    错误classspring错误汇总
    android下载手动下载Android SDK
    节点node[算法导论]二叉排序树(Binary Search Tree)
    共享访问失败解决(无法访问 您可能没有权限使用网络资源)
    DnsTester(DNS 服务器性能测试工具)
    vc 花指令 防破解
  • 原文地址:https://www.cnblogs.com/dyzll/p/5860597.html
Copyright © 2020-2023  润新知