「CF650E」Clockwork Bomb
传送门
Luogu
解题思路
显然对于两棵树共有的边,我们不会动它。
考虑第二颗树中有和第一棵树不同的边怎么处理。
我们设 \(fa_1[u],fa_2[u]\) 分别代表 \(u\) 在两棵树中的父亲。
很显然的想法就是对于第一棵树中的边 \(fa_1[u] \rightarrow u\) 没有出现在第二棵树上,那就把这条边换成 \(fa_2[u] \rightarrow u\)
但是我们会发现有可能 \(fa_2[u] \rightarrow u\) 已经被连上了。
那么我们就尝试跳 \(fa_2[u]\) 的父亲知直到可以连边,这个过程可以用一个并查集来搞(预处理出每个点会连向哪个点)。
并且在dfs的过程中先处理子树信息,可以保证不会连出环。
细节注意事项
- 咕咕咕
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <vector>
#define rg register
using namespace std;
template < typename T > inline 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 _ = 500000 + 10;
int n, ff[2][_], fa[_];
vector < int > G[2][_];
struct node{ int a, b, c, d; };
vector < node > ans;
inline int findd(int x) { return fa[x] == x ? x : findd(fa[x]); }
inline void dfs(int x, int u, int f) {
for (rg int v : G[x][u]) {
if (v == f) continue;
ff[x][v] = u, dfs(x, v, u);
}
}
inline void dfss(int u, int f) {
for (rg int v : G[0][u]) {
if (v == f) continue; dfss(v, u);
if (u != ff[1][v] && v != ff[1][u])
ans.push_back((node) { v, u, findd(v), ff[1][findd(v)] });
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n);
for (rg int u, v, i = 1; i < n; ++i) read(u), read(v), G[0][u].push_back(v), G[0][v].push_back(u);
for (rg int u, v, i = 1; i < n; ++i) read(u), read(v), G[1][u].push_back(v), G[1][v].push_back(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;
}
完结撒花 \(qwq\)
「CF650E」Clockwork Bomb的更多相关文章
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
- 「2014-3-13」Javascript Engine, Java VM, Python interpreter, PyPy – a glance
提要: url anchor (ajax) => javascript engine (1~4 articles) => java VM vs. python interpreter =& ...
随机推荐
- Spring Boot 2.x 缓存应用 Redis注解与非注解方式入门教程
Redis 在 Spring Boot 2.x 中相比 1.5.x 版本,有一些改变.redis 默认链接池,1.5.x 使用了 jedis,而2.x 使用了 lettuce Redis 接入 Spr ...
- Vue父组件主动获取子组件的数据和方法
Vue父组件主动获取子组件的数据和方法 https://www.jianshu.com/p/bf88fc809131
- Atcoder Grand Contest 039C(容斥原理,计数DP)
//每次操作相当于将最低位取反加到最高位(N~1位)#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace s ...
- golang的io.copy使用
net/http 下载 在golang中,如果我们要下载一个文件,最简单的就是先用http.get()方法创建一个远程的请求后,后面可使用ioutil.WriteFile()等将请求内容直接写到文件中 ...
- GUI编程与CLI编程
作为一名多年的iOS开发人员,多次触发我思酌“GUI编程与CLI编程”各自的优劣,尤其是在当我为界面交互花费大占比时间时,时常怀疑自己的工作性质,我终究还是为互联网工作的码农,而不是让互联网为我所用的 ...
- HTML常用标签效果展示
HTML常用标签效果展示 一.文本效果 段落1---收到了开发建设看来得更加快乐圣诞节福利肯定是减肥的路上苏里科夫就是打开了飞机都是风口浪尖上的疯狂了大煞风景圣诞快乐的索科洛夫几点上课了关键是低空掠过 ...
- 从npz文件中读取图片并显示的小例子
前提:我把自己的数据集存成了npz的形式,也就是npy的压缩形式.如果电脑上安装了解压软件,双击npz文件的话,会出现每一部分压缩文件的名字例如npz文件的名称为:mnist.npz文件,用好压解压软 ...
- 【转】How to create a test plan
What is a Test Plan? A TEST PLAN is a detailed document that describes the test strategy, objectives ...
- Java 8 有哪些新特性
一.支持 lambda 表达式 例如:查询学生信息,并打印 List<Student> studentList = Student.findAllStudent(); for(Studen ...
- The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest - F.Sequence(打表+线段树)
题意:给你一个长度为$n$的数组,定义函数$f(l,r)=a_{l} \oplus a_{l+1} \oplus...\oplus a_{r}$,$F(l,r)=f(l,l)\oplus f(l,l+ ...