hdu 5876 ACM/ICPC Dalian Online 1009 Sparse Graph
分析:这叫补图上的BFS,萌新第一次遇到= =。方法很简单,看了别人的代码后,自己也学会了。方法就是开两个集合,一个A表示在下一次bfs中能够到达的点,另一个B就是下一次bfs中到不了的点。一开始先把出了起点的所有点都加入A,然后从bfs的点跑一遍边, 把边相连的点从A中取出放到B中。然后遍历A集合,进行bfs。然后把B全部放入A中,清空B。于是又回到了通过边把边连接的点从A移动到B,重复bfs。。。解释地不是很清楚,大体意思就是这样的了。在这个方法中,每个点和每条边都值操作过一次,复杂度是O(N+M)但是set好像有一点常数(?)萌新表示不是很清楚。
代码:
/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31;
/*****************************************************/
const int maxn = 2e5 + 5;
std::vector<int> G[maxn];
set<int> in, out;
int N, M;
int dis[maxn];
bool inn[maxn];
void bfs(int s) {
queue<int> que;
in.clear(); out.clear();
mem(inn, false);
mem(dis, INF);
for (int i = 1; i <= N; i ++) if (i != s) {
in.insert(i);
inn[i] = true;
}
dis[s] = 0;
que.push(s);
while (!que.empty() && in.size() != 0) {
int u = que.front(); que.pop();
for (int i = 0; i < G[u].size(); i ++) {
int v = G[u][i];
if (inn[v]) {
inn[v] = false;
in.erase(v);
out.insert(v);
}
}
for (set<int> :: iterator it = in.begin(); it != in.end(); it ++) {
int v = *it;
inn[v] = false;
if (dis[v] > dis[u] + 1) {
dis[v] = dis[u] + 1;
que.push(v);
}
}
in.clear();
for (set<int> :: iterator it = out.begin(); it != out.end(); it ++) {
int k = *it;
inn[k] = true;
in.insert(k);
}
out.clear();
}
}
int main(int argc, char const *argv[]) {
int T; cin>>T;
while (T --) {
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; i ++) G[i].clear();
for (int i = 0; i < M; i ++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].pb(v);
G[v].pb(u);
}
int s; scanf("%d", &s);
bfs(s);
for (int i = 1; i <= N; i ++) if (i != s) {
printf("%d", dis[i] == INF ? -1 : dis[i]);
if (i == N) puts("");
else printf(" ");
}
}
return 0;
}
hdu 5876 ACM/ICPC Dalian Online 1009 Sparse Graph的更多相关文章
- hdu 5875 ACM/ICPC Dalian Online 1008 Function
题目链接 分析:用RMQ预处理每段的最小值,然后对每次查询的区间找最靠近左边的小于的值,取模后递归操作.因为每次取模至少会使原来的值减半,所以递归操作是的.每次查询最小值如果通过线段树那么最终的复杂度 ...
- hdu 5877/ 2016 ACM/ICPC Dalian Online 1010 Weak Pair
题目链接 分析:树上的节点祖先与儿子的关系,一般就会想到dfs序.正解就是对树先进行dfs序排列,再将问题转化到树状数组统计个数.应该把节点按照权值从大到小排序,这样对于,就是从小到大的顺序.这样更新 ...
- HDU 5876 (大连网赛1009)(BFS + set)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5876 题意:给定一个图(n个顶点m条边),求其补图最短路 思路:集合a表示当前还未寻找到的点,集合b表 ...
- 2016 ACM/ICPC Asia Regional Dalian Online
1009 Sparse Graph(hdu5876) 由于每条边的权值都为1,所以最短路bfs就够了,只是要求转置图的最短路,所以得用两个set来维护,一个用来存储上次扩散还没访问的点,一个用来存储这 ...
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)
Friends and Enemies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)
Football Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
随机推荐
- JSP 运行
Tomcat 上运行 JSP 应用程序 完成一些包装工作后,才能在 Tomcat 上运行 index.jsp 程序.通常需要按照以下步骤操作: 创建 JSP 应用程序.如果只使用一个页面,则称它为 i ...
- Maven聚合与继承的实例讲解(一)
概述 在javaweb高速发展的今天,我们软件设计人员往往会用很多种方式对软件划分模块,目的就是为了能有清晰的设计和低耦合性的,高重用性的软件.Maven有很好的依赖管理系统(Dependency M ...
- 解决Firefox浏览器每次打开都弹出导入向导的问题
每次打开Firefox浏览器都会弹出导入向导这个页面,只有这个页面关闭后,Firefox界面才会打开. 解决办法: C:\Users\{用户名}\AppData\Roaming\Mozilla\Fir ...
- 通过Trainingkit对Azure有一个初步的了解
学习Azure有一个非常不错的资料库Azure training kit. 这里面包含了很多Azure团队编写的实例代码,以及为初学Azure的开发人员准备的新手教学课程. 开发人员可以从http:/ ...
- Upload file
<h3>Upload File</h3> <form action="@Url.Action("Upload","UploadCo ...
- WordPress 添加Meta Box的方法步骤
需要使用到add meta boxes Action,该Action允许我们为任何文章类型注册Meta Box,在该Action中,我们需要使用add_meta_box()方法来添加Meta Box的 ...
- Spine批量导出Command line Export
1.准备工作及介绍 时间有点紧张,写的不是很详细,请见谅. 当前版本是2.2以上,购买版的.试用版的无法试用Command line Both Spine and the Spine launcher ...
- 【转】Repository has not been enabled to accept revision propchanges
转载地址:http://lg-zhou.blog.163.com/blog/static/178068920111179341041/ 使用SVN提交版本信息时,注释内容写的不全.通过右键Tortoi ...
- YTU 2345: 后序遍历二叉树
原文链接:https://www.dreamwings.cn/ytu2345/2611.html 2345: 后序遍历二叉树 时间限制: 1 Sec 内存限制: 128 MB 提交: 3 解决: ...
- POJ3420Quad Tiling(矩阵快速幂)
Quad Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3740 Accepted: 1684 Descripti ...