Pagini recente » Monitorul de evaluare | Cod sursa (job #559920) | Cod sursa (job #1584744) | Autentificare | Cod sursa (job #794150)
Cod sursa(job #794150)
#include <cstdio>
const int MAX_SIZE(100001);
struct edge
{
int vertex;
struct edge *next;
} *graph [MAX_SIZE];
int k [MAX_SIZE];
int monkeys [MAX_SIZE];
int stack [MAX_SIZE];
bool mark [MAX_SIZE];
bool nodes [MAX_SIZE];
int n, root, depth;
inline void read (void)
{
std::freopen("cerere.in","r",stdin);
std::scanf("%d",&n);
for (int *iterator(k + 1), *end(k + n) ; iterator <= end ; ++iterator)
std::scanf("%d",iterator);
int a, *a_ptr(&a), b, *b_ptr(&b);
struct edge *new_edge;
for (int i(0) ; i < n ; ++i)
{
std::scanf("%d%d",a_ptr,b_ptr);
new_edge = new struct edge;
new_edge->vertex = b;
new_edge->next = graph[a];
graph[a] = new_edge;
nodes[b] = true;
}
bool *iterator(nodes + 1);
while (*iterator)
++iterator;
root = iterator - nodes;
std::fclose(stdin);
}
void DepthFirstSearch (int node)
{
stack[depth] = node;
if (k[node])
monkeys[node] = monkeys[stack[depth - k[node]]] + 1;
++depth;
for (struct edge *iterator(graph[node]) ; iterator ; iterator = iterator->next)
{
if (mark[iterator->vertex])
continue;
mark[iterator->vertex] = true;
DepthFirstSearch(iterator->vertex);
}
--depth;
}
inline void print (void)
{
std::freopen("cerere.out","w",stdout);
for (int *iterator(monkeys + 1), *end(monkeys + n) ; iterator <= end ; ++iterator)
std::printf("%d ",*iterator);
std::putchar('\n');
std::fclose(stdout);
}
int main (void)
{
read();
DepthFirstSearch(root);
print();
return 0;
}