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. Python之Numpy详细教程

    NumPy - 简介 NumPy 是一个 Python 包. 它代表 “Numeric Python”. 它是一个由多维数组对象和用于处理数组的例程集合组成的库. Numeric,即 NumPy 的前 ...

  2. 【servlet学习1】使用eclipse+tomcat开发servlet示例

    零.开发环境 1.eclipse+tomcat(tomcat插件已安装到eclipse中). 一.开发servlet步骤 1.在eclipse中新建工程 File —> New —> Ot ...

  3. Winform安装包出现无法访问网络位置

    1.原因:安装包的安装路径出现了问题 2.如下图:错误路径 3.如下图:正确路径

  4. bzoj4430

    bit+容斥原理 我不会cdq分治只能用这个做法 考虑什么情况下不满足,至少有一个顺序不对就不行了,那么不满足的总有两对属性形成逆序对,那么我们用总方案数*2=n*(n-1)减去不符合的*2再/2就是 ...

  5. PHPstorm相同变量标识

    setting-> plugins-> Browse Repositories 输入BrowseWordAtCaret 搜索,安装,然后重启

  6. 有关如何线程安全的使用map(hashMap)

    最近在写一个多线程中控制输出顺序的系统中的一个代码,使用了map的数据结构.具体的业务是需要一个单例的对象,然后需要在多线程的环境下实现添加和删除的操作.部分代码如下: public class Up ...

  7. 【Hadoop】HDFS笔记(二):HDFS的HA机制和Federation机制

    HA解决了HDFS的NameNode的单点问题: Federation解决了整个HDFS集群中只有一个名字空间,并且只有单独的一个NameNode管理所有DataNode的问题. 一.HA机制(Hig ...

  8. TypeScript完全解读(26课时)_17.装饰器

    实验性的特性,需要在tslint里面把这项设置为true 作用域类的声明方法.访问符.属性和参数上 使用@符号加一个名字来定义,名字必须是一个函数,或者求值后是一个函数 装饰器工厂,setPro当做一 ...

  9. HDU 2586 How far away ? (LCA,Tarjan, spfa)

    题意:给定N个节点一棵树,现在要求询问任意两点之间的简单路径的距离,其实也就是最短路径距离. 析:用LCA问题的Tarjan算法,利用并查集的优越性,产生把所有的点都储存下来,然后把所有的询问也储存下 ...

  10. 利用StringBuffer来替换内容

    package com.test.java; public class StringBufferTest { public static void main(String[] args) { Stri ...