Cod sursa(job #2558680)

Utilizator teomdn001Moldovan Teodor teomdn001 Data 26 februarie 2020 18:55:00
Problema Cerere Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <vector>
#define VVI vector <vector<int> >
using namespace std;

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

VVI G;
int n;
int stramosi[100001], rez[100001];
void Citire();
void Dfs(int nod, int pas, int fix);

int main()
{
    Citire();
    for (int i = 1; i <= n; ++i)
    {
        Dfs(i, 0, i);
        fout << rez[i] << ' ';
    }
}

void Dfs(int nod, int pas, int fix)
{
    if (stramosi[nod] == 0)
    {
        rez[fix] = pas;
        return;
    }
    else
    {
        int tata = G[nod][stramosi[nod] - 1];
        Dfs(tata, pas + 1, fix);
    }
}

void Citire()
{
    fin >> n;
    G = VVI(n + 1);
    for (int i = 1; i <= n; ++i)
        fin >> stramosi[i];

    int x, y;
    while (fin >> x >> y)
    {
        G[y].push_back(x);
        for (unsigned int i = 0; i < G[x].size(); ++i)
            G[y].push_back(G[x][i]);
    }
}