http://codeforces.com/problemset/problem/778/C (题目链接)

题意

  给出一个字典树,问删掉哪一层以后,得到的字典树最小。

Solution

  直接对于每一层进行讨论启发式合并暴力搞就好了。复杂度证明真是玄学。

细节

  注意根节点。

代码

// codeforces 778C
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=300010;
int head[maxn],size[maxn],deep[maxn],c[maxn][26];
int n,ans[maxn],cnt,sz;
char ch[10];
struct edge {int to,next;char w;}e[maxn<<1]; void link(int u,int v) {
e[++cnt]=(edge){v,head[u],ch[0]};head[u]=cnt;
e[++cnt]=(edge){u,head[v],ch[0]};head[v]=cnt;
}
int merge(int a,int b) {
if (!a || !b) return a|b;
int x=++sz;size[x]=1;
for (int i=0;i<26;i++) {
c[x][i]=merge(c[a][i],c[b][i]);
size[x]+=size[c[x][i]];
}
return x;
}
void dfs(int x,int fa) {
size[x]=1;
for (int i=head[x];i;i=e[i].next) if (e[i].to!=fa) {
deep[e[i].to]=deep[x]+1;
c[x][e[i].w-'a']=e[i].to;
dfs(e[i].to,x);
size[x]+=size[e[i].to];
}
ans[deep[x]]+=size[x];sz=n;
int p=0;
for (int i=0;i<26;i++) p=merge(p,c[x][i]);
ans[deep[x]]-=max(size[p],1);
}
int main() {
scanf("%d",&n);
for (int u,v,i=1;i<n;i++) {
scanf("%d%d%s",&u,&v,ch);
link(u,v);
}
dfs(1,0);
int res=0;
for (int i=0;i<n;i++) if (ans[i]>ans[res]) res=i;
printf("%d\n%d",n-ans[res],res+1);
return 0;
}

【codeforces 778C】 Peterson Polyglot的更多相关文章

  1. CodeForces - 778C: Peterson Polyglot (启发式合并trie树)

    Peterson loves to learn new languages, but his favorite hobby is making new ones. Language is a set ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. bootstrap switch样式修改与多列等间距布局

    先以一张图开启今天的随笔 今天实习遇到了switch按钮,小姐姐说用插件bootstrap switch来写,我第一次用这个插件,首先在引入方面就遇到了很多坑,先来总结一下bootstrap swit ...

  2. undefined和“undefined”

    说实话,它们之间的区别挺明显的,我们一般认为undefined是JavaScript提供的一个“关键字”,而“undefined”却是一个字符串,只是引号的内容和undefined一样. undefi ...

  3. ResNet——Deep Residual Learning for Image Recognition

    1. 摘要 更深的神经网络通常更难训练,作者提出了一个残差学习的框架,使得比过去深许多的的网络训连起来也很容易. 在 ImageNet 数据集上,作者设计的网络达到了 152 层,是 VGG-19 的 ...

  4. Node.js开发入门—套接字(socket)编程

    Node.js的net模块提供了socket编程接口,方便我们利用较为底层的套接字接口来实现应用协议.这次我们看一个简单的回显服务器示例,包括服务端和客户端的代码. 代码 分服务器和客户端两部分来说吧 ...

  5. iOS静默推送(Silent Remote Notifications)

    此功能是iOS7新增加的功能,允许应用收到通知后在后台(background)状态下运行一段代码,可用于从服务器获取内容更新. 普通推送:收到推送后(有文字有声音),点开通知,进入APP后,才执行-- ...

  6. 第三次实验报告 敏捷开发与XP实践

    一.  实验内容 (一)敏捷开发与XP 摘要:一项实践在XP环境中成功使用的依据通过XP的法则呈现,包括:快速反馈.假设简单性.递增更改.提倡更改.优质工作.XP软件开发的基石是XP的活动,包括:编码 ...

  7. Leetcode题库——23.合并k个排序链表

    @author: ZZQ @software: PyCharm @file: mergeKLists.py @time: 2018/10/12 19:55 说明:合并 k 个排序链表,返回合并后的排序 ...

  8. IDE调研之二

    Eclipse和Jetbrains的IntelliJ IDEA对比: Eclipse工具 在Eclipse中,可以最大化控制台.通过双击标签或者Ctrl+M快捷键就可以实现,但是在IntelliJ I ...

  9. 结对&团队之1715|K班取经

    声明:同学请勿抄袭,追责莫要怪我: 因为暂时闲着没事,就翻阅学长学姐的博客找找灵感,个人觉得应该还有人和我一样对软工实践未来的一大段路还很天真,包括目前的结对作业和团队组队也很迷路,于是写下这篇博客提 ...

  10. jquery easyui datagrid getSelected getChecked获取选择行数据(勾选)数据

    要想获得上图所选取的元素只能用getChecked getSelected不能进行多个选择,只能单选 /* getSelected取得选中的数据,否则为null */ var user=$(" ...