Pagini recente » Cod sursa (job #1606927) | Cod sursa (job #1807587) | Cod sursa (job #272794) | Cod sursa (job #989018) | Cod sursa (job #794144)
Cod sursa(job #794144)
#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];
bool mark [MAX_SIZE];
bool nodes [MAX_SIZE];
int n, root;
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, int depth)
{
if (k[node] && depth)
{
monkeys[node] = depth / k[node];
if (depth % k[node])
++monkeys[node];
}
for (struct edge *iterator(graph[node]) ; iterator ; iterator = iterator->next)
{
if (mark[iterator->vertex])
continue;
mark[iterator->vertex] = true;
if (k[iterator->vertex])
DepthFirstSearch(iterator->vertex,depth + 1);
else
DepthFirstSearch(iterator->vertex,0);
}
}
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,0);
print();
return 0;
}