HDU 5876 大连网络赛 Sparse Graph
Sparse Graph
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 928 Accepted Submission(s): 312
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.
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.
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.
1
2 0
1
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的更多相关文章
- 2016大连网络赛 Sparse Graph
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) P ...
- 大连网络赛 1006 Football Games
//大连网络赛 1006 // 吐槽:数据比较水.下面代码可以AC // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) #include <bits/stdc++.h> usi ...
- HDU 5867 Sparse Graph (2016年大连网络赛 I bfs+补图)
题意:给你n个点m条边形成一个无向图,问你求出给定点在此图的补图上到每个点距离的最小值,每条边距离为1 补图:完全图减去原图 完全图:每两个点都相连的图 其实就是一个有技巧的bfs,我们可以看到虽然点 ...
- 【icpc网络赛大连赛区】Sparse Graph
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submissi ...
- HDU 5875 Function (2016年大连网络赛 H 线段树+gcd)
很简单的一个题的,结果后台数据有误,自己又太傻卡了3个小时... 题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对 ...
- HDU 5875 Function 大连网络赛 线段树
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Tota ...
- HDU 5877 Weak Pair (2016年大连网络赛 J dfs+反向思维)
正难则反的思想还是不能灵活应用啊 题意:给你n个点,每个点有一个权值,接着是n-1有向条边形成一颗有根树,问你有多少对点的权值乘积小于等于给定的值k,其中这对点必须是孩子节点与祖先的关系 我们反向思考 ...
- HDU 4004 The Frog's Games(2011年大连网络赛 D 二分+贪心)
其实这个题呢,大白书上面有经典解法 题意是青蛙要跳过长为L的河,河上有n块石头,青蛙最多只能跳m次且只能跳到石头或者对面.问你青蛙可以跳的最远距离的最小值是多大 典型的最大值最小化问题,解法就是贪心 ...
随机推荐
- atitit.激活一个窗口总结 swing java .net php
atitit.激活一个窗口总结 1 激活窗口:鼠标激活vswindows消息激活 1.1 Web框架激活 2 退出激活窗口热键(dbg模式) 3 俩个窗口激活优先级 ...
- JsonHelper修改4.0
public class JsonHelper { /// <summary> /// 将对象序列化为JSON格式 /// </summary> /// <param n ...
- Taking A Fresh Look At What Open Source API Management Architecture Is Available
http://apievangelist.com/2014/10/05/taking-a-fresh-look-at-what-open-source-api-management-architect ...
- 【计算机网络】IP分类
A类IP地址 A类IP地址:用可变的7位(bit)来标识网络号,可变的24位标识主机号,最前面一位为"0",即A类地址的第一段取值介于1-126之间.A类地址通常为大型网络而提供, ...
- 第八课:不一样的链表 linux链表设计哲学 5星级教程
这一课最后实现的链表,和普通链表不同,借鉴了linux内核链表的思想,这也是企业使用的链表. 基础介绍: 顺序表的思考 顺序表的最大问题是插入和删除需要移动大量的元素!如何解决?A:在线性表数据元素之 ...
- Phoenix的数据类型和操作符、函数
其实官方文档已经有这些东西了,如下: http://phoenix.apache.org/language/functions.html http://phoenix.apache.org/langu ...
- 如何测试是否安装了web服务器
windows默认没有安装web服务器,我们可以安装IIS. 我们安装个tomacte服务器,开发web程序必须的!!如果测试后出现这个页面说明安装成功le ! 我们这个安装的是本地服务器,可以把we ...
- mysql 存储过程调用
CALL 存储过程名('参数值1',‘参数值2',’参数值3')
- [oracle] update语句卡住问题
执行update语句的时候发现执行半天不成功 update main_order set order_source = '2', order_status = '2' 查询哪些对象被锁 select ...
- ubuntu 按键替换 Control_R to Left
ubuntu 按键替换 Control_R to Left 1 查看当前键盘布局 $xmodmap -pke keycode 105 = Control_R NoSymbol Control_Rkey ...