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的更多相关文章

  1. 最短路+找规律 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两个节点,所经过 ...

  2. 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 ...

  3. [HNOI2006]最短母串问题——AC自动机+状压+bfs环形处理

    Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. 32MB Input 第一行是一个正整数n(n< ...

  4. 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的点 ...

  5. BZOJ3073 PA2011Journeys(线段树+bfs)

    线段树优化建图裸题.建两棵线段树,一棵表示入一棵表示出.对题中所给的边新建一个虚拟点,将两段区间拆成线段树上对应区间,出线段树中对应区间所表示的点向虚拟点连边权0的边,虚拟点向入线段树中对应区间所表示 ...

  6. 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 ...

  7. HDU 4444:Walk(思维建图+BFS)***

    http://acm.hdu.edu.cn/showproblem.php?pid=4444 题意:给出一个起点一个终点,给出n个矩形的两个对立顶点,问最少需要拐多少次弯可以从起点到达终点,如果不能输 ...

  8. 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$张卡, 使用后达到坐 ...

  9. Borg Maze(MST & bfs)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9220   Accepted: 3087 Descrip ...

随机推荐

  1. RTMP协议的理解

    RTMP协议:real time message protocol 工作原理: 先采集摄像头视频和麦克风音频信息,再进行音视频的编码(mpeg),通过FMLE(Flash Media Live Enc ...

  2. CISCO-路由器交换机密码恢复

    路由器密码恢复: 准备工作:一台PC跟一台路由器用console线相连 工作原理:如果忘记密码被锁在路由器外,通过修复寄存器值来进行修复 默认的寄存器值为0x2102(关闭的),若要恢复口令需要开启这 ...

  3. regular

    regular.py import re # . # 只能匹配一个字母,而不是2个或0个 # \ # 转义 # 'abc\\.com' r'abc\.com' # 字符集[] # 匹配他所包括的任意字 ...

  4. WinThruster中文版破解方法(注册表无伤清理工具)

    每次卸载完软件,都会有注册表残余垃圾,久而久之电脑会越来越慢,winThruster可以检测出系统无用注册表,并删除. 1.解压文件,安装Setup_WinThruster_2015.exe文件. 2 ...

  5. 在IIS6.0以上版本发布Ajax中,解决添加.v路径找不到的问题?

    问题描述:配置Aiax方式如下: 1.在AppCode中加入文件夹Ajax,加入两个类文件: Ajax.cs: using System; using System.Collections.Gener ...

  6. Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源方法

    一.开篇 这里整合分别采用了Hibernate和MyBatis两大持久层框架,Hibernate主要完成增删改功能和一些单一的对象查询功能,MyBatis主要负责查询功能.所以在出来数据库方言的时候基 ...

  7. bzoj4833

    $数论$ $这个题已经忘了怎么做了,也不想知道了,只记得看了3个小时$ $对于有gcd(f_i, f_j) = f_{gcd(i, j)}性质的数列,以下结论适用$ #include<bits/ ...

  8. JAVA全栈工程师应具备怎样的知识体系?

    Java是超高人气编程语言,拥有跨平台.面向对象.泛型编程等特性.在TIOBE编程语言排行榜中,连续夺得第一宝座,而且国内各大知名互联网公司,后端开发首选语言:非Java莫属. 今天是针对各类目有更详 ...

  9. Hadoop中Yarnrunner里面submit Job以及AM生成 至Job处理过程源码解析

    参考 http://blog.csdn.net/caodaoxi/article/details/12970993 Hadoop中Yarnrunner里面submit Job以及AM生成 至Job处理 ...

  10. java的环境变量classpath中加点号 ‘.’ 的作用

    java的环境变量classpath中加点号 ‘.’ 的作用 “.”表示当前目录,就是编译或者执行程序时,你的.class文件所在的目录: 当找.class文件时,先去“.”路径下找,找不到的话,在去 ...