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. 字符串函数---itoa()函数具体解释及实现

    itoa()函数 itoa():char *itoa( int value, char *string,int radix); 原型说明: value:欲转换的数据. string:目标字符串的地址. ...

  2. 开源搜索引擎评估:lucene sphinx elasticsearch (zhuan)

    http://lutaf.com/158.htm ************************ 开源搜索引擎程序有3大类 lucene系,java开发,包括solr和elasticsearch s ...

  3. 折腾kali linux2.0

    偶然的机会了解到了kali linux这个用于渗透测试的linux发行版,于是就从官网下了iso,但是制作启动盘老出错.网上查了下说在linux下用dd命令特别简单,于是转到ubuntu下制作启动盘, ...

  4. xadmin 安装详解

    1.安装必要的包 django>=1.9.0 django-crispy-forms>=1.6.0 django-import-export>=0.5.1 django-revers ...

  5. 存储管理器实验——SDRAM

    序言:2440有nand和nor两种启动方式,在裸机部分,都是使用的nand启动. 现在,我们想在nand flash启动的时候,通过SRAM访问存储在外设SDRAM中的程序. nand启动,先把前4 ...

  6. Java compiler level does not match the version of the installed Java project facet 的解决方案

     今天将MyEclipse升级到 9.1 后,打开原来的工作空间,原来所有的项目都前面都显示了一个小叉叉,代码中却没有任何错误.于从 problems 视图中查看错误信息,错误信息的"D ...

  7. python学习笔记(10)--爬虫下载煎蛋图片

    说明: 1. 有很多细节需要注意! 2. str是保留字,不要作为变量名 3. 保存为txt报错,encoding=utf-8 4. 403错误,添加headers的方法 5. 正则match只能从开 ...

  8. 一款基于jQuery和CSS3炫酷3D旋转画廊特效插件

    这是一款效果炫酷的jQuery和CSS3 3D旋转画廊特效插件.该3D画廊插件可以通过前后导航按钮来切换图片,效果就像旋转木马一样.它还带有点击放大图片,显示图片标题和用键盘操作等功能. 在线预览   ...

  9. Apache Rewrite 拟静态

    mod_rewrite是Apache的一个非常强大的功能,它可以实现伪静态页面.一些防盗链就是通过该方法做到的. 00x1 启动rewrite引擎 00x2 如何启用apache rewrite? 0 ...

  10. headfirst设计模式swift版01

    headfirst设计模式这本书真好,准备用一个月学完.书里讲得很清楚了. 设计原则: 1.找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起. 2.针对接口编程,而不是针 ...