Laser Strike

시간 제한3초메모리 제한2048 MB

요약
Ann이 트리의 리프 제거 순서와 이진 메시지를 정하고, Kathrin은 매 턴 Ann이 알려주는 간선만으로 그 순서를 그대로 재현해야 한다.
난이도

어려움10점 중 9점

유형
트리, 그리디, 조합론, 구현
정답자
아직 제출이 없습니다

문제

Ann and her friend Kathrin have recently discovered a new board game that has become their favourite: Laser Strike. In this game, the two players work together to remove NN pieces from the board. The game runs in two phases. The catch is that Kathrin will not have complete information about the game. In order to win the game, Ann and Kathrin have to work together, while communicating as little as possible.

There are NN unique pieces on the board, numbered from 00 to N−1N-1. Both players can see these pieces. There are also N−1N-1 connections between pairs of pieces, such that it is possible to reach any piece from any other piece by following these connections. In other words, these connections form a tree. Only Ann can see these connections; Kathrin does not know them.

In the first phase of the game, Ann decides on an order ℓ_0,ℓ_1,…,ℓ_N−2\ell\_0, \ell\_1, \ldots, \ell\_{N-2} in which pieces should be removed, until there is only one left. This order will be kept secret from Kathrin. If she can replicate it, they will win the game. The removal of pieces must satisfy the following rule: every time a piece is removed, it must be connected with exactly one remaining piece. In other words, the removed piece must be a leaf of the tree formed by the remaining pieces and itself. (After the N−1N-1 pieces have been removed, the last piece is removed automatically and the players win.) Ann must choose an order that corresponds to the above rule.

Ann will also write down a message to Kathrin, in the form of a binary string. Ann can choose how long this message is -- but the shorter it is, the more points they get.

After that, the second phase of the game starts. The goal of the game is for Kathrin to remove N−1N-1 pieces from the board in the order ℓ_0,ℓ_1,…,ℓ_N−2\ell\_0, \ell\_1, \ldots, \ell\_{N-2}. She will make N−1N-1 moves. Before move ii, Ann tells Kathrin a pair of integers aa, bb with the following properties:

  • a<ba < b;
  • there is still a pair of directly connected pieces with numbers aa and bb; and
  • either aa or bb is the correct piece ℓ_i\ell\_i that should be removed in this move.

Note that for Ann the connection (a,b)(a,b) is uniquely determined by the leaf ℓ_i\ell\_i in the current tree.

Kathrin then removes either aa or bb from the board. If this was the correct piece -- that is, ℓ_i\ell\_i -- they keep playing. Otherwise they lose the game.

Your task is to implement both Ann's and Kathrin's strategies so that they win the game.

Your program will be scored depending on the length of the message that Ann writes in the first phase of the game.

제한

  • N=1,000N = 1\\,000.
  • 0≤a<b≤N−10 \le a < b \le N-1 for all connections.

예제2

  1. 예제 1

    입력
    1 7
    0 1
    1 2
    2 3
    0 4
    0 6
    1 5
    
    
    
    
    
    
    
    
    예상 출력
    
    
    
    
    
    
    
    0110
    5
    3
    2
    6
    4
    0
    
  2. 예제 2

    입력
    2 7
    0110
    1 5
    
    2 3
    
    1 2
    
    0 6
    
    0 4
    
    0 1
    
    
    예상 출력
    
    
    
    5
    
    3
    
    2
    
    6
    
    4
    
    0