Sparse Graph

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

Total Submission(s): 928    Accepted Submission(s): 312

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 N−1 other
vertices.
 
Input
There are multiple test cases. The first line of input is an integer T(1≤T<35) denoting
the number of test cases. For each test case, the first line contains two integers N(2≤N≤200000) and M(0≤M≤20000).
The following M lines
each contains two distinct integers u,v(1≤u,v≤N) denoting
an edge. And S (1≤S≤N) is
given on the last line.
 
Output
For each of T test
cases, print a single line consisting of N−1 space
separated integers, denoting shortest distances of the remaining N−1 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
补图上的BFS
因为补图是一个完全图,然后去掉最多两万条边,有可以想象,从起点到任何一点的最短距离肯定很短,我可以从起点开始扫
把所有没有和起点有边的(原图中)入队列,这是第一层距离为1的,然后以这些点继续扩展,因为最短距离很短,最多扩展10
次就把所有点都扫过了,已经扩展过的点在bfs过程中就不要扫了,所以可以set或者vector把扩展过的点删除
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <set>
#include <vector>
#include <queue> using namespace std;
const int maxn=2*1e5;
int n,m,k;
int d[maxn+5];
set<int> a[maxn+5];
set<int> s;
queue<int> q;
int de[maxn+5];
void BFS(int x)
{
q.push(x);
memset(d,-1,sizeof(d));
d[x]=0;
while(!q.empty())
{
int term=q.front();
q.pop();
int cnt=0;
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
bool tag=true;
int i=*it;
if(a[i].find(term)==a[i].end())
{
d[i]=d[term]+1;
q.push(i);
de[cnt++]=i;
}
}
for(int i=0;i<cnt;i++)
s.erase(de[i]);
}
}
int main()
{
int t;
int x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
s.clear();
for(int i=1;i<=n;i++)
s.insert(i),a[i].clear();
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
a[x].insert(y);
a[y].insert(x);
} scanf("%d",&k);
s.erase(k);
BFS(k);
for(int i=1;i<=n;i++)
{
if(i==k)
continue;
printf("%d",d[i]);
if(i!=n)
printf(" ");
}
printf("\n");
}
return 0;
}

 

HDU 5876 大连网络赛 Sparse Graph的更多相关文章

  1. 2016大连网络赛 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) P ...

  2. 大连网络赛 1006 Football Games

    //大连网络赛 1006 // 吐槽:数据比较水.下面代码可以AC // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) #include <bits/stdc++.h> usi ...

  3. HDU 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)

    题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...

  4. 【icpc网络赛大连赛区】Sparse Graph

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

  5. HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)

    很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...

  6. HDU 5875 Function 大连网络赛 线段树

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  7. HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Tota ...

  8. HDU 5877 Weak Pair (2016年大连网络赛 J dfs+反向思维)

    正难则反的思想还是不能灵活应用啊 题意:给你n个点,每个点有一个权值,接着是n-1有向条边形成一颗有根树,问你有多少对点的权值乘积小于等于给定的值k,其中这对点必须是孩子节点与祖先的关系 我们反向思考 ...

  9. HDU 4004 The Frog's Games(2011年大连网络赛 D 二分+贪心)

    其实这个题呢,大白书上面有经典解法  题意是青蛙要跳过长为L的河,河上有n块石头,青蛙最多只能跳m次且只能跳到石头或者对面.问你青蛙可以跳的最远距离的最小值是多大 典型的最大值最小化问题,解法就是贪心 ...

随机推荐

  1. Web Socket rfc6455 握手 (C++)

    std::string data((const char*)buf->data(),bytes_transferred); recycle_buffer(buf); std::string ke ...

  2. vss安装及服务器端、客户端配置图文教程

    安装VSS 一.双击setup. 之后点完成.就安装完了! 服务器端VSS配置 一.选择开始——所有程序——打开 一直下一步  到完成 二.然后再打开 说明:把这个enable rights and  ...

  3. 解决Access denied for user &#39;&#39;@&#39;localhost&#39; to database &#39;mysql&#39;问题

    在改动mysql的root用户password后,再登陆,提示如标题的错误,找了一番答案之后,最终解决,过程例如以下: 1.停掉mysql:      service mysqld stop 2.使用 ...

  4. PHP——数组和数据结构

    <body> <?php $arr[0]=5;//赋值定义 $arr[1]="aa"; print_r($arr); echo "<br /> ...

  5. 微信小程序 - toptip效果

    在Page顶部下滑一个提示条 , 代码见 /mixins/UIComponent.js ,其中的self 可以认为是微信小程序的Page对象 效果: 默认2秒展示,上移动画隐藏 /** * 展示顶部 ...

  6. ASP.NET MVC4 异常拦截

    ASP.NET MVC4 程序发生异常时,通过拦截Action的异常,重写ActionFilterAttribute 的方法OnActionExecuted实现. 具体实现代码如下: /// < ...

  7. mac环境搭建selenium

    前言 搭建python+selenium,mac自带python2.7,需要公司使用的python是3.x,可以自己百度安装python环境. 1. selenium安装 1. selenium的安装 ...

  8. 用 free 或 delete 释放了内存之后,立即将指针设置为 NULL,防止产 生“野指针”

    用 free 或 delete 释放了内存之后,立即将指针设置为 NULL,防止产 生“野指针”. #include <iostream> using namespace std; /* ...

  9. storm的集群安装与配置

    storm集群安装 机器:(storm及zookeeper都是这3台机器) 192.168.80.20 192.168.80.21 192.168.80.22 须要准备的软件有: zookeeper( ...

  10. c# windows service(服务)

    //安装%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe WindowsServiceTest.exe //卸载%Syst ...