CF650E.Clockwork Bomb

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 k>=2k>=2 contacts c1,c2,...,ckc_{1},c_{2},...,c_{k} forming a circuit, i. e. there exist kk distinct wires between contacts c1c_{1} and c2c_{2} , c2c_{2} and c3c_{3} , ...... , 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 ( 2<=n<=5000002<=n<=500000 ), the number of contacts.

Each of the following n1n-1 lines contains two of integers xix_{i} and yiy_{i} ( 1<=xi,yi<=n1<=x_{i},y_{i}<=n , xiyix_{i}≠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.

It is guaranteed that the starting and the ending schemes are correct (i. e. do not contain cicuits nor wires connecting contact with itself).

输出格式

The first line should contain kk ( k>=0k>=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.

输入输出样例

  • 输入#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:

首页