HDU2586-How far away ?(LCA)

How far away ?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19536 Accepted Submission(s): 7661

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
題目:HDU2586
題意:給你一張圖,詢問兩個點的最短距離。
思路:看到這題本向用Flody直接跑一遍算了,結果發現n<=40000,果斷放棄,然後發現詢問次數只有200,寫線段樹太浪費,用LCA剛好,先求出最近公共祖先,然後答案等於兩個人到公共祖先的距離之和。
AC代碼:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const int maxn=40005;
const double eps=1e-4;
int p[maxn],num,f[maxn],n,vis[maxn],root[maxn],ps[maxn],nums,cont,m;
struct edge
{
    int en,next,len;
} e[5*maxn],es[2*maxn];//正向邊和反向邊
struct ans
{
    int lat,ss;//ss記錄詢問的順序,a[i].lat表示詢問i與lat的距離
    ll que;
    friend bool operator< (ans c, ans b)
    {
        return c.ss<b.ss;
    }
}a[205];
vector<ans>ask[maxn];
void init()
{
    met(p,-1);
    met(ps,-1);
    met(root,0);
    met(vis,0);
    met(a,0);
    for(int i=1; i<=n; i++)f[i]=i,ask[i].clear();
    num=0;
    cont=0;
    nums=0;
}
int finds(int u)
{
    return f[u]==u?f[u]:finds(f[u]);
}
ll findpass(int x,int pass)//計算到公共祖先的距離
{
    ll res=0;
    if(x==pass)return res;
    for(int i=ps[x];i!=-1;i=es[i].next)
    {
        if(es[i].en==f[x])res+=es[i].len+findpass(f[x],pass);
    }
    return res;
}
void unions(int u,int v)
{
    int x=finds(u);
    int y=finds(v);
    if(x!=y)
    {
        f[y]=x;
    }
}
void add(int st,int en,int len)
{
    e[num].en=en;
    e[num].len=len;
    e[num].next=p[st];
    p[st]=num++;
}
void adds(int st,int en,int len)
{
    es[nums].en=en;
    es[nums].len=len;
    es[nums].next=ps[st];
    ps[st]=nums++;
}
void tarjan(int u)
{
    for(int i=p[u]; i!=-1; i=e[i].next)
    {
        int v=e[i].en;
        tarjan(v);
        unions(u,v);
    }
    vis[u]=1;
    int sizee=ask[u].size();
    for(int i=0; i<sizee; i++)
    {
        int v=ask[u][i].lat;
        if(vis[v])
        {
            int pass=finds(v);//最近公共祖先
            a[cont].que=findpass(u,pass)+findpass(v,pass);
            a[cont++].ss=ask[u][i].ss;
        }
    }
}
int main()
{
    int t;
    scan(t);
    while(t--)
    {
        scann(n,m);
        init();
        for(int i=1; i<n; i++)
        {
            int x,y,z;
            scannn(x,y,z);
            add(x,y,z);
            adds(y,x,z);
            root[y]=1;
        }
        for(int i=1; i<=m; i++)
        {
            int x,y;
            scann(x,y);
            ans o;
            o.lat=y;
            o.ss=i;
            ask[x].push_back(o);
            o.lat=x;
            ask[y].push_back(o);
        }
        for(int i=1; i<=n; i++)
        {
            if(!root[i])
            {
                tarjan(i);
                break;
            }
        }
        sort(a,a+cont);
        for(int i=0;i<cont;i++)prinl(a[i].que);//按照詢問順序排序
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章