【codeforces 778C】 Peterson Polyglot
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的更多相关文章
- CodeForces - 778C: Peterson Polyglot (启发式合并trie树)
Peterson loves to learn new languages, but his favorite hobby is making new ones. Language is a set ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- 学习python,第四篇:Python 3中bytes/string的区别
原文:http://eli.thegreenplace.net/2012/01/30/the-bytesstr-dichotomy-in-python-3 python 3中最重要的新特性可能就是将文 ...
- Annotation 使用备忘
title: Annotation 使用备忘 date: 2016-11-16 23:16:43 tags: [Annotation] categories: [Programming,Java] - ...
- node上的__dirname和./的区别
概要 Node.js 中,__dirname 总是指向被执行 js 文件的绝对路径,所以当你在 /d1/d2/myscript.js 文件中写了 __dirname, 它的值就是 /d1/d2 . 相 ...
- Python之并发编程-多进程
目录 一.multiprocessiong模块介绍 二.Process类的介绍 三.进一步介绍(守护进程.锁.队列.管道.事件等) 1.守护进程 2.锁(同步锁.互斥锁) 3.信号量(了解) 4.队列 ...
- 数据库之python操作mysql
目录 一.pymysql 二.SQLAchemy 2.操作使用 (1)连接数据库 (2)执行原生SQL语句 (3)ORM操作-数据表操作 (4)ORM操作-数据行操作 (5)更多例子 一.pymysq ...
- 前端_html
目录 HTML介绍 标签说明 常用标签 <!DOCTYPE>标签 <head>内常用标签 <body>内常用标签 特殊字符 其他:各种各样的标签 HTML的规范 H ...
- url传多值问题
使用url传值的特点是操作简单,虽然安全性低,但依然广泛运用. url传数据绑定的值: <a href='Default.aspx?id=<%#Eval("ID")%& ...
- 我的寒假C(C++)学习计划
前言 要补缺加强C语言的想法由来已久,上学期因为种种原因,某些知识点学习得不是很理想,而且,许多地方也有加强的必要,所以这次布置的随笔可谓是来得恰到好处. 学习材料 C Primer Plus 师爷的 ...
- POJ 2096 Collecting Bugs 期望dp
题目链接: http://poj.org/problem?id=2096 Collecting Bugs Time Limit: 10000MSMemory Limit: 64000K 问题描述 Iv ...
- wcf服务查看工具
文章:接口测试工具soapUI(一) 文章:VS自带WCF测试客户端简单介绍