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. Tensorflow张量的形状表示方法

    对输入或输出而言: 一个张量的形状为a x b x c x d,实际写出这个张量时: 最外层括号[…]表示这个是一个张量,无别的意义! 次外层括号有a个,表示这个张量里有a个样本 再往内的括号有b个, ...

  2. Mysql读写分离——主从数据库+Atlas

    mysql集群 最近在参加项目开发微信小程序后台,由于用户数量巨大,且后台程序并不是很完美,所以对用户的体验很是不友好(简单说就是很卡).赶巧最近正在翻阅<大型网站系统与Java中间件实践> ...

  3. python编辑选课系统

    一.需求分析 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 3. 课程包含,周期,价格,通过学校创建课 ...

  4. Plasma Cash 合约解读

    作者介绍 虫洞社区·签约作者 steven bai Plasma Cash 合约解读 Plasma Cash 合约解读 1. 合约代码 2. 合约文件简单介绍 3. Plasma Cash 的基础数据 ...

  5. dobule运算

    DecimalFormat df = new DecimalFormat("0.00"); double rate = (warnMonNum/totalCustCount)*10 ...

  6. 如何知道一个App的包名呢

    包名(Package name)是Android系统中判断一个APP的唯一标识 记录我获取包名的几种方式 方法一:通过cmd命令,打开你要获取包名的APP 1.adb shell 2.dumpsys ...

  7. 第一次spring冲刺第5天

    今天进行讨论基础功能的核心代码方面,还有简单的讨论继续关于界面的美化, 计算生成的答案功能 public class Core {// char[]h={'+','-','*','/'};int re ...

  8. iOS- CALayer绘图,如何绘制渐变效果图

    1.渐变效果 —> 色彩空间 首先:自定义一个渐变视图,在自定义视图中重写drawRect #import "ViewController.h" #import " ...

  9. Java网络编程一:基础知识详解

    网络基础知识 1.OSI分层模型和TCP/IP分层模型的对应关系 这里对于7层模型不展开来讲,只选择跟这次系列主题相关的知识点介绍. 2.七层模型与协议的对应关系 网络层   ------------ ...

  10. 订制EditText光标

    订制EditText光标 设置背景android:background="@null" 设置光标样式:android:textCursorDrawable="@drawa ...