【CF650E】 Clockwork Bomb

题目

题目描述
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 n contacts connected by n-1 n−1 wires. Contacts are numbered with integers from 1 1 to n n . Bomb has a security mechanism that ensures the following condition: if there exist k>=2 k>=2 contacts c_{1},c_{2},…,c_{k} c
1

,c
2

,…,c
k

forming a circuit, i. e. there exist k k distinct wires between contacts c_{1} c
1

and c_{2} c
2

, c_{2} c
2

and c_{3} c
3

, … … , c_{k} c
k

and c_{1} 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 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 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 n ( 2<=n<=500000 2<=n<=500000 ), the number of contacts.

Each of the following n-1 n−1 lines contains two of integers x_{i} x
i

and y_{i} y
i

( 1<=x_{i},y_{i}<=n 1<=x
i

,y
i

<=n , x_{i}≠y_{i} x
i



=y
i

) denoting the contacts currently connected by the i i -th wire.

The remaining n-1 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).

输出格式
The first line should contain k k ( k>=0 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 k lines output four integers a_{i} a
i

, b_{i} b
i

, c_{i} c
i

, d_{i} d
i

meaning that on the i i -th step it is neccesary to unplug the wire connecting the contacts a_{i} a
i

and b_{i} b
i

and plug it to the contacts c_{i} c
i

and d_{i} d
i

. Of course the wire connecting contacts a_{i} a
i

and b_{i} b
i

should be present in the scheme.

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

题意翻译
给出两棵n个节点的树,每次操作可以把第一棵树的一条边删掉然 后连另外两个点,且必须满足每次操作完之后仍是一棵树

问最少需要多少步操作才能把第一棵树变成第二棵树(是完全相 同,并不是同构),并输出方案

n ≤ 500000

输入输出样例
输入 #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:

思路

首先答案显然是[n-1-两棵树共有的边]
那么我们把这些边用并查集合并缩点
考虑这样一个过程,每次找到第一棵树的叶子x,把x和父亲的边删掉,然后连上第二棵树上的x和父亲,这样做显然不会出现环
这个东西也可以用并查集维护。
复杂度O(nα(n))

代码

#include<bits/stdc++.h>
#define rg register
using namespace std;
template < typename T > void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while (!isdigit(c)) f |= (c == '-'),c = getchar();
    while (isdigit(c)) s = s * 10 + (c ^ 48),c = getchar();
    s = f ? -s : s;
}

const int N = 500000 + 10;

int n,ff[2][N],fa[N];
vector < int > e[2][N];
struct node{ int a,b,c,d; };
vector < node > ans;

int findd(int x) { return fa[x] == x ? x : findd(fa[x]); }

void dfs(int x,int u,int f) {
    for (rg int v : e[x][u]) {
        if (v == f) continue;
        ff[x][v] = u,dfs(x,v,u);
    }
}

void dfss(int u,int f) {
    for (rg int v : e[0][u]) {
        if (v == f) continue; dfss(v,u);
        if (u != ff[1][v] && v != ff[1][u])
            ans.pushNback((node) { v,u,findd(v),ff[1][findd(v)] });
    }
}

int main()
{
    read(n);
    for (rg int u,v,i = 1; i < n; ++i) read(u),read(v),e[0][u].pushNback(v),e[0][v].pushNback(u);
    for (rg int u,v,i = 1; i < n; ++i) read(u),read(v),e[1][u].pushNback(v),e[1][v].pushNback(u);
    dfs(0,1,0);
    dfs(1,1,0);
    for (rg int i = 2; i <= n; ++i)
	{
        int u = ff[1][i];
        if (u == ff[0][i] || i == ff[0][u])
            fa[i] = u;
        else
            fa[i] = i;
    }
    dfss(1,0);
    printf("%d\n",(int) ans.size());
    for (rg node x : ans) printf("%d %d %d %d\n",x.a,x.b,x.c,x.d);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值