Clockwork Bomb | 프로그래밍의 벗 PivotOJ
PivotOJ

Clockwork Bomb

시간 제한: 2500ms메모리 제한: 1024MB출처: MOOI 2015-16 finalBOJ 30815
이 문제는 본문 이미지 일부가 표시되지 않습니다. 텍스트만으로 풀이가 어려울 수 있습니다.

문제

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 nn contacts connected by n1n - 1 wires. Contacts are numbered with integers from 11 to nn. Bomb has a security mechanism that ensures the following condition: if there exist k2k \geq 2 contacts c1,c2,,ckc_1, c_2, \ldots, c_k forming a circuit, i. e. there exist kk distinct wires between contacts c1c_1 and c2c_2, c2c_2 and c3c_3, \ldots, ckc_k and c1c_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 22. 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 n2n - 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 nn (2n5000002 \leq n \leq 500\,000), the number of contacts.

Each of the following n1n - 1 lines contains two of integers xix_i and yiy_i (1xi,yin1 \leq x_i, y_i \leq n, xiyix_i \neq y_i) denoting the contacts currently connected by the ii-th wire.

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

출력

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

In each of the following kk lines output four integers aia_i, bib_i, cic_i, did_i meaning that on the ii-th step it is neccesary to unplug the wire connecting the contacts aia_i and bib_i and plug it to the contacts cic_i and did_i. Of course the wire connecting contacts aia_i and bib_i should be present in the scheme.

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

힌트

Picture with the clarification for the sample tests:

[이미지 1]

예제

예제 1

입력
3
1 2
2 3
1 3
3 2
출력
1
1 2 1 3

예제 2

입력
4
1 2
2 3
3 4
2 4
4 1
1 3
출력
3
1 2 1 3
4 3 4 1
2 3 2 4
이 문제는 채점 준비 중입니다. 테스트 데이터가 확보되면 제출이 가능합니다.