hdu_5876_Sparse Graph(补图BFS)
附上叉姐的题解:
1009 Sparse Graph [by ftiasch]
题意:n 个点的无向完全图中删除 m 条边,问点 s 到其他点的最短路长度。
题解:
补图上的 BFS 是非常经典的问题。一般的做法是用链表(或者偷懒用 std::set)维护还没 BFS 过的点。当要扩展点 u 的时候,遍历一次还没访问过的点 v,如果 uv 没边,那么将 v 入队。否则将 v 留在未扩展点中。
很明显,后者只会发生 m 次,前者只会发生 n 次,所以复杂度是 O(n + m)O(n+m)
.
#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;i++)
using namespace std; const int N=2e5+,M=,inf=2e6+;
int t,n,m,ed,v[M],nxt[M],g[N],x,y,S,d[N]; void adg(int x,int y){v[++ed]=y,nxt[ed]=g[x],g[x]=ed;} void fuck()
{
set<int>ta,tb;
set<int>::iterator it;
queue<int>Q;
F(i,,n)d[i]=inf;
d[S]=,Q.push(S);
F(i,,n)if(i!=S)ta.insert(i);
while(!Q.empty())
{
int now=Q.front();Q.pop();
for(int i=g[now];i;i=nxt[i])if(ta.find(v[i])!=ta.end())ta.erase(v[i]),tb.insert(v[i]);
for(it=ta.begin();it!=ta.end();it++)Q.push(*it),d[*it]=d[now]+;
ta.swap(tb),tb.clear();
}
F(i,,n)if(i!=S)printf("%d%c",d[i]==inf?-:d[i]," \n"[i==n]);
} int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m),ed=;
F(i,,n)g[i]=;
F(i,,m)scanf("%d%d",&x,&y),adg(x,y),adg(y,x);
scanf("%d",&S),fuck();
}
return ;
}
hdu_5876_Sparse Graph(补图BFS)的更多相关文章
- [Algorithms] Graph Traversal (BFS and DFS)
Graph is an important data structure and has many important applications. Moreover, grach traversal ...
- HDU 5876 Sparse Graph(补图上BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意: 有一个 n 个点无向图,再给你 m 对顶点, 代表着这 m 对顶点之间没有边, 除此之外 ...
- hdu 5876 (补图BFS) Sparse Graph
题目:这里 题意: 相当于一开始给一个初始好了的无向完全图给你,然后给让你删除m条边,再给你一个点v,最后问你在剩下的图里从这个点v出发能到达所有边点的最小路径是多少? 一看是所有点的最小路径,一看就 ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- 【BZOJ】1098: [POI2007]办公楼biu(补图+bfs+链表)
http://www.lydsy.com/JudgeOnline/problem.php?id=1098 显然答案是补图连通块..... 想到用并查集...可是连补图的边都已经...n^2了...怎么 ...
- 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)
In this problem, we will define a graph called star graph, and the question is to find the minimum d ...
- POJ-2415 Hike on a Graph (BFS)
Description "Hike on a Graph" is a game that is played on a board on which an undirected g ...
- 补图BFS(hdu 5876)
题目大意: 给出一个图和起点S,求补图中S到其他点的最短距离. http://acm.hdu.edu.cn/showproblem.php?pid=5876 我自己的垃圾做法: 用线段树来维护dijk ...
- algorithm@ find the shortest path in a graph using BFS
Finding Shortest Paths By BFS
随机推荐
- iOS 自定义layer的两种方式
在iOS中,你能看得见摸得着的东西基本都是UIView,比如一个按钮,一个标签,一个文本输入框,这些都是UIView: 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图层 在创建UIVi ...
- SDN基础
http://www.h3c.com.cn/Solution/Smart_Network/SDN/ http://network.51cto.com/network/content2013/SDNke ...
- java 访问后台方法顺序混乱
今天遇到后台接值顺序混乱的问题. 环境:前台ajax请求后台方法.前台页面会频繁访问这个ajax. 现象:访问后台方法的顺序混乱. 怎么发现的问题:数量小访问没有问题,今天压力测试发现的问题. 解决办 ...
- iOS 发布项目到CocoaPods其实没那么复杂😆
首先大家必须要了解一下CocoaPods (如果你连CocoaPods是啥都不知道可以不用往下看了
- python3 列表 函数
python3中list的所有函数 list是有序的,元素个数无限的,元素类型多样的,可变的 增加 # 'append', 增加对象# 'insert', 指定位置增加# 'extend', 增加可迭 ...
- ERRO错误解决方案整理
1.chorm访问本地数据出现跨域问题 Cross origin requests are only supported for protocol schemes: http, data, chrom ...
- zabbix 布署实践【3 proxy安装】
使用openstack在生产环境创建的一台虚拟机 环境 CentOS7 4核4G内存40G硬盘 IP:10.120.150.150 镜像默认关闭防火墙,selinux ,NetworkManage ...
- hadoop,yarn和vcpu资源配置
Hadoop YARN同时支持内存和CPU两种资源的调度(默认只支持内存,如果想进一步调度CPU,需要自己进行一些配置),本文将介绍YARN是如何对这些资源进行调度和隔离的. 在YARN中,资源管理 ...
- 浙大pat1020题解
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- ubuntu搭建git服务器
1.准备两台ubuntu虚拟机,其中一个作为server,另一个作为client.检查是否安装ssh,如果没有安装:sudo apt-get install openssh-server, 然后在se ...