Cod sursa(job #1535909)

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

using namespace std;

int m[17][100005], 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[poz][next];
     //       poz = 0;
        }
        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[0][b] = a;
    }
    for (int j = 1; j < 17; j++) {
        for (int i = 1; i <= n; i++) {
          m[j][i] = m[j - 1][m[j - 1][i]];
        }
    }
    for (int i = 1; i <= n; i++) {
        int ans = f(i);
        cout << ans << " ";
    }

    return 0;
}