Logo
(追記) (追記ここまで)

30815번 - Clockwork Bomb 스페셜 저지다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2.5 초 1024 MB111100.000%

문제

My name is James diGriz, I'm the most clever robber and treasure hunter in the whole galaxy. There are books written about my adventures and songs about my operations, though you were able to catch me up in a pretty awkward moment.

I was able to hide from cameras, outsmart all the guards and pass numerous traps, but when I finally reached the treasure box and opened it, I have accidentally started the clockwork bomb! Luckily, I have met such kind of bombs before and I know that the clockwork mechanism can be stopped by connecting contacts with wires on the control panel of the bomb in a certain manner.

I see $n$ contacts connected by $n - 1$ wires. Contacts are numbered with integers from 1ドル$ to $n$. Bomb has a security mechanism that ensures the following condition: if there exist $k \geq 2$ contacts $c_1, c_2, \ldots, c_k$ forming a circuit, i. e. there exist $k$ distinct wires between contacts $c_1$ and $c_2,ドル $c_2$ and $c_3,ドル $\ldots,ドル $c_k$ and $c_1,ドル then the bomb immediately explodes and my story ends here. In particular, if two contacts are connected by more than one wire they form a circuit of length 2ドル$. It is also prohibited to connect a contact with itself.

On the other hand, if I disconnect more than one wire (i. e. at some moment there will be no more than $n - 2$ wires in the scheme) then the other security check fails and the bomb also explodes. So, the only thing I can do is to unplug some wire and plug it into a new place ensuring the fact that no circuits appear.

I know how I should put the wires in order to stop the clockwork. But my time is running out! Help me get out of this alive: find the sequence of operations each of which consists of unplugging some wire and putting it into another place so that the bomb is defused.

입력

The first line of the input contains $n$ (2ドル \leq n \leq 500,000円$), the number of contacts.

Each of the following $n - 1$ lines contains two of integers $x_i$ and $y_i$ (1ドル \leq x_i, y_i \leq n,ドル $x_i \neq y_i$) denoting the contacts currently connected by the $i$-th wire.

The remaining $n - 1$ lines contain the description of the sought scheme in the same format.

출력

The first line should contain $k$ ($k \geq 0$) --- the minimum number of moves of unplugging and plugging back some wire required to defuse the bomb.

In each of the following $k$ lines output four integers $a_i,ドル $b_i,ドル $c_i,ドル $d_i$ meaning that on the $i$-th step it is neccesary to unplug the wire connecting the contacts $a_i$ and $b_i$ and plug it to the contacts $c_i$ and $d_i$. Of course the wire connecting contacts $a_i$ and $b_i$ should be present in the scheme.

If there is no correct sequence transforming the existing scheme into the sought one, output -1.

제한

예제 입력 1

3
1 2
2 3
1 3
3 2

예제 출력 1

1
1 2 1 3

예제 입력 2

4
1 2
2 3
3 4
2 4
4 1
1 3

예제 출력 2

3
1 2 1 3
4 3 4 1
2 3 2 4

노트

Picture with the clarification for the sample tests:

출처

Olympiad > Moscow Open Olympiad in Informatics > Moscow Open Olympiad in Informatics 2015-16 > Day 1 Python번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /