博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2586 How far away ? Lca的模板了、、
阅读量:4543 次
发布时间:2019-06-08

本文共 2470 字,大约阅读时间需要 8 分钟。

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2522    Accepted Submission(s): 931
Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 
 
Input
First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
 
 
Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 
 
Sample Input
2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1
 
 
Sample Output
10 25 100 100
// 差不多就是Lca的模板了、、 dis[i]代表根节点到i的距离 #include 
#include
#include
#include
#include
#include
#define Max 40001 using namespace std; struct Node {     int to;     int next;     __int64 dis;//本来是 LCA }; int qhead[Max]; Node qedge[522]; int head[Max]; Node edge[Max<<1]; __int64 dis[Max]; int f[Max]; bool visit[Max]; int Find(int x) {     if(f[x]!=x) f[x]=Find(f[x]);     return f[x]; } void LCA(int u) {     visit[u]=true;     int e,v;     for(e=head[u];e!=-1;e=edge[e].next)     {         v=edge[e].to;         if(!visit[v])         {             dis[v]=dis[u]+edge[e].dis;             LCA(v);             f[v]=u;         }     }     for(e=qhead[u];e!=-1;e=qedge[e].next)     {         if(visit[qedge[e].to])         {             int lca=Find(qedge[e].to); //最近公共祖先             qedge[e].dis=dis[u]+dis[qedge[e].to]-(dis[lca]+dis[lca]);             qedge[e^1].dis= qedge[e].dis;         }     } } int main() {     int n,m;     int T;     scanf("%d",&T);     while(T--)     {        int i;        int a,b,k;        int e1=0,e2=0;        scanf("%d %d",&n,&m);        for(i=1;i<=n;i++)         qhead[i]=head[i]=-1;        for(i=1;i

 

转载于:https://www.cnblogs.com/372465774y/archive/2013/04/09/3009320.html

你可能感兴趣的文章
项目管理知识1
查看>>
在window环境下安装Python中的pip
查看>>
A大龙插件官方群3:621816328
查看>>
oi再见,你好明天。
查看>>
2018 Multi-University Training Contest 1 - D Distinct Values (STL+双指针)
查看>>
js学习笔记一-语法结构
查看>>
键盘对应的键值
查看>>
goLang 纳秒转 毫秒 转 英文时间格式
查看>>
微信小程序的坑坑
查看>>
图片轮播(Jquery)
查看>>
hdu 1704 Rank(floyd传递闭包)
查看>>
Educational Codeforces Round 27 G. Shortest Path Problem?(Guass异或线性基)
查看>>
【BZOJ3622】已经没有什么好害怕的了(动态规划+广义容斥)
查看>>
HDOJ 1023 Train Problem II
查看>>
途牛订单的服务化演进
查看>>
软件工程之四则运算
查看>>
ABAP 根据权限显示或隐藏状态栏的按钮
查看>>
跑步计划
查看>>
mvc中使用uploadify批量上传的应用
查看>>
Kibana查询说明
查看>>