ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1471. Distance in the Tree

here is my code. can you help me?
Posted by abc 26 Sep 2012 11:23
I use rools of tree. and i have crashed test#2
==>
# include <stdio.h>

long n;
long u[500005],v[500005],w[500005];
long m;
long x[750075],y[750075];
long t[100005][5];
long q[200],s[200];

int main()
{
    scanf("%ld",&n);

    for(int i=0;i<n-1;i++)
    {
        scanf("%ld %ld %ld",&u[i],&v[i],&w[i]);

        if(u[i]<v[i])
        {
            if(v[i]%2==0)
            t[u[i]][0]=w[i];

            else
            t[u[i]][1]=w[i];
        }

        else
        {
            if(u[i]%2==0)
            t[v[i]][0]=w[i];

            else
            t[v[i]][1]=w[i];
        }
    }

    int r=0,d=0,e=0;

    while(e<17)
    {
        q[e]=r;
        s[e]=d;
        r=r*2+1;
        d=d*2+2;
        e++;
    }

    scanf("%ld",&m);

    for(int i=0;i<m;i++)
    scanf("%ld %ld",&x[i],&y[i]);

    for(int i=0;i<m;i++)
    {
        long sum=0;

        while(x[i]!=y[i])
        {
            for(int j=e;j>=0;j--)
            {
                if(x[i]>=q[j] && x[i]<=s[j])
                {
                    int a;
                    if(x[i]%2==0)
                    {
                        a=x[i]/2-1;
                        sum+=t[a][0];
                    }

                    else
                    {
                        a=x[i]/2;
                        sum+=t[a][1];
                    }
                    x[i]=a;
                    break;
                }

                if(y[i]>=q[j] && y[i]<=s[j])
                {
                    int a;
                    if(y[i]%2==0)
                    {
                        a=y[i]/2-1;
                        sum+=t[a][0];
                    }

                    else
                    {
                        a=y[i]/2;
                        sum+=t[a][1];
                    }
                    y[i]=a;
                    break;
                }

            }
        }

        printf("%ld\n",sum);
    }
return 0;
}

Edited by author 26.09.2012 11:23