[shkola] malko zadachki-zakachki.

Zadichite sa (sravnitelno )lesni i sym se postaral da mogat da se reshat s
teoriqta koqto sa obsyzhdani na shkolata (i s malko mislene estestveno).

-- 
Rangel Dokov


Title: Online Judge. Problemset.
Ural State University Problem Set Archive with Online Judge System

English
Russian
Online Judge
About
Submit Problem
Problem Set
Status (online)
Webboard
FAQ
Links
Photos
Authors
Register
Update your info
Authors ranklist
Online Contests
Current Contest
Scheduled Contests
Archive
Summary Ranklist Ðåéòèíã ðåñóðñîâ "ÓðàëWeb"


Language: English
Anniversary party.
Time Limit: 5 second
Memory Limit: 1000K

Background

   The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor relation forms a tree rooted at the president. Employees are numbered by integer numbers in a range from 1 to N, The personnel office has ranked each employee with a conviviality rating. In order to make the party fun for all attendees, the president does not want both an employee and his or her immediate supervisor to attend.

Problem

   Your task is to make up a guest list with the maximal conviviality rating of the guests.

Input

   The first line of the input contains a number N. 1 <= N <= 6000.
Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that the supervisor relation tree goes.
   Each line of the tree specification has the form

<L> <K>

which means that the K-th employee is an immediate supervisor of L-th employee. Input is ended with the line

0 0

Output

The output file should contain the maximal total rating of the guests.

Sample Input

7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0

Sample Output

5


Problem Source: Ural State University Internal Contest October'2000 Students Session
Author: Bakirov M.



Print     View Problem Statistics     Discuss     Submit

TIMUS © 2000. All rights reserved.
Title: Online Judge. Problemset.
Ural State University Problem Set Archive with Online Judge System

English
Russian
Online Judge
About
Submit Problem
Problem Set
Status (online)
Webboard
FAQ
Links
Photos
Authors
Register
Update your info
Authors ranklist
Online Contests
Current Contest
Scheduled Contests
Archive
Summary Ranklist Ðåéòèíã ðåñóðñîâ "ÓðàëWeb"


Language: English, Russian
Genealogical tree.
Time Limit: 5 second
Memory Limit: 1000K

Background

The system of Martians? blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent as well as ten. Nobody will be surprised by a hundred of children. Martians have got used to this and their style of life seems to them natural.
And in the Planetary Council the confusing genealogical system leads to some embarrassment. There meet the worthiest of Martians, and therefore in order to offend nobody in all of the discussions it is used first to give the floor to the old Martians, than to the younger ones and only than to the most young childless assessors. However, the maintenance of this order really is not a trivial task. Not always Martian knows all of his parents (and there?s nothing to tell about his grandparents!). But if by a mistake first speak a grandson and only than his young appearing great-grandfather, this is a real scandal.

Problem

Your task is to write a program, which would define once and for all, an order that would guarantee that every member of the Council takes the floor earlier than each of his descendants.

Input

The first line of the standard input contains an only number N, 1 <= N <= 100 ? a number of members of the Martian Planetary Council. According to the centuries-old tradition members of the Council are enumerated with the natural numbers from 1 up to N. Further, there are exactly N lines, moreover, the I-th line contains a list of I-th member?s children. The list of children is a sequence of serial numbers of children in a arbitrary order separated by spaces. The list of children may be empty. The list (even if it is empty) ends with 0.

Output

The standard output should contain in its only line a sequence of speakers? numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you are to write to the standard output any of them. At least one such sequence always exists.

Sample Input

5
0
4 5 1 0
1 0
5 3 0
3 0

Sample Output

2 4 5 3 1


Problem Source: Ural State University Internal Contest October'2000 Junior Session
Author: Volkov L.



Print     View Problem Statistics     Discuss     Submit

TIMUS © 2000. All rights reserved.
Title: Online Judge. Problemset.
Ural State University Problem Set Archive with Online Judge System

English
Russian
Online Judge
About
Submit Problem
Problem Set
Status (online)
Webboard
FAQ
Links
Photos
Authors
Register
Update your info
Authors ranklist
Online Contests
Current Contest
Scheduled Contests
Archive
Summary Ranklist Ðåéòèíã ðåñóðñîâ "ÓðàëWeb"


Language: English
Map Colouring
Time Limit: 1 second
Memory Limit: 1000K

We consider a geographical map with N countries numbered from 1 to N (0 < N < 99). For every country we know the numbers of other countries which are connected with its border. From every country we can reach to any other one, eventually crossing some borders. Write a program which determines whether it is possible to colour the map only in two colours - red and blue in such a way that if two countries are connected their colours are different. The colour of the first country is red. Your program must output one possible colouring for the other countries, or show, that such colouring is impossible.

Input

On the first line is written the number N. On the following N lines, the I-th line contains the countries to which the I-th country is connected. Every integer on this line is bigger than I, except the last one which is 0 and marks that no more countries are listed for country I. If a line contains 0, that means that the I-th country is not connected to any other country, which number is larger than I.

Output

The output contains exactly one line. If the colouring is possible, this lines must contain a list of zeros and ones, without any separators between them. The I-th digit in this sequence is the colour of the I-th country. 0 coresponds to red colour, and one - to blue colour. If a colouring is not possible, output the integer -1.

Sample Input

3
2 0
3 0
0

Sample Output

010


Problem Source: Winter Mathematical Festival Varna '2001 Informatics Tournament
Author: Emil Kelevedzhiev



Print     View Problem Statistics     Discuss     Submit

TIMUS © 2000. All rights reserved.

Other related posts: