[USACO08OCT]牧场散步Pasture Walking BZOJ1602 LCA
题目描述
The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures also conveniently numbered 1..N. Most conveniently of all, cow i is grazing in pasture i.
Some pairs of pastures are connected by one of N-1 bidirectional walkways that the cows can traverse. Walkway i connects pastures A_i and B_i (1 <= A_i <= N; 1 <= B_i <= N) and has a length of L_i (1 <= L_i <= 10,000).
The walkways are set up in such a way that between any two distinct pastures, there is exactly one path of walkways that travels between them. Thus, the walkways form a tree.
The cows are very social and wish to visit each other often. Ever in a hurry, they want you to help them schedule their visits by computing the lengths of the paths between 1 <= L_i <= 10,000 pairs of pastures (each pair given as a query p1,p2 (1 <= p1 <= N; 1 <= p2 <= N).
POINTS: 200
有N(2<=N<=1000)头奶牛,编号为1到W,它们正在同样编号为1到N的牧场上行走.为了方 便,我们假设编号为i的牛恰好在第i号牧场上.
有一些牧场间每两个牧场用一条双向道路相连,道路总共有N - 1条,奶牛可以在这些道路 上行走.第i条道路把第Ai个牧场和第Bi个牧场连了起来(1 <= A_i <= N; 1 <= B_i <= N),而它的长度 是 1 <= L_i <= 10,000.在任意两个牧场间,有且仅有一条由若干道路组成的路径相连.也就是说,所有的道路构成了一棵树.
奶牛们十分希望经常互相见面.它们十分着急,所以希望你帮助它们计划它们的行程,你只 需要计算出Q(1 < Q < 1000)对点之间的路径长度•每对点以一个询问p1,p2 (1 <= p1 <= N; 1 <= p2 <= N). 的形式给出.
输入输出格式
输入格式:
* Line 1: Two space-separated integers: N and Q
* Lines 2..N: Line i+1 contains three space-separated integers: A_i, B_i, and L_i
* Lines N+1..N+Q: Each line contains two space-separated integers
representing two distinct pastures between which the cows wish to
travel: p1 and p2
输出格式:
* Lines 1..Q: Line i contains the length of the path between the two pastures in query i.
输入输出样例
说明
Query 1: The walkway between pastures 1 and 2 has length 2.
Query 2: Travel through the walkway between pastures 3 and 4, then the one between 4 and 1, and finally the one between 1 and 2, for a total length of 7.
LCA+bfs 即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int edge[maxn], ver[maxn], head[maxn], nxt[maxn];
int dp[maxn][25];
int dep[maxn];
int dis[maxn];
int t, cnt;
queue<int>q; void addedge(int x, int y, int w) {
ver[++cnt] = y; edge[cnt] = w; nxt[cnt] = head[x]; head[x] = cnt;
} void bfs() {
q.push(1); dis[1] = 0; dep[1] = 1;
while (!q.empty()) {
int x = q.front(); q.pop();
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (dep[y])continue;
dep[y] = dep[x] + 1;
dis[y] = dis[x] + edge[i]; dp[y][0] = x;
for (int j = 1; j <= t; j++) {
dp[y][j] = dp[dp[y][j - 1]][j - 1];
}
q.push(y);
}
}
} int lca(int x, int y) {
if (dep[x] > dep[y])swap(x, y);
for (int i = t; i >= 0; i--) {
if (dep[dp[y][i]] >= dep[x])y = dp[y][i];
}
if (x == y)return x;
for (int i = t; i >= 0; i--) {
if (dp[y][i] != dp[x][i])y = dp[y][i], x = dp[x][i];
}
return dp[x][0];
} int main()
{
//ios::sync_with_stdio(0);
int n;
rdint(n); t = 20;
int q; rdint(q);
for (int i = 1; i < n; i++) {
int x, y; rdint(x); rdint(y); int w; rdint(w);
addedge(x, y, w); addedge(y, x, w);
}
bfs();
while (q--) {
int a, b; rdint(a); rdint(b);
cout << dis[a] + dis[b] - 2 * dis[lca(a, b)] << endl;
}
return 0;
}
[USACO08OCT]牧场散步Pasture Walking BZOJ1602 LCA的更多相关文章
- 洛谷——P2912 [USACO08OCT]牧场散步Pasture Walking(lca)
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- [luoguP2912] [USACO08OCT]牧场散步Pasture Walking(lca)
传送门 水题. 直接倍增求lca. x到y的距离为dis[x] + dis[y] - 2 * dis[lca(x, y)] ——代码 #include <cstdio> #include ...
- bzoj1602 / P2912 [USACO08OCT]牧场散步Pasture Walking(倍增lca)
P2912 [USACO08OCT]牧场散步Pasture Walking 求树上两点间路径--->lca 使用倍增处理lca(树剖多长鸭) #include<iostream> # ...
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
- 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]
P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...
- luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- 洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...
- Luogu 2912 [USACO08OCT]牧场散步Pasture Walking
快乐树剖 #include<cstdio> #include<cstring> #include<algorithm> #define rd read() #def ...
随机推荐
- python metaclass(元类)
metaclass(元类) 一.创建类的执行流程 二.元类的认识 什么是元类呢?在Python3中继承type的就是元类 二.元类的示例 方式一: # 方式一 class MyType(type): ...
- Java连接mysql数据库攻略
一. 软件下载 Mysql 下载版本:4.1.11 http://dev.mysql.com/downloads/mysql/4.1.html JDBC驱动 下载版本:3.1.8 http://dev ...
- 回调函数(callback)经典解答
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:常溪玲链接:http://www.zhihu.com/question/19801131/answer/13005983来源: ...
- opencv相关
http://opencv.org/ ================== 不错的博客: 图像处理(小魏的修行路):http://blog.csdn.net/xiaowei_cqu/article/c ...
- python的程序书写以及保存
在前面的微博里已经叙述Python的安装以及路径的设置. 在我们的windows的电脑上面我们已经安装完了python以及设置了它的路径,所以我们可以再cmd里面进行Python的编写(cmd的快速打 ...
- 向linux内核增加一个系统调用-2(利用proc打印信息)
添加系统调用,打印/proc中的系统信息 前面关于proc和内核态函数的东西可以对比代码来看. 参考 http://blog.csdn.net/kylin_fire_zeng/article/deta ...
- 第二天:tomcat体系结构和第一个Servlet
1. 打war包 2. Tomcat体系再说明: 问题:如何去配置默认主机??? 3.tomcat和servlet在网络中的位置 4. servlet快速入门案例 1).开发s ...
- SNNU女装T台走秀(状压dp)
呜啦啦啦啦啦啦~~!!SNNU首届女装T走秀大赛开始了! 本次比赛共有N名队员希望参加比赛:ddjing希望这次比赛尽可能的吸睛,因此他决定对N名队员进行一次海选: 多亏ddjing有一双发现美的眼睛 ...
- Codeforces 1077E (二分乱搞或者dp)
题意:给你一个数组,可以从中选区若干种元素,但每种元素选区的个数前一种必须是后一种的2倍,选区的任意2种元素不能相同,问可以选取最多的元素个数是多少? 思路1(乱搞):记录一下每种元素的个数,然后暴力 ...
- winform combobox绑定数据
mboBox下拉菜单控件,在数据库内的ComboBox应用的表进行修改时,如果是用的普通方法,显示数据一个方法,添加数据一个方法 这样会导致程序后期维护难度增加,在这里使用数据绑定来让ComboBox ...