http://www.lydsy.com/JudgeOnline/problem.php?id=1602

一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle。

显然我没有看到树的性质,树边只有n-1,且一个点一定能到达另一个点

所以我们求出lca,然后记录从根到每个点的距离,然后答案就是d[u]+d[v]-2*d[lca(u, v)]

然后blabla就出来了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1002;
int lca[N], d[N], fa[N], p[N], ihead[N], vis[N], cnt, n, U[N], V[N];
vector<pair<int, int> > q[N];
struct ED { int to, next, w; } e[N<<1];
void add(int u, int v, int w) {
e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v; e[cnt].w=w;
e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u; e[cnt].w=w;
}
const int ifind(const int &x) { return x==p[x]?x:p[x]=ifind(p[x]); }
void tarjan(const int &x) {
p[x]=x;
for(int i=ihead[x]; i; i=e[i].next) if(e[i].to!=fa[x]) {
fa[e[i].to]=x; tarjan(e[i].to); p[e[i].to]=x;
}
vis[x]=1;
int t=q[x].size();
rep(i, t) if(vis[q[x][i].first]) lca[q[x][i].second]=ifind(q[x][i].first);
}
void dfs(const int &x, const int &sum) {
d[x]=sum;
for(int i=ihead[x]; i; i=e[i].next) if(e[i].to!=fa[x]) dfs(e[i].to, sum+e[i].w);
}
int main() {
read(n); int cs=getint();
int u, v, w;
rep(i, n-1) {
read(u); read(v); read(w);
add(u, v, w);
}
for1(i, 1, cs) {
read(u); read(v);
U[i]=u; V[i]=v;
q[u].push_back(pair<int, int> (v, i));
q[v].push_back(pair<int, int> (u, i));
}
tarjan(n>>1); dfs(n>>1, 0);
for1(i, 1, cs) {
u=U[i], v=V[i];
int lc=lca[i];
printf("%d\n", d[u]+d[v]-(d[lc]<<1));
}
return 0;
}

Description

N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草。 这n块土地被n-1条边连接。 奶牛可以在边上行走,第i条边连接第Ai,Bi块牧场,第i条边的长度是Li(1<=Li<=10000)。 这些边被安排成任意两头奶牛都可以通过这些边到达的情况,所以说这是一棵树。 这些奶牛是非常喜欢交际的,经常会去互相访问,他们想让你去帮助他们计算Q(1<=q<=1000)对奶牛之间的距离。

Input

*第一行:两个被空格隔开的整数:N和Q *第二行到第n行:第i+1行有两个被空格隔开的整数:AI,BI,LI *第n+1行到n+Q行:每一行有两个空格隔开的整数:P1,P2,表示两头奶牛的编号。

Output

*第1行到第Q行:每行输出一个数,表示那两头奶牛之间的距离。

Sample Input

4 2
2 1 2
4 3 2
1 4 3
1 2
3 2

Sample Output

2
7

HINT

Source

【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)的更多相关文章

  1. bzoj 1602 [Usaco2008 Oct]牧场行走(LCA模板)

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 379  Solved: 216[Submit][Sta ...

  2. BZOJ 1602: [Usaco2008 Oct]牧场行走( 最短路 )

    一棵树..或许用LCA比较好吧...但是我懒...写了个dijkstra也过了.. ---------------------------------------------------------- ...

  3. LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking

    题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...

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

  5. BZOJ 1602: [Usaco2008 Oct]牧场行走 倍增裸题

    Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走, ...

  6. BZOJ 1602 [Usaco2008 Oct]牧场行走 dfs

    题意:id=1602">链接 方法:深搜暴力 解析: 这题刚看完还有点意思,没看范围前想了想树形DP,只是随便画个图看出来是没法DP的,所以去看范围. woc我没看错范围?果断n^2暴 ...

  7. BZOJ 1602 USACO2008 Oct 牧场行走

    翻翻吴大神的刷题记录翻到的... 乍一看是一个树链剖分吓瓜我...难不成吴大神14-10-28就会了树剖?orz... 再一看SB暴力都可过... 然后一看直接树上倍增码个就好了... 人生真是充满着 ...

  8. bzoj 1602: [Usaco2008 Oct]牧场行走【瞎搞】

    本来想爆手速写个树剖,然而快下课了就手残写了了个n方的短小-- 暴力把查询的两个点中深的一个跳上来,加上边权,然后一起跳加边权就行了 #include<iostream> #include ...

  9. 1602: [Usaco2008 Oct]牧场行走

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1211  Solved: 616 [Submit][ ...

随机推荐

  1. make_head,,,pop_head,,,push_head,,,sort_head..

    STL中,有很多的排序函数模板供我们调用,省去我们自己编写一些排序过程的麻烦.本文是一篇关于STL中堆排序的一个介绍. 本文涉及的几个函数如下:make_heap(), push_heap(), po ...

  2. PHPNG (next generation)

    PHPNG (next generation) This page gives short information about development state of a new PHP branc ...

  3. 关于ubuntu配置静态IP 无法正常上网的解决方案

    在ubuntu中配置静态IP后无法正常上网. 解决: 1.在终端执行 vim /etc/network/interfaces 在文件中加入如下内容,网关要写上,我开始一直无法上网就是因为没有配置网关 ...

  4. CheckBoxList1复选框

    循环绑定数据的两个方法: List<string> LIColl = new List<string>(); protected void Page_Load(object s ...

  5. Linux运维

    概要:http://os.51cto.com/art/201312/423616.htm 论坛: http://www.linux360.cn/ https://www.centos.bz/ http ...

  6. Office 2010启动时出现无法验证此应用程序的许可证的解决

    Office 2010启动之后弹出一个窗口提示:Microsoft Office无法验证此应用程序的许可证.修复尝试失败或者已被用户取消.应用程序将立即关闭. 遇到这样的情况,原因是Office的系统 ...

  7. July 19th, Week 30th Tuesday, 2016

    The good seaman is known in bad weather. 惊涛骇浪,方显英雄本色. You can't be afraid to fail. It's the only way ...

  8. July 12th, Week 29th Tuesday, 2016

    When the traveler goes alone he gets acquainted with himself. 独自旅行可以让人更好地了解自己. With other's company, ...

  9. 转:JavaScript事件冒泡简介及应用

    (本文转载自别处) JavaScript事件冒泡简介及应用   一.什么是事件冒泡 在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理 ...

  10. Http协议之Get和Post的区别

    get(默认值)是通过URL传递表单值,post传递的表单值是隐藏到http报文体中,url中看不到.区别(常考):get是通过url传递表单值,post通过url看不到表单域的值:get传递的数据量 ...