Under construction

Still being built. Things may change

Belt Conveyor

Time limit5sMemory limit1024 MB

Summary
Given a tree of N tables, recover the original direction of every conveyor using at most 30 product-placement queries.
Level

Hard8 of 10

Topics
Tree, Bit manipulation, Implementation
Solved
No attempts yet

Problem

The factory of JOI Co., Ltd. has NN tables, numbered from 0 to N−1N - 1. It also has N−1N - 1 belt conveyors, numbered from 0 to N−2N - 2. Belt conveyor ii (0≤i≤N−20 ≤ i ≤ N - 2) connects table AiA_i and table BiB_i and carries products from one table to the other. The direction of transport is not visible. Ignoring directions, every pair of tables is connected through the conveyors.

IOI-kun is the factory director. He has forgotten the transport direction of every conveyor, so he repeats the following operations several times.

  1. He chooses some conveyors and reverses their transport directions.
  2. He chooses some tables and puts one product on each chosen table.
  3. For every table with a product, one of the following happens simultaneously.
    1. If no conveyor carries products out of the table, nothing happens.
    2. If one or more conveyors carry products out of the table, the product moves along one of those conveyors. It stops at that conveyor's destination and does not move anymore.
  4. IOI-kun checks each table for products. If a table has any, he takes all of them.
  5. For every conveyor reversed in step 1, IOI-kun restores its original direction.

IOI-kun wants to determine the original direction of every conveyor using at most 30 of these operations.

Constraints

All input data satisfy the following conditions.

  • 0≤Ai≤N−10 ≤ A_i ≤ N - 1 (0≤i≤N−20 ≤ i ≤ N - 2).
  • 0≤Bi≤N−10 ≤ B_i ≤ N - 1 (0≤i≤N−20 ≤ i ≤ N - 2).
  • Ignoring directions, every pair of tables is connected through the belt conveyors.

Examples1

  1. Example 1

    Input
    2
    0 1
    
    Expected output
    0 1