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 ...
随机推荐
- PHP编写网页BS结构计算器
利用PHP和Html + javascript 编写一个简单的网页计算器程序,可以根据这种有趣的开发构架出更加有趣的网页.1. [代码]PHP计算器 <html><head& ...
- asterisk用 freetds记录 cdr 到mssql
Compile, configure, and install the latest FreeTDS package: [pre] tar -zxvf freetds-0.62.4 ...
- 「LuoguP3796」 【模板】AC自动机(加强版)
题目描述 有N个由小写字母组成的模式串以及一个文本串T.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串T中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据. 每组数据的第一 ...
- MongoDB 3.2复制集单节点部署(四)
MongoDB在单节点中也可以做复制集,但是仅限于测试实验,最大的好处就是部署方便快速,可以随便添加新节点,节省资源.在这里我使用的是MongoDB 3.2版本进行复制集实验(但MongoDB配置文件 ...
- Gibonacci number-斐波那契数列
Description In mathematical terms, the normal sequence F(n) of Fibonacci numbers is defined by the r ...
- jQuery基本语法2
jQuery事件 一:jQuery鼠标事件 click单击 dbclick双击 $ele.click()不带任何参数一般是用来指定触发一个事件 $ele.click( handler(eventObj ...
- Qt Creator Theme FlatDark 配色
1.预处理指令,宏定义 颜色 #FF6AAD 2.普通代码 颜色 #D6CF9A 3.头文件 #D69545 4.系统限定符(namespace, class, public, typedef等) ...
- python 之生产者消费者模型
进程实现: import time,random from multiprocessing import Process,Queue def producer(name,q): count= 0 wh ...
- 【Linux学习】Linux系统管理2—作业调度
Linux系统管理2-作业调度 at: 作业仅执行一次就从系统工作队列中取消 语法 denny@ubuntu:~$ at [-m] TIME → 作业命令at ...
- Eclipse 安装Maven插件
这个好: http://www.iteye.com/topic/1123225 其他: 1先安装subeclipse插件就是svn svn - http://subclipse.tigris.org/ ...