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 ...
随机推荐
- Swift Optional Chaining
Optional Chaining介绍 关于「optional chaining」,<The Swift Programming Language>是这么描述的: Optional cha ...
- C++之面向对象初探----对象管理模型(关键是this指针)
前言 c++对象模型可以概括为以下2部分 1.语言中直接支持面向对象程序员设计部分,主要涉及如构造函数.析构函数.虚函数.继承(单继承.多继承.虚继承).多态等待. 2.对于各种支持的底层实现机制 在 ...
- C# 获取QQ好友列表信息的实现
分析部分 当我们访问QQ空间的时候,大家可以在右侧的发现一个这样的统计信息 当点击这个链接的时候,会跳转到 这样一个URL 这个URl可以管理好友,当然也就能读取到好友 上面我们是在浏览器中的操 ...
- py-day1简单使用方法及语法使用详解
一.python入门 1.python的标准格式 创建python文件以.py结尾 如:vi hello.py #!/usr/bin/env python #-*- coding:utf-8 -*- ...
- 5.oracle中一个字段中存储'a','b'与'a'与a的写法,存储过程中与之对应
select '''a'',''b''' from dual; --'a','b' select '''a''' from dual; --'a' select 'a' from dual; --a
- 抓屏工具 faststone capture
百度百科 http://baike.baidu.com/link?url=te51CfOKYIEmqT1jsyRwcB8Pnals5xQ8nUXk6trvBPGSJRBO5G7BEZL7cYQxmx8 ...
- sql语句之查询操作
语法顺序: select distinct 字段1,字段2,字段3 from 库.表 where 条件 group by 分组条件 having 过滤 # 执行顺序的话,到这步会返回运行select语 ...
- 八大排序算法java代码
1.冒泡排序 public static void main(String[] args) { int[] arr = {1,4,2,9,5,7,6}; System.out.println(&quo ...
- JQuery点击table获取点击行的数据
$(function () {var TaskType = '';$("#data_table tr:gt(0)").click(function () { TaskType = ...
- Codeforces 749C【模拟】
FST的时候好像挂了挺多人的~ 其实思路没啥难的,就是更好地理解题意吧,1到n一直循环,直到没有人能vote,一个人能vote也能叉掉一个人,一个人被叉就不能vote,判谁赢. 其实我管vote干嘛, ...