2016 ICPC 大连网络赛 HDU 5876 Sparse Graph

Sparse Graph

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


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
 【题意】给你一个n个结点,m条边的无向图G,再给你G的补图H上的一个点S,要求出在补图H上,点S到其他n-1个结点的最短路
【解题方法】某一年四川省赛原题了。做法是:由于起点S是给出的,我们可以从点S开始,进行广搜,对于点u和点v,若原图中,两点直接相连,那么补图中的这两点必定没有直接相连,但如果我们对每一点都要判断一遍有哪些点在原图中没有直接相连,这个复杂度还是很高的,所以我们可以采用std::set维护还没BFS过的点,当要扩展点u的时候,遍历一次还没访问过的点v,如果u和v之间没边,那么将v入队;否则将v留在未扩展点中。
【时间复杂度】O(n+m)
【AC 代码】
//
//Created by just_sort 2016/9/12 16:27
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#include <set>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 200010;
int head[maxn],ans[maxn],edgecnt,n,m;
struct edge{
    int v,next;
}E[maxn*2];
void init(){
    edgecnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v){
    E[edgecnt].v=v,E[edgecnt].next=head[u],head[u]=edgecnt++;
}
void bfs(int s)
{
    queue<int>que;
    ans[s]=0;
    que.push(s);
    set<int>s1,s2;//s1未被访问
    for(int i=1; i<=n; i++){
        if(i!=s) s1.insert(i);
    }
    while(!que.empty()){
        int u=que.front();
        que.pop();
        for(int i=head[u]; ~i; i=E[i].next){
            int v=E[i].v;
            if(!s1.count(v)) continue;
            s1.erase(v);
            s2.insert(v);
        }
        for(auto it=s1.begin(); it!=s1.end(); it++){
            que.push(*it);
            ans[*it]=ans[u]+1;
        }
        s1.swap(s2);
        s2.clear();
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d",&n,&m);
        for(int i=1; i<=m; i++){
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        int s;
        scanf("%d",&s);
        bfs(s);
        for(int i=1; i<n; i++){
            if(i!=s) printf("%d ",ans[i]);
        }
        printf("%d\n",ans[n]);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值