Cod sursa(job #226354)

Utilizator GavrilaVladGavrila Vlad GavrilaVlad Data 1 decembrie 2008 15:44:16
Problema Atac Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.76 kb
#include <stdio.h>
#include <vector>

using namespace std;

long r[17][32010], q[17][32010], f[32010], g[32010];
long n, i, j, k, m, p, x, y, a, b, c, d, xa, xb, st, a1, a2, b1, b2, maxi, s, o;
vector <long> v[32010], w[32010];

void df(long x, long u)
{
    long y;
    f[x]=1;
    for(y=0; y<v[x].size(); y++)
    {
        s=v[x][y];
        if(f[s]==0)
        {
            q[0][s]=w[x][y];
            r[0][s]=x;
            df(s, u+1);
        }
    }
    g[x]=u;
}   

long mini(long a, long b)
{
    if(a<b) return a;
    return b;
}

int main()
{
    freopen("atac.in", "r", stdin);
    freopen("atac.out", "w", stdout);
    scanf("%d %d %d", &n, &m, &p);
    for(i=2; i<=n; i++)
    {
        scanf("%d %d", &xa, &xb);
        v[i].push_back(xa);
        w[i].push_back(xb);
        v[xa].push_back(i);
        w[xa].push_back(xb);
    }
    df(1, 1);
    for(i=1; 1<<i<n; i++)
    {
        for(j=1; j<=n; j++)
        {
            st=r[i-1][j];
            r[i][j]=r[i-1][st];
            q[i][j]=min(q[i-1][j], q[i-1][st]);
        }
    }
    o=13;
    scanf("%d %d %d %d %d %d", &x, &y, &a, &b, &c, &d);
    for(i=1; i<=m; i++)
    {
        a1=x;
        a2=y;
        maxi=1000000;
        if(x==y) maxi=0;
        else
        {
            if(g[a1]>g[a2])
            {
                s=o;
                while(s>=0)
                {
                    st=r[s][a1];
                    if(g[st]>=g[a2])
                    {
                        maxi=mini(maxi, q[s][a1]);
                        a1=st;
                    }
                    s--;
                }
            }
            else
            if(g[a2]>g[a1])
            {
                s=o;
                while(s>=0)
                {
                    st=r[s][a2];
                    if(g[st]>=g[a1])
                    {
                        maxi=mini(maxi, q[s][a2]);
                        a2=st;
                    }
                    s--;
                }
            }
            if(a1!=a2)
            {
                s=o;
                while(s>=0)
                {
                    b1=r[s][a1];
                    b2=r[s][a2];
                    if(b1!=b2)
                    {
                        maxi=mini(maxi, q[s][a1]);
                        maxi=mini(maxi, q[s][a2]);
                        a1=b1;
                        a2=b2;
                    }
                    s--;
                }
                maxi=mini(maxi, q[0][a1]);
                maxi=mini(maxi, q[0][a2]);
            }
        }   
        if(i>=m-p+1) printf("%d\n", maxi); 
        x=(x*a+y*b)%n+1;
        y=(y*c+maxi*d)%n+1;
    }
    return 0;
}