Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

E. Clockwork Bomb

题目连接:

http://www.codeforces.com/contest/650/problem/E

Description

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 ≥ 2 contacts c1, c2, ..., ck forming a circuit, i. e. there exist k distinct wires between contacts c1 and c2, c2 and c3, ..., ck and c1, 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.

Input

The first line of the input contains n (2 ≤ n ≤ 500 000), the number of contacts.

Each of the following n - 1 lines contains two of integers xi and yi (1 ≤ xi, yi ≤ n, xi ≠ yi) 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.

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

Output

The first line should contain k (k ≥ 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 ai, bi, ci, di meaning that on the i-th step it is neccesary to unplug the wire connecting the contacts ai and bi and plug it to the contacts ci and di. Of course the wire connecting contacts ai and bi should be present in the scheme.

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

Sample Input

3

1 2

2 3

1 3

3 2

Sample Output

1

1 2 1 3

Hint

题意

给你两棵树,你只能操作第一棵树,你每次操作是删除一条边,加一条边

但是都不能构成环,然后问你最少多少步。

题解:

可以强行用LCT做动态最小生成树无脑肝过去就好了。

我推荐rng58的做法:

我们从第一棵树的dfs顺序开始考虑,做到第u个点了,v是u的父亲,如果边(v,u)存在在第二棵树,显然我们就不用动这条边。否则的话,我们将u点连向第二棵树u点的父亲fa[u]就好了。

现在有一个问题,如果第二棵树中u点已经连了父亲fa[u]了,怎么办?

把(u,w)这条边变成(find(u),fa[find(u)])就好了。find是并查集,找到第一个在第一棵树不和自己第二棵树fa[u]相连接的点。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e5+7;
vector<int> E[2][maxn];
int fa[2][maxn];
int father[maxn];
int tot=0;
int ans[maxn][4];
void addans(int x,int y,int z,int p)
{
ans[tot][0]=x,ans[tot][1]=y,ans[tot][2]=z,ans[tot][3]=p;
tot++;
}
int fi(int u){
return u != father[u] ? father[u] = fi( father[u] ) : u;
}
bool check(int id,int x,int y)
{
if(x==0||y==0)return false;
if(fa[id][x]==y||fa[id][y]==x)return true;
return false;
}
void dfs(int id,int x,int f)
{
fa[id][x]=f;
for(int i=0;i<E[id][x].size();i++)
{
int v = E[id][x][i];
if(v==f)continue;
dfs(id,v,x);
}
}
void solve(int x,int f)
{
for(int i=0;i<E[0][x].size();i++)
{
int v = E[0][x][i];
if(v==f)continue;
solve(v,x);
if(fa[1][v]!=x&&fa[1][x]!=v)
{
int p = fi(v);
addans(x,v,p,fa[1][p]);
}
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<n;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[0][x].push_back(y);
E[0][y].push_back(x);
}
for(int i=1;i<n;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[1][x].push_back(y);
E[1][y].push_back(x);
}
dfs(0,1,0);
dfs(1,1,0);
for(int i=1;i<=n;i++)
{
if(!check(0,i,fa[1][i]))
father[i]=i;
else
father[i]=fa[1][i];
}
solve(1,0);
cout<<tot<<endl;
for(int i=0;i<tot;i++,cout<<endl)
for(int j=0;j<4;j++)
printf("%d ",ans[i][j]);
}
上一篇:Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】


下一篇:Codeforces Round #345 (Div. 2) A