Cod sursa(job #1808136)

Utilizator CiurezAndreiCiurez Marius-Andrei CiurezAndrei Data 17 noiembrie 2016 13:25:36
Problema Atac Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.54 kb
#include <bits/stdc++.h>

#define INF 100000000

using namespace std;

ifstream fin("atac.in");
ofstream fout("atac.out");

int radicalN;
int ind, N, M, K, x, y, nr, P, Q;
char c;
int niv[32010], T[32010];
pair<int, int> T2[32010], T3[32010];
vector< pair<int, int> > L[32010];
bitset<32010>viz;

inline void dfs(int node, int n1, int level, int maxN1)
{
    viz[node] = 1;
    niv[node] = level;

    if(level % radicalN == 0) n1 = node, maxN1 = INF;

    for(int i = 0; i < L[node].size(); i ++)
    {
        if(viz[ L[node][i].first ] == 0 )
        {
            T2[ L[node][i].first ] = make_pair(node, L[node][i].second);

            T3[ L[node][i].first ].first = n1;
            maxN1 = min(L[node][i].second, maxN1);
            T3[ L[node][i].first ].second = maxN1;

            dfs(L[node][i].first, n1, level + 1, maxN1);
        }
    }
}

int main()
{
    //fin >> N >> M >> K;
    fin >> N >> Q >> P;
    radicalN = sqrt(N);

    for(int i = 2; i <= N; i ++)
    {
        //fin >> v[i].a >> v[i].b  >> v[i].cost;
        int a, c;
        fin >> a >> c;
        L[ a ].push_back(make_pair(i, c));
        L[ i ].push_back(make_pair(a, c));
    }

    dfs(1, 0, 0, INF);
    int x2, y2, A, B, C, D;
    fin >> x2 >> y2 >> A >> B >> C >> D;

    for(int i = 1; i <= Q; i ++)
    {
        int solution = INF;
        x = x2;
        y = y2;

        while(T3[x].first != T3[y].first)
        {
            if(niv[x] > niv[y])
            {
                if(T3[x].second < solution)
                {
                    solution = T3[x].second;
                }
                x = T3[x].first;
            }
            else
            {
                if(T3[y].second < solution)
                {
                    solution = T3[y].second;
                }
                y = T3[y].first;
            }
        }
        while(x != y)
        {
            if(niv[x] > niv[y])
            {
                if(T2[x].second < solution)
                {
                    solution = T2[x].second;
                }
                x = T2[x].first;
            }
            else
            {
                if(T2[y].second < solution)
                {
                    solution = T2[y].second;
                }
                y = T2[y].first;
            }
        }
        if(Q - P < i)
        {
            fout << solution << '\n';
        }
        x2 = (x2*A + y2*B) % N + 1;
        y2 = (y2*C + solution*D) % N + 1;
    }

    return 0;
}