hdu 2586(LCA+并查集)

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 6734    Accepted Submission(s): 2498

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

Source

ECJTU 2009 Spring Contest

Recommend

lcy

就是一个最近公共祖先问题,tarjan算法,说白了只是一个dfs。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 40010
typedef struct node{
    int v,d;
    int next;
}Node;

int index;
int head[maxn];
Node edge[2*maxn];

int index1;
int head1[maxn];
Node edge1[2*maxn];

int res[210][3];
int f[maxn];
int vis[maxn];
int dis[maxn];
int n,m;

void add(int u,int v,int d){
    index++;
    edge[index].v=v;
    edge[index].d=d;
    edge[index].next=head[u];
    head[u]=index;
}
void add1(int u,int v,int d){
    index1++;
    edge1[index1].v=v;
    edge1[index1].d=d;
    edge1[index1].next=head1[u];
    head1[u]=index1;
}
int find(int x){
    if(x!=f[x]){
        f[x]=find(f[x]);
        return f[x];
    }
    return x;
}
void tarjan(int u){
    vis[u]=1;
    f[u]=u;

    for(int p=head1[u];p;p=edge1[p].next){
        if(vis[edge1[p].v]){
            res[edge1[p].d][2]=find(edge1[p].v);
        }
    }

    for(int p=head[u];p;p=edge[p].next){
        if(vis[edge[p].v]==0){
            dis[edge[p].v]=dis[u]+edge[p].d;
            //if(dis[edge[p].v]){
              //  printf("%d\n",dis[edge[p].v]);
            //}
            tarjan(edge[p].v);
            f[edge[p].v]=u;
        }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);

        memset(head,0,sizeof(head));
        index=0;
        for(int i=1;i<n;i++){
            int u,v,d;
            scanf("%d%d%d",&u,&v,&d);
            add(u,v,d);
            add(v,u,d);
        }

        memset(head1,0,sizeof(head1));
        index1=0;
        for(int i=0;i<m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            res[i][0]=u;
            res[i][1]=v;
            add1(u,v,i);
            add1(v,u,i);
        }
        memset(vis,0,sizeof(vis));
        dis[1]=0;
        tarjan(1);
        //for(int i=1;i<=n;i++){
          //  printf("%d ",dis[i]);
        //}
        //printf("\n");
        for(int i=0;i<m;i++){
            printf("%d\n",dis[res[i][0]]+dis[res[i][1]]-2*dis[res[i][2]]);
        }
    }
    return 0;
}
时间: 2024-10-11 22:25:52

hdu 2586(LCA+并查集)的相关文章

Hdu 2473(并查集删除操作) Junk-Mail Filter

有木有很吊 加强 加强版   啊  ,看了都不敢做了   ,后来先做了食物链这个我还是看过的,但还是A不掉,没明白神魔意思 ,总而言之,大牛的博客是个好东西,我就那么看了一下,还是不懂怎莫办啊,哎,就那样就A掉了....... 今天我们来谈一下这个并查集的删除操作,根据我对大牛的理解啊,这个并查集的删除操作并不是把原来的节点删除掉,而是用一个替身替掉,现在的这个点只是用作桥梁的作用,即是无用的,del  ,,,del  ,,,,删除,那些被删掉的就从n开始给他们一个地址,然后即如下代码所示 #i

HDU 4496 D-City(并查集,逆思维)

题目 熟能生巧...常做这类题,就不会忘记他的思路了... //可以反过来用并查集,还是逐个加边,但是反过来输出...我是白痴.....又没想到 //G++能过,C++却wa,这个也好奇怪呀... #include<stdio.h> #include<string.h> int fx,fy,r,bin[10010]; int x[100010],y[100010],n,m,i,count,ans[100010],j; int find(int x) { //改成这样就不会超时了么好

HDU 1272 简单并查集

小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24915    Accepted Submission(s): 7641 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 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&quo

hdu 3038 (并查集)

题目大意: 给出m个询问,问[l,r]之间的和   ,求出有多少次询问不和之前的矛盾的. 思路分析: 用并查集记录当前节点到根节点的和. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 222222 using namespace std; int set[maxn]; int sum[maxn]; int find(int

【CodeForces】827 D. Best Edge Weight 最小生成树+倍增LCA+并查集

[题意]给定n个点m条边的带边权无向连通图,对每条边求最大边权,满足其他边权不变的前提下图的任意最小生成树都经过它.n,m<=2*10^5,1<=wi<=10^9. [算法]最小生成树+倍增LCA+并查集 [题解]首先求出图的一个最小生成树,则所有边分成树边和非树边. 对于非树边(u,v),假设u和v在最小生成树上的路径的最大边权Max,那么一定满足w(u,v)<=Max /////////////////////////////////////// 原文地址:https://ww

Hdu 5458 Stability (LCA + 并查集 + 树状数组 + 缩点)

题目链接: Hdu 5458 Stability 题目描述: 给出一个还有环和重边的图G,对图G有两种操作: 1 u v, 删除u与v之间的一天边 (保证这个边一定存在) 2 u v, 查询u到v的路径上有几条桥. 解题思路: 这个题目有很多次操作,包含查询和删边两类,首先想到的是连通分量加缩点.如果按照顺序来,删边时候求桥就是问题了.所以可以离线处理,然后一边记录答案一边加边缩点. 对于一个图,把连通分量缩成一个点后,这个图就成为了一棵树, 然后深度差就等于桥的数目.查询的时候对于(u, v)

HDU 4297--One and One Story(LCA&amp;并查集)

One and One Story Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)Total Submission(s): 1049 Accepted Submission(s): 459 Problem Description Have you ever played the romantic Flash game, "One and One Story"?1 In

HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

题目大意:给一个N个点M条边的无向图,有Q个询问:1.删掉a.b之间所存在的边:2.询问有多少条边,单独删掉之后a与b不再连通. 思路:脑洞大开. 对于询问,首先想到的就是a与b之间有多少桥(割边),然后想到双连通分量,然而删边是个坑爹的问题,于是我们离线倒着来,把删边变成加边. 双连通分量这种东西呢,其实缩点连起来之后,就是一棵树辣. 然后询问两个点的时候,设根到点x的距离为dep[x],a.b的最近公共祖先为lca(a, b),那么询问query(a, b) = dep[a] + dep[b