B. 2Trees
time limit per test

0.75 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two trees with the same number of leaves L, your task is to merge the two trees' leaves in a way that ensures the following:

  • The number of colors needed to color the resulting graph such that no two adjacent nodes share the same color is minimum.
  • Each leaf in the first tree is merged into exactly one leaf from the second tree.
  • Each leaf in the second tree is merged into exactly one leaf from the first tree.
  • Nodes other than leaves are not merged.

Note that by merging two leaves a and b, the resulting node will have both edges of a and b.

Input

The first line of input contains one integer N (3 ≤ N ≤ 105), the number of nodes in the first tree.

Then follows N - 1 lines, the ith line contains two integers u and v (1 ≤ u, v ≤ N), the indices of the nodes connected by the ith edge in the first tree.

The next line contains an integer M (3 ≤ M ≤ 105), the number of nodes in the second tree.

Then follows M - 1 lines, the ith line contains two integers u and v (1 ≤ u, v ≤ M), the indices of the nodes connected by the ith edge in the second tree.

It is guaranteed that the two trees will have the same number of leaves L.

Output

On a single line print the number of colors needed to color the resulting graph.

Followed by L lines, the ith line of them contains two integers u and v (1 ≤ u ≤ N)(1 ≤ v ≤ M), the indices of the leaves to be merged, where u is a leaf in the first tree, and v is a leaf in the second tree.

If there’s more than one possible solution, print any of them.

Examples
Input
3
1 2
1 3
3
3 1
2 3
Output
2
2 1
3 2
Input
4
1 2
2 3
3 4
3
3 1
1 2
Output
3
4 2
1 3
Note
  • A tree of N nodes is a connected undirected graph with N - 1 edges.
  • A node is a leaf if and only if it is connected to at most one other node.

In the first sample, the two trees can be illustrated as follows:

After merging node 2 from first tree with node 1 from the second tree, and node 3 from the first tree with node 2 from the second tree, the resulting graph is illustrated in the figure below:

The minimum number of colors required to satisfy the problem constraints is 2.

【分析】给你两棵树,他们的叶子节点个数都为L,现在要将两棵树合并,方法是只合并叶子结点,即一一对应,合并后两个叶子结点就成了一个节点,两棵树即成了

一个有环图。然后给节点染色,相邻节点染不同颜色,问如何合并使得颜色数最少。

首先得想到一点,对于任何一个节点,他是可以与对面任一节点合并的。那么我们考虑一个合并后的一条支路(两个叶子结点合并后形成的),由第一棵树的根节点指向

叶子结点u,再指向第二颗树的叶子结点,到根。root1-->u-->v-->root2,假设合并前u的深度为x,v的深度为y,则合并后支路的长度为L=x+y-1;

若L为奇数,则我们可以选择两种颜色,分别染1,2,1,...2,1。

若L为偶数,则我们可以选择两种颜色,分别染1,2,1,...2。

也就是说,这个图,我们从root1-->root2染色,如果只用1,2染色,那么所有支路的长度必须同为奇或同为偶。而这个奇偶又是由合并前叶子的深度决定的,所以先DFS

算深度,然后再if-else匹配。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
const int N=2e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,tot=;
int cnt[N],dep1[N],dep2[N];
int a[N];
int d[][]={,,,,-,,,-};
vector<int>edg1[N],edg2[N],v1,v2;
queue<int>odd1,even1,odd2,even2;
void dfs1(int u,int fa){
dep1[u]=dep1[fa]+;
if(edg1[u].size()==){
if(dep1[u]&)odd1.push(u);
else even1.push(u);
return;
}
for(int i=;i<edg1[u].size();i++){
int v=edg1[u][i];
if(v!=fa)dfs1(v,u);
}
}
void dfs2(int u,int fa){
dep2[u]=dep2[fa]+;
if(edg2[u].size()==){
if(dep2[u]&)odd2.push(u);
else even2.push(u);
return;
}
for(int i=;i<edg2[u].size();i++){
int v=edg2[u][i];
if(v!=fa)dfs2(v,u);
}
}
int main()
{
int x,y,ans=,sum=;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
edg1[x].pb(y);
edg1[y].pb(x);
}
for(int i=;i<=n;i++){
if(edg1[i].size()>){
dfs1(i,);
break;
}
}
scanf("%d",&m);
for(int i=;i<m;i++){
scanf("%d%d",&x,&y);
edg2[x].pb(y);
edg2[y].pb(x);
}
for(int i=;i<=m;i++){
if(edg2[i].size()>){
dfs2(i,);
break;
}
}
if(odd1.size()!=odd2.size()&&odd1.size()!=even2.size()){
puts("");
for(int i=;i<=n;i++){
if(edg1[i].size()==)v1.push_back(i);
}
for(int i=;i<=m;i++){
if(edg2[i].size()==)v2.push_back(i);
}
for(int i=;i<v1.size();i++){
printf("%d %d\n",v1[i],v2[i]);
}
}
else if(odd1.size()==even2.size()){
puts("");
while(!odd1.empty()){
int od1=odd1.front();odd1.pop();
int ev2=even2.front();even2.pop();
printf("%d %d\n",od1,ev2);
}
while(!even1.empty()){
int od2=odd2.front();odd2.pop();
int ev1=even1.front();even1.pop();
printf("%d %d\n",ev1,od2);
}
}
else if(odd1.size()==odd2.size()){
puts("");
while(!odd1.empty()){
int od1=odd1.front();odd1.pop();
int od2=odd2.front();odd2.pop();
printf("%d %d\n",od1,od2);
}
while(!even1.empty()){
int ev1=even1.front();even1.pop();
int ev2=even2.front();even2.pop();
printf("%d %d\n",ev1,ev2);
}
}
return ;

2017 Hackatari Codeathon B. 2Trees(深搜)(想法)的更多相关文章

  1. 2017 Hackatari Codeathon C. Arcade(DP)(滚动数组)

    C. Arcade time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  2. ACM 海贼王之伟大航路(深搜剪枝)

    "我是要成为海贼王的男人!" 路飞他们伟大航路行程的起点是罗格镇,终点是拉夫德鲁(那里藏匿着"唯一的大秘宝"--ONE PIECE).而航程中间,则是各式各样的 ...

  3. Poj(2488),按照字典序深搜

    题目链接:http://poj.org/problem?id=2488 思路:按照一定的字典序深搜,当时我的想法是把所有的可行的路径都找出来,然后字典序排序. 后来,凡哥说可以在搜索路径的时候就按照字 ...

  4. [vijos1159&洛谷1494]岳麓山上打水<迭代深搜>

    题目链接:https://vijos.org/p/1159 https://www.luogu.org/problem/show?pid=1494 这是今天的第三道迭代深搜的题,虽然都是迭代深搜的模板 ...

  5. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  6. 利用深搜和宽搜两种算法解决TreeView控件加载文件的问题。

    利用TreeView控件加载文件,必须遍历处所有的文件和文件夹. 深搜算法用到了递归. using System; using System.Collections.Generic; using Sy ...

  7. 2016弱校联盟十一专场10.3---Similarity of Subtrees(深搜+hash、映射)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52310 problem description Define the depth of a ...

  8. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  9. 2015暑假多校联合---Cake(深搜)

    题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...

随机推荐

  1. 「模板」 线段树——区间乘 && 区间加 && 区间求和

    「模板」 线段树--区间乘 && 区间加 && 区间求和 原来的代码太恶心了,重贴一遍. #include <cstdio> int n,m; long l ...

  2. 【BZOJ】2190 [SDOI2008]仪仗队

    [算法]欧拉函数 欧拉线性筛 [题解]将图从左至右,从下至上,分别标号0~n-1. 除了坐标0,一个点会被观察到当且仅当其坐标(i,j)的i与j互质,否则会被(i/d,j/d)挡住. 所以累加2~n- ...

  3. 联系博主 Contact

    李莫 / Ray OI 蒟蒻一只 / A Player of Olympiad in Informatics QQ:740929894 邮箱 / Email :rayking2017@outlook. ...

  4. H5特性 MutationObserver 监听元素 动态改变iframe高度

    这些代码要写在iframe页中执行 <script type="text/javascript"> $(function () { // Firefox和Chrome早 ...

  5. quartz的简介

    1. 介绍  Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源的任务调度框架,是完全由java开发的一个开源的任务日程管理系统,“任务进度管理器”就是一个在预 ...

  6. mysql之数据库操作进阶(三)

    环境信息 数据库:mysql-5.7.20 操作系统:Ubuntu-16.04.3 查询 条件查询 # 使用where关键字 select * from 表名 where 条件 # 比较运算符 > ...

  7. java基础 流程控制和条件语句,循环语句

    顺序结构 程序的顺序结构: 如果代码里没有流程控制,程序是按照书写的格式从上而下一行一行执行的, 一条语句执行完之后继续执行下一条语句,中间没有判断和跳转,直到程序的结束. if语句 if语句使用bo ...

  8. linux dpm机制分析(上)【转】

    转自:http://blog.csdn.net/lixiaojie1012/article/details/23707681 1      DPM介绍 1.1        Dpm:  设备电源管理, ...

  9. 大公司开源网址[www]

    https://github.com/blackberry https://github.com/CallForSanity?tab=repositories https://github.com/b ...

  10. XCopy复制文件夹命令及参数详解以及xcopy拷贝目录并排除特定文件

    XCOPY是COPY的扩展,可以把指定的目录连文件和目录结构一并拷贝,但不能拷贝系统文件:使用时源盘符.源目标路径名.源文件名至少指定一个:选用/S时对源目录下及其子目录下的所有文件进行COPY.除非 ...