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. Git知识点整合

    Git安装 Windows上安装Git 64 位安装包下载地址 : https://github.com/git-for-windows/git/releases/download/v2.16.2.w ...

  2. 下一个时代的发展架构竟然是它!FaaaaaaaaS到底是个啥?

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云serverless团队发表于云+社区专栏 导读:2018年7月6 - 7日,一年一度的技术圈盛会ArchSummit全球架构师 ...

  3. 【RL系列】MDP与DP问题

    推荐阅读顺序: Reinforcement Learning: An Introduction (Drfit)  有限马尔可夫决策过程 动态编程笔记 Dynamic programming in Py ...

  4. 点斜杠 & 如何查看linux程序安装位置 dpkg -L yyy

    方法1: sudo find / -name ssh 方法2: Ubuntu下 看应用程序安装路径的方法 ubuntu下dpkg -L xxx看应用程序安装路径 1.点斜杠 “./”就代表在当前目录下 ...

  5. PHP中的文件包含

    在PHP中,包含文件有两种方式:include和require.这两种方式的功能一样,只有一个区别,就是使用require包含一个文件时,如果出现错误,脚本不会继续执行:而如果使用include包含, ...

  6. 2017年软件工程第十二次作业-PSP总结报告

    回顾1 1.回想一下你曾经对计算机专业的畅想 当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么? 你认为过去接触到的课程是否符合你对计算机专业的期待,为什么?经过一个学期 ...

  7. 第九次psp例行报告

    本周psp 本周进度条 代码累积折线图 博文字数累积折线图 饼状图

  8. iOS自学-UILabel常见属性

    #import "ViewController.h" #import <CoreText/CoreText.h> @interface ViewController ( ...

  9. Merge join、Hash join、Nested loop join对比分析

    简介 我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge Join,Hash Join ...

  10. Spring笔记⑥--整合struts2

    Spring如何在web应用里面用 需要额外加入的jar包 Spring-web-4.0.0 Spring-webmvc-4.0.0 Spring的配置文件,没什么不同   需要在web.xml下配置 ...