L. Right Build bfs
http://codeforces.com/gym/101149/problem/L
给出一个有向图,从0开始,<u, v>表示要学会v,必须掌握u,现在要学会a和b,最小需要经过多少个点。
做这题的时候,一看就觉得是先找出a和b点的lca,但是以前学的LCA是树的,现在这个是图。
一定要知道LCA是树的,图的不行。
然后下午去上课了,图中找了一个博客里说,求有向图得lca可以反向建图 + bfs来搞,(当时还不清楚LCA是用在树上的,一直以为可以。)具体思路是对两个起点进行了bfs,然后拿她们的bfs序来判断相交在哪里。然后就搞了一个晚上了,最后发现是不行的,有数据保证不行,因为bfs的时候也有先后之分,这个先后和你的边顺序有关,就不能确定了
然后就想暴力吧,没一个a和b在反图上路径相同的点,都暴力去和答案更新最小值。
TLE几次后就能过了,预处理出数组就可以过。
给一些数据你们吧,找了一天的数据~~
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string> const int maxn = 5e5 + ;
struct node {
int u, v, tonext;
} e[maxn];
int num;
int first[maxn];
int used;
void add(int u, int v) {
++num;
e[num].u = u;
e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
int n, m, a, b;
int ans;
int book[maxn];
int zero[maxn];
struct bfsnode {
int cur;
int cnt;
bfsnode(int a, int b) : cur(a), cnt(b) {}
};
queue<struct bfsnode>que, quese;
int bfs(int begin, int end, int DFN) {
if (begin == end) return ;
book[begin] = DFN;
while (!quese.empty()) quese.pop();
quese.push(bfsnode(begin, ));
while (!quese.empty()) {
struct bfsnode t = quese.front();
quese.pop();
for (int i = first[t.cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (book[v] == DFN) continue;
if (v == end) return t.cnt + ;
zero[v] = t.cnt + ;
book[v] = DFN;
quese.push(bfsnode(v, t.cnt + ));
}
}
return inf;
}
int first_two[maxn];
struct Edge {
int u, v, tonext;
} e_two[maxn];
int num_two;
void add_two(int u, int v) {
++num_two;
e_two[num_two].u = u;
e_two[num_two].v = v;
e_two[num_two].tonext = first_two[u];
first_two[u] = num_two;
}
int bookA[][maxn];
int book_two[maxn];
void BFS(int begin, int DFN, int now) {
while (!que.empty()) que.pop();
book_two[begin] = DFN;
bookA[now][begin] = ;
if (bookA[!now][begin] != inf && zero[begin] != inf) {
ans = min(bookA[now][begin] + bookA[!now][begin] + zero[begin], ans);
}
que.push(bfsnode(begin, ));
while (!que.empty()) {
struct bfsnode t = que.front();
que.pop();
for (int i = first_two[t.cur]; i; i = e_two[i].tonext) {
int v = e_two[i].v;
if (book_two[v] == DFN) continue;
book_two[v] = DFN;
bookA[now][v] = t.cnt + ;
if (bookA[!now][v] != inf && zero[v] != inf) {
++used;
int tans = zero[v] + bookA[now][v] + bookA[!now][v];
ans = min(ans, tans);
}
que.push(bfsnode(v, t.cnt + ));
}
}
return;
}
void find() {
memset(bookA, 0x3f, sizeof bookA);
ans = inf;
used = ;
BFS(a, used, );
++used;
BFS(b, used, );
return;
} int in[maxn];
void work() {
IOS;
cin >> n >> m >> a >> b;
if (a == b) while();
for (int i = ; i <= m; ++i) {
int u, v;
cin >> u >> v;
add(u, v);
add_two(v, u);
}
memset(zero, 0x3f, sizeof zero);
zero[] = ;
bfs(, inf, );
find();
cout << ans << endl;
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
11 12 1 6
0 1
1 2
2 3
3 4
4 5
4 8
8 9
9 10
10 11
6 4
0 6
5 6
证明bfs找lca不行
7 14 10 7
0 1
1 2
2 3
3 4
3 5
5 6
9 10
4 5
5 7
1 8
8 7
4 5
6 9
8 6
证明普通LCA不行,因为lca用在树上
L. Right Build bfs的更多相关文章
- 最短路+找规律 Samara University ACM ICPC 2016-2017 Quarterfinal Qualification Contest L. Right Build
题目链接:http://codeforces.com/gym/101149/problem/L 题目大意:有n个点(其实是n+1个点,因为编号是0~n),m条有向边.起点是0,到a和b两个节点,所经过 ...
- Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)
A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- [HNOI2006]最短母串问题——AC自动机+状压+bfs环形处理
Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 32MB Input 第一行是一个正整数n(n< ...
- HDU5957 Query on a graph(拓扑找环,BFS序,线段树更新,分类讨论)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5957 题意:D(u,v)是节点u和节点v之间的距离,S(u,v)是一系列满足D(u,x)<=k的点 ...
- BZOJ3073 PA2011Journeys(线段树+bfs)
线段树优化建图裸题.建两棵线段树,一棵表示入一棵表示出.对题中所给的边新建一个虚拟点,将两段区间拆成线段树上对应区间,出线段树中对应区间所表示的点向虚拟点连边权0的边,虚拟点向入线段树中对应区间所表示 ...
- hdu-2586 How far away ?(lca+bfs+dfs+线段树)
题目链接: How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4444:Walk(思维建图+BFS)***
http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给出一个起点一个终点,给出n个矩形的两个对立顶点,问最少需要拐多少次弯可以从起点到达终点,如果不能输 ...
- Board Game CodeForces - 605D (BFS)
大意: 给定$n$张卡$(a_i,b_i,c_i,d_i)$, 初始坐标$(0,0)$. 假设当前在$(x,y)$, 若$x\ge a_i,y\ge b_i$, 则可以使用第$i$张卡, 使用后达到坐 ...
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
随机推荐
- Java深度理解——Java字节代码的操纵
导读:Java作为业界应用最为广泛的语言之一,深得众多软件厂商和开发者的推崇,更是被包括Oracle在内的众多JCP成员积极地推动发展.但是对于 Java语言的深度理解和运用,毕竟是很少会有人涉及的话 ...
- html5--3.15 textarea元素
html5--3.15 textarea元素 学习要点 掌握textarea元素的使用 textarea元素 用来建立多行输入文本框 元素标签中的内容将一文本框默认值的形式呈现 不仅可以用在表单中,也 ...
- python中的不可变类型和可变类型
在python中整形,字符串,元组是不可变类型,而列表和字典都是可变类型. 对于不可变类型进行重新赋值,相当于是用以前的变量名重新指向了新的地址,这个地址中存的变量值就是重新的赋值 通过python中 ...
- bootstrap 学习笔记(1)---介绍bootstrap和栅格系统
学习前端许久,对于布置框架和响应浏览器用html 和javascript 写的有点繁琐,无意间看到这个框架,觉得挺好用的就开始学习了,但是这个框架上面有很多知识,不是所有的都要学的,故将学习笔记和觉得 ...
- saltstack master minion安装配置简单使用
首先先了解下saltstack是什么,为什么使用它 它与Ansible.Puppet都属于集中管理工具,由于现在企业规模的不断庞大及业务的增长,所需要管理的服务器数量增多,而且大部分机器都属于同类业务 ...
- ping测试网络
https://jingyan.baidu.com/article/ac6a9a5e109d5f2b653eacbc.html 百度百科:https://baike.baidu.com/item/pi ...
- Mertens
题意: 求解$\sum_{i=a}^b{\mu(i)}$. 解法: 由$(\mu * I)(n) = e(n)$ 得 $\sum_{d|n}{\mu(d)} = [n=1]$ 得 $\mu(n) = ...
- 3-C++程序的结构1.2
对象的生存周期 可以分为静态生存周期和动态生存周期 1.静态生存周期 如果对象的生存期与程序的运行期相同,我们称它具有静态生存期.在文件作用域中声明的对象都具有静态生存期的.如果要在函数的块中声明具有 ...
- Qt开篇
使用Qt两年有余,遇到问题多是现查现用,由于之前供职于一家保密性较强的单位,遇到的很多问题没有被记录下来.从今天开始,我会记记录自己的笔记.
- C++ STL map使用
Map是c++的一个标准容器,它提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map构造函数:map<string , in ...