Peterson loves to learn new languages, but his favorite hobby is making new ones. Language is a set of words, and word is a sequence of lowercase Latin letters.

Peterson makes new language every morning. It is difficult task to store the whole language, so Peterson have invented new data structure for storing his languages which is called broom. Broom is rooted tree with edges marked with letters. Initially broom is represented by the only vertex — the root of the broom. When Peterson wants to add new word to the language he stands at the root and processes the letters of new word one by one. Consider that Peterson stands at the vertex u. If there is an edge from u marked with current letter, Peterson goes through this edge. Otherwise Peterson adds new edge from u to the new vertex v, marks it with the current letter and goes through the new edge. Size of broom is the number of vertices in it.

In the evening after working day Peterson can't understand the language he made this morning. It is too difficult for bored Peterson and he tries to make it simpler. Simplification of the language is the process of erasing some letters from some words of this language. Formally, Peterson takes some positive integer p and erases p-th letter from all the words of this language having length at least p. Letters in words are indexed starting by 1. Peterson considers that simplification should change at least one word, i.e. there has to be at least one word of length at least p. Peterson tries to make his language as simple as possible, so he wants to choose p such that the size of the broom for his simplified language is as small as possible.

Peterson is pretty annoyed with this task so he asks you for help. Write a program to find the smallest possible size of the broom and integer p.

Input

The first line of input contains integer n (2 ≤ n ≤ 3·105) — the size of the broom.

Next n - 1 lines describe the broom: i-th of them contains integers ui, vi and letter xi — describing the edge from ui to vi marked with letter xi.

Vertices are numbered from 1 to n. All xi are lowercase latin letters. Vertex 1 is the root of the broom.

Edges describe correct broom which is made from Peterson's language.

Output

The first line of output should contain the minimum
possible size of the broom after its simplification. The second line of
output should contain integer p to choose. If there are several suitable p values, print the smallest one.

Examples

Input
5
1 2 c
2 3 a
3 4 t
2 5 t
Output
3
2
Input
16
1 2 o
2 3 f
1 4 p
4 5 i
5 6 e
6 7 c
7 8 e
4 9 r
9 10 e
10 11 t
11 12 t
12 13 y
10 14 f
14 15 i
15 16 x
Output
12
2

题意:给定一些单词的trie树,现在你可以删去其中一层(即原有的单词的某一位),使得新的trie树最小(即节点数最少)。

思路:我们需要枚举每一层,对于每一层的这些点,需要把它们各自的子树合并成新的trie树。 菜鸡我一直在想dp去做,维护LCP什么的,没想到就是合并树来做,启发式可以做到NlogN? (虽然每次用小的加到大的里面去,但是每个节点都要做一遍合并,我直观上觉得是两个log。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int ch[maxn][],cnt,a[maxn],N;
int merge(int u,int v)
{
if(!u||!v) return u|v; int now=++cnt;
rep(i,,) ch[now][i]=merge(ch[u][i],ch[v][i]); return now;
}
void dfs(int u,int d)
{
int now=N+; cnt=N+;
rep(i,,) if(ch[u][i]) now=merge(now,ch[u][i]);
a[d]+=cnt-N-;
rep(i,,) if(ch[u][i]) dfs(ch[u][i],d+);
}
int main()
{
int u,v; char c[];
scanf("%d",&N);
rep(i,,N-){
scanf("%d%d%s",&u,&v,c+);
ch[u][c[]-'a']=v;
}
dfs(,); int ans=,p=;
rep(i,,N) if(a[i]>ans) ans=a[i],p=i;
printf("%d\n%d\n",N-ans,p);
return ;
}

CodeForces - 778C: Peterson Polyglot (启发式合并trie树)的更多相关文章

  1. bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)

    Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了!ndsf:暴力就可以轻松虐!zky:…… ...

  2. BZOJ 2733 [HNOI2012]永无乡 - 启发式合并主席树

    Description 1: 查询一个集合内的K大值 2: 合并两个集合 Solution 启发式合并主席树板子 Code #include<cstdio> #include<cst ...

  3. 【BZOJ3123】[SDOI2013] 森林(启发式合并主席树)

    点此看题面 大致题意: 给你一片森林,有两种操作:询问两点之间的第\(k\)小点权和在两棵树之间连一条边. 前置技能:树上主席树 做这道题目,我们首先要会树上主席树. 关于树上主席树,这有一道很好的例 ...

  4. CodeForces 958F3 Lightsabers (hard) 启发式合并/分治 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8835443.html 题目传送门 - CodeForces 958F3 题意 有$n$个球,球有$m$种颜色,分 ...

  5. 启发式合并&线段树合并/分裂&treap合并&splay合并

    启发式合并 有\(n\)个集合,每次让你合并两个集合,或询问一个集合中是否存在某个元素. ​ 我们可以用平衡树/set维护集合. ​ 对于合并两个\(A,B\),如果\(|A|<|B|\),那么 ...

  6. Codeforces 965E Short Code 启发式合并 (看题解)

    Short Code 我的想法是建出字典树, 然后让后面节点最多的点优先向上移到不能移为止, 然后gg. 正确做法是对于当前的节点如果没有被占, 那么从它的子树中选出一个深度最大的点换到当前位置. 用 ...

  7. BZOJ 3123 [SDOI2013] 森林 - 启发式合并 主席树

    Description 给你一片森林, 支持两个操作: 查询$x$到$y$的$K$大值,  连接两棵树中的两个点 Solution 对每个节点$x$动态开权值线段树, 表示从$x$到根节点路径上权值出 ...

  8. [BZOJ2733][HNOI2010]永无乡 解题报告 启发式合并,线段树合并

    好久没更新博客了,前段时间一直都在考试,都没时间些,现在终于有点闲了(cai guai)... 写了一道题,[HNOI2012]永无乡,其实是一道板子题,我发现我写了好多板子题...还是太菜了... ...

  9. bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)

    这题之前写过线段树合并,今天复习Splay的时候想起这题,打算写一次Splay+启发式合并. 好爽!!! 写了长长的代码(其实也不长),只凭着下午的一点记忆(没背板子...),调了好久好久,过了样例, ...

随机推荐

  1. 使用tk.mybatis快速开发curd

    使用mybatis已经是可以快速开发程序了,对于单表的curd似乎是一种可抽象的结果,下面介绍tk.mybatis的使用方式. maven引用 我使用的是这个版本,所以相关功能介绍也是这个版本. 使用 ...

  2. 安装gcc4.8.5

    安装gcc4.8.51. g++ --version, 4.4.7不支持c++112. 升级gcc-c++, 下载gcc    https://gcc.gnu.org/ 官网,镜像下载地址https: ...

  3. java后台校验 hibernate validator

    链接 : https://www.cnblogs.com/softidea/p/6044123.html

  4. Codeforces 832C - Strange Radiation

    832C - Strange Radiation 思路:二分最短时间. 代码: #include<bits/stdc++.h> using namespace std; #define l ...

  5. Linux 替换^M字符 方法

    转自:http://blog.csdn.net/lhf_tiger/article/details/8203013 真恶心,10X流程产生的csv文件的行位居然有^M字符,害我一直在找报错原因,真是坑 ...

  6. boruvka算法

    算法正确性证明: 1.最优性:最小边一定包含在生成树中. 2.合法性:一定不会构成环.如果存在环说明一个点的最小连边有两个,显然矛盾. 算法时间复杂度证明: 每执行一次算法,所有联通块的大小都至少为2 ...

  7. JS冒泡排序的6种写法(武当雄风)

    天下英雄出我辈,一入江湖岁月催.鸿图霸业谈笑间,不胜人生一场醉. 武当山上,一年一度的试道大会又开始了... 众武当弟子摩拳擦掌都想在此次试道大会上一展风采... 张三丰临终前曾留下一句话:试道大会采 ...

  8. python-day67--MTV之Template

    一.什么是模板? html+模板语法 二.模版包括在使用时会被值替换掉的 变量,和控制模版逻辑的 标签. 三.嵌入变量的三种方式: def current_time(req): # ========= ...

  9. Mvc 学习笔记(一)

    1. MVC 表示 模型-视图-控制器.MVC是一种用于开发应用程序的模式,具备良好的架构,可测试和易于维护.基于MVC应用程序中包含: Models:表示应用程序的数据,并使用验证逻辑强制执行业务规 ...

  10. 无名管道跟dup,dup的使用

    参考资料: http://www.tldp.org/LDP/lpg/node11.html http://blog.csdn.net/yeyuangen/article/details/6852682 ...