• hdu 2586 How far away ? Lca的模板了、、


    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 <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <vector>
    #include <stack>
    #include <iostream>
    #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<n;i++)
           {
               scanf("%d %d %d",&a,&b,&k);
               edge[e1].to=b;
               edge[e1].dis=k;
               edge[e1].next=head[a];
               head[a]=e1;
               e1++;

               edge[e1].to=a;
               edge[e1].dis=k;
               edge[e1].next=head[b];
               head[b]=e1;
               e1++;
           }
           for(i=1;i<=m;i++)
           {
               scanf("%d %d",&a,&b);
               qedge[e2].to=b;
               qedge[e2].next=qhead[a];
               qhead[a]=e2;
               e2++;

               qedge[e2].to=a;
               qedge[e2].next=qhead[b];
               qhead[b]=e2;
               e2++;
           }
           for(i=1;i<=n;i++)
            f[i]=i,visit[i]=false;
           dis[1]=0;
           LCA(1);
           for(i=0;i<e2;i+=2)
            {
                printf("%I64d\n",qedge[i].dis);
            }
        }
        return 0;
    }

  • 相关阅读:
    asp.net 上传文件 显示进度条
    .net 无刷新的几种模式的开发
    使用ASP.NET 2.0提供的WebResource管理资源
    sqlserver2005 递归查询
    利用触发器对某个表的操作过程(删除、修改、插入)
    Sqlserver 先分页再查询
    关于左右滚动,控制某几列不动
    几个不错的SQl语句
    怎样用.net读出Excel的多个Sheet
    将DELPHI数据库连接写进INI配置文件中
  • 原文地址:https://www.cnblogs.com/372465774y/p/3009320.html
Copyright © 2020-2023  润新知