Mai intai trebuie sa te autentifici.
Cod sursa(job #2283809)
| Utilizator | Data | 15 noiembrie 2018 21:43:02 | |
|---|---|---|---|
| Problema | Cerere | Scor | 60 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva de probleme | Marime | 0.67 kb |
#include <iostream>
#include <fstream>
#include <vector>
const int NMAX = 1e5 + 5;
using namespace std;
ifstream fin("cerere.in");
ofstream fout("cerere.out");
int n;
int c[NMAX], val[NMAX];
bool use[NMAX];
vector < int > v;
vector < int > g[NMAX];
void dfs(int nod){
v.push_back(nod);
if(c[nod])
val[nod] = 1 + val[v[v.size() - 1 - c[nod]]];
for(int i : g[nod])
dfs(i);
v.pop_back();
}
int main(){
fin >> n;
for(int i = 1; i <= n; i++)
fin >> c[i];
int x, y;
while(fin >> x >> y)
g[x].push_back(y), use[y] = 1;
fin.close();
for(int i = 1; i <= n; i++)
if(!use[i])
dfs(i);
for(int i = 1; i <= n; i++)
fout << val[i] << ' ';
fout.close();
return 0;
}