Cod sursa(job #1535906)

Utilizator FairPlay94George Cioroiu FairPlay94 Data 25 noiembrie 2015 13:18:37
Problema Cerere Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <cstdio>
#include <iostream>
#include <set>
#include <climits>
#include <map>
#include <algorithm>
#include <list>
#include <vector>
#include <utility>

using namespace std;

int m[100005][20], n, v[100005];

int f(int i) {
    if (v[i] == 0) {
        return 0;
    }
    int next = i, poz = 0, aux = v[i];
    while (aux > 0) {
        if (aux % 2 == 1) {
            next = m[next][poz];
            poz = 0;
        } else {
            poz++;
        }
        aux >>= 1;
    }
    return 1 + f(next);
}

int main() {
   // freopen("tt.txt", "r", stdin);
    freopen("cerere.in", "r", stdin);
    freopen("cerere.out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> v[i];
    }
    for (int i = 1; i <= n; i++) {
        int a, b;
        cin >> a >> b;
        m[b][0] = a;
    }
    for (int j = 1; j < 20; j++) {
        for (int i = 1; i <= n; i++) {
            m[i][j] = m[m[i][j - 1]][j - 1];
        }
    }
    for (int i = 1; i <= n; i++) {
        int ans = f(i);
        cout << ans << " ";
    }

    return 0;
}