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. docker 安装vim

    执行以下命令 apt-get update apt-get install vim

  2. docker node项目 连接mongodb

    在弄docker部署node项目的时候遇到了连接mongdb的问题,记录一下问题解决办法 一.Docker 安装 MongoDB 1.查找Docker Hub上的mongo镜像 [root@VM_49 ...

  3. AJAX请求中出现OPTIONS请求

    背景 有一个前后端分离的VUE项目来发送ajax请求, 查看Nginx日志或使用Chrome Dev Tools查看请求发送情况时, 会看到每次调后台API的请求之前, 都会发送一个OPTIONS请求 ...

  4. TeamWork#3,Week5,Bing Input Method vs Sogou Input Method

    现在电脑上用五笔的用户越来越少了,好的拼音输入法也是难求.必应输入法的前身英库拼音输入法来自微软亚洲研究院的多项基础研究成果.最新的必应输入法不仅保留了英库拼音输入法的各项优势,还结合了必应的搜索体验 ...

  5. MathExam小学一二年级计算题生成器V1.0

    MathExam小学一二年级计算题生成器v1.0 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning ...

  6. 2018-2019-20172321 《Java软件结构与数据结构》第八周学习总结

    2018-2019-20172321 <Java软件结构与数据结构>第八周学习总结 教材学习内容总结 第12章 优先队列与堆 一.概述 堆 堆的前提就是他首先是一个完全二叉树,其次就是满足 ...

  7. vs2013+python+ cocos2d-x-3.3rc0环境搭建

    1.vs2013安装一路next,安装即可,时间1~2个小时 2.解压cocos2d-x-3.3rc0   build文件夹里会有名为  cocos2d-win32.vc2012的sln文件  打开  ...

  8. spring冲刺第十天

    调试运行,对整体的游戏方面进行改进.冲刺完了,但依然有很多问题,比如无法暂停,游戏结束后只能退出重来等

  9. spring冲刺第八天

    昨天使人物成功的在地图上运动,并设计炸弹爆炸效果. 今天使炸弹可以炸死人物并可以炸没砖块,并试着将小怪加入地图. 遇到的问题:现在还没有将小怪加入地图,各个模块的整合是比较麻烦的,我还要在这方面下点功 ...

  10. The Begining

    学习记录之旅,就此开始.软件工程,Java神马的统统到我碗里来.