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
随机推荐
- 解决了IE8不支持数组的indexOf方法
ie在过去给我们添了很多坑. if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt /*, from*/ ) { ...
- CentOS6.5自带Python2.6.6升级至Python2.7
CentOS6.5中Python2.6升级到Python2.7 由于Python开发团队已不再支持2.6版本,且该版本对一些软件不支持,因此将2.6升级到2.7. 1.安装Python2.7: 下载源 ...
- wpf 界面线程 添加项
foreach (var r in sec.Records) { listView.Dispatcher.Invoke((new Action(delegate() { listView.Items. ...
- jdk 多版本安装 for mac
2016年mac上已经安装有jdk1.6的版本 目录在/Library/Java/JavaVirtualMachines/1.6.0.jdk 有时候mac版本跟新会自动删除jdk1.6 所以要去ma ...
- js框架封装,模拟jQuery封装
模拟jQuery框架,利用原生的js技术,封装一个js框架,以加深对jQuery的常用api的使用和面向对象原理的理解:一:结构部分首先利用闭包,构造一个自执行函数,然后利用选择器函数Sizzle,获 ...
- Tomcat多域名的配置
有时候我们有好几个项目需要发布在同一个tomcat服务器上,每个项目有不同的域名.这就需要在tomcat里配置多域名,添加多个虚拟主机. 主要在server.xml里面设置: 在<Engine& ...
- Android抓包方法
0. Fiddler代理 1.tcpdump命令+wireshark工具 adb shell #登入手机 su #切换Root用户 /data/local/tcpdump -p ...
- github本地库及clone常用命令
新建本地库 1. cd d: 2. mkdir git 3. cd git 4. git init 5. git remote add origin git@github.com:swportal/ ...
- java poi 导出Excel文件
1,导包 poi-3.9-XXX.JAR 2, 创建一个实体对象 public class Student implements Serializable { /** * */ private st ...
- window下面配置sftp
Windows 下 搭建 基于 ssh 的sftp 服务器,服务器端可以用 freesshd,F-secure server 等,filezilla server不可用,之前傻乎乎的用filezi ...