首页 > 其他分享 >hdu:How far away ?(树链剖分)

hdu:How far away ?(树链剖分)

时间:2023-05-15 20:02:16浏览次数:41  
标签:hdu 剖分 idx far house int each answer line

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(树链剖分)

用树链剖分的方式求LCA,用edg数组保留点到父结点的距离,dis数组保存点到top结点的距离,在求LCA的过程中求出答案

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5;
int h[N],e[N],ne[N],c[N],idx;
int s[N],d[N],f[N],son[N],top[N],dis[N],edg[N];
void add(int u,int v,int w)
{
    e[++idx]=v;
    ne[idx]=h[u];
    c[idx]=w;
    h[u]=idx;
}
void dfs1(int x,int fa,int w)
{
    s[x]=1;d[x]=d[fa]+1;
    son[x]=0;f[x]=fa;edg[x]=w;
    for(int i=h[x];~i;i=ne[i])
    {
        int j=e[i];
        if(j==fa) continue;
        dfs1(j,x,c[i]);
        s[x]+=s[j];
        if(s[son[x]]<s[j]) son[x]=j;
    }
}
void dfs2(int x,int t)
{
    top[x]=t;
    if(x!=t)
    dis[x]=dis[f[x]]+edg[x];
    if(son[x]!=0) dfs2(son[x],t);
    for(int i=h[x];~i;i=ne[i])
    {
        int j=e[i];
        if(j!=f[x]&&j!=son[x]) dfs2(j,j);
    }
}
int lca(int u,int v)
{
    int ans=0;
    while(top[u]!=top[v])
    {
        if(d[u]<d[v]) swap(u,v);
        ans+=dis[u]+edg[top[u]];
        u=f[top[u]];
    }
    ans+=abs(dis[u]-dis[v]);
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        memset(h,-1,sizeof h);
        idx=0;
        int n,m;
        cin>>n>>m;
        for(int i=0;i<n-1;++i)
        {
            int u,v,w;
            cin>>u>>v>>w;
            add(u,v,w);
            add(v,u,w);
        }
        dfs1(1,0,0);
        dfs2(1,1);
        for(int i=0;i<m;++i)
        {
            int a,b;
            cin>>a>>b;
            cout<<lca(a,b)<<'\n';
        }
        
    }
}

标签:hdu,剖分,idx,far,house,int,each,answer,line
From: https://www.cnblogs.com/ruoye123456/p/17402901.html

相关文章

  • hdu:Arbitrage(最短路变形)
    ProblemDescriptionArbitrageistheuseofdiscrepanciesincurrencyexchangeratestotransformoneunitofacurrencyintomorethanoneunitofthesamecurrency.Forexample,supposethat1USDollarbuys0.5Britishpound,1Britishpoundbuys10.0......
  • hdu:六度分离(最短路)
    ProblemDescription1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(smallworldphenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人,即只用6个人就可以将他们联系在一起,因此他的理论也被称为“六度分离”理论(sixdegreesofsepa......
  • iOS 5中safari带来的新特性
    iOS5中safari带来的新特性作者:神飞mingelz分享到:随着iPhone4s的发布,iOS5也正是发布了,增加了很多很有用的新特性,不过对于前端开发者来说,最关心的还是浏览器,让我们来看一下苹果为iOS5中的safari新增了哪些新特性吧。CSS终于支持position:fixed了;支持overflow:scroll了......
  • hdu:求和(逆元)
    ProblemDescriptionApex实验室里培养了很多种类的细菌。细菌的繁殖遵循如下规则:第k种细菌在第t个单位时间内新增的数量为k^t。例如,k=2,t=4时,第种细菌的总数为2+4+8+16。现在,实验室里一共有n种细菌,分别为1,2,3,…,n。在第1单位时间结束后,第k种细菌的个数为k个。求第m个单位时......
  • 利用MATLAB GUI设计平台,设计多算法雷达一维恒虚警检测CFAR可视化界面,通过选择噪声类型
    利用MATLABGUI设计平台,设计多算法雷达一维恒虚警检测CFAR可视化界面,通过选择噪声类型、目标类型、算法类型,手动输入相关参数,可视化显示噪声波形与目标检测的回波-检测门限波形图ID:94200670614830016......
  • hdu:XOR(线性基)
    XOR数学-线性基设原集为S,线性基为P,若|S|>|P|,则异或会出现0。若|P|==n,则会产生2^n个不同数(包括0)而线性基不包含0元素,故若|S|中元素可以异或出0,k需要自减点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e4+10;lla[N......
  • (hdu step 3.1.7)Children’s Queue(求n个人站在一起有m个人必须是连在一起的方案数)
    题目:Children’sQueueTimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):853AcceptedSubmission(s):479 ProblemDescriptionTherearemanystudentsinPHTSchool.Oneday,theheadmasterwhosen......
  • (hdu step 3.2.1)Max Sum(简单dp:求最大子序列和、起点、终点)
    题目:MaxSumTimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):1390AcceptedSubmission(s):542 ProblemDescriptionGivenasequencea[1],a[2],a[3]......a[n],yourjobistocalculatethemaxsu......
  • (hdu step 3.1.3)母牛的故事(简单递推)
    题目:母牛的故事TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):659AcceptedSubmission(s):481 ProblemDescription有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小......
  • (hdu step 3.2.3)Super Jumping! Jumping! Jumping!(DP:求最长上升子序列的最大和)
    题目:SuperJumping!Jumping!Jumping!TimeLimit:2000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):896AcceptedSubmission(s):518 ProblemDescriptionNowadays,akindofchessgamecalled“SuperJumping!......