Cod sursa(job #2568134)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 3 martie 2020 21:02:46
Problema Cerere Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <bits/stdc++.h>
#define FILE_NAME "cerere"
#define fast ios_base :: sync_with_stdio(0); cin.tie(0);
#pragma GCC optimize("O3")
#define NMAX 100000
using namespace std;

ifstream f(FILE_NAME ".in");
ofstream g(FILE_NAME ".out");

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pi;
typedef pair<ld,ld> pct;

const ll inf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;


void add(ll &a , ll b)
{
    a += b;
    a %= mod;
}

void sub(ll &a, ll b)
{
    a = (a - b + mod) % mod;
}

int pd[20][NMAX], t[NMAX], x, y, n, k[NMAX], ans;

int getDAD(int nod, int cat)
{
    ans ++;
    for(int i = 0; i <= 17; ++i)
        if(cat & (1 << i))
            nod = pd[i][nod];
    return nod;
}
int main()
{
    f >> n;
    for(int i = 1; i <= n; ++i)
        f >> k[i];

    for(int i = 1; i < n; ++i)
    {
        f >> x >> y;
        t[y] = x;
    }
    for(int i = 1; i <= n; ++i)
        pd[0][i] = t[i];

    for(int i = 1; i <= 17; ++i)
        for(int j = 1; j <= n; ++j)
            pd[i][j] = pd[i - 1][ pd[i - 1][j] ];
    for(int i = 1; i <= n; ++i)
    {
        int act = i;
        ans = 0;
        while( k[act] != 0 )
            act = getDAD(act, k[act]);
        g << ans << ' ';
    }

    f.close();
    g.close();
    return 0;
}