Codeforces 601D. Acyclic Organic Compounds(四个愿望一次满足)
trie合并的裸题...因为最多只有n个点,所以最多合并n次,复杂度$O(N*26)$。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int maxn=, inf=1e9;
struct poi{int too, pre;}e[maxn<<];
struct tjm{int nxt[], size;}tree[maxn];
int n, x, y, ans, cnt, tot;
int last[maxn], c[maxn];
char s[maxn];
inline void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline void add(int x, int y){e[++tot]=(poi){y, last[x]}; last[x]=tot;}
void merge(int &x, int y)
{
if(!x || !y) {x+=y; return;}
tree[x].size=;
for(int i=;i<;i++)
merge(tree[x].nxt[i], tree[y].nxt[i]), tree[x].size+=tree[tree[x].nxt[i]].size;
}
void dfs(int x, int fa)
{
tree[x].size=;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa)
{
dfs(too, x); int tmp=tree[tree[x].nxt[s[e[i].too]-'a']].size;
merge(tree[x].nxt[s[e[i].too]-'a'], e[i].too);
tree[x].size+=tree[tree[x].nxt[s[e[i].too]-'a']].size-tmp;
}
if(tree[x].size+c[x]>ans) ans=tree[x].size+c[x], cnt=;
else if(tree[x].size+c[x]==ans) cnt++;
}
int main()
{
read(n);
for(int i=;i<=n;i++) read(c[i]); scanf("%s", s+);
for(int i=;i<n;i++) read(x), read(y), add(x, y), add(y, x);
dfs(, ); printf("%d\n%d", ans, cnt);
}
当然还可以直接上哈希+平衡树+启发式合并,用set自带去重就好了,复杂度$O(Nlog^2N)$。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<set>
#define ll long long
using namespace std;
const int maxn=, inf=1e9;
const ll mod=;
typedef set<ll>::iterator ddq;
struct poi{int too, pre;}e[maxn<<];
int n, x, y, tot, ans, ansnum;
int last[maxn], c[maxn], root[maxn];
ll hs[maxn];
char ch[maxn];
set<ll>s[maxn];
inline void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline void add(int x, int y){e[++tot]=(poi){y, last[x]}; last[x]=tot;}
inline int merge(int x, int y)
{
if(s[x].size()<s[y].size()) swap(x, y);
for(ddq ity=s[y].begin();ity!=s[y].end();ity++) s[x].insert(*ity);
s[y].clear(); return x;
}
void dfs(int x, int fa)
{
hs[x]=(hs[fa]*+ch[x]-'a'+)%mod;
s[x].insert(hs[x]); root[x]=x;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa)
{
dfs(too, x);
root[x]=merge(root[x], root[too]);
}
int size=s[root[x]].size();
if(size+c[x]>ans) ans=size+c[x], ansnum=;
else if(size+c[x]==ans) ansnum++;
}
int main()
{
read(n);
for(int i=;i<=n;i++) read(c[i]);
scanf("%s", ch+);
for(int i=;i<n;i++) read(x), read(y), add(x, y), add(y, x);
dfs(, ); printf("%d\n%d", ans, ansnum);
}
当然还可以hash之后用dsu on tree,复杂度$O(NlogN)$。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=, inf=1e9;
const ll mod=;
struct poi{int too, pre;}e[maxn<<];
int n, x, y, N, tot, sum, skip, ANS, ANSNUM;
int c[maxn], last[maxn], son[maxn], size[maxn], cnt[maxn], ans[maxn];
ll b[maxn], hs[maxn];
char s[maxn];
inline void read(int &k)
{
int f=; k=; char c=getchar();
while(c<'' || c>'') c=='-' && (f=-), c=getchar();
while(c<='' && c>='') k=k*+c-'', c=getchar();
k*=f;
}
inline void add(int x, int y){e[++tot]=(poi){y, last[x]}; last[x]=tot;}
void dfs1(int x, int fa)
{
size[x]=; b[x]=hs[x]=(hs[fa]*+s[x]-'a'+)%mod;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa)
{
dfs1(too, x);
size[x]+=size[too];
if(size[too]>size[son[x]]) son[x]=too;
}
}
void update(int x, int fa, int delta)
{
if(delta==) sum+=(!cnt[hs[x]]);
cnt[hs[x]]+=delta;
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa && too!=skip) update(too, x, delta);
}
void dfs2(int x, int fa, bool heavy)
{
for(int i=last[x], too;i;i=e[i].pre)
if((too=e[i].too)!=fa && too!=son[x]) dfs2(too, x, );
if(son[x]) dfs2(son[x], x, ), skip=son[x];
update(x, fa, ); ans[x]=sum; skip=;
if(!heavy) update(x, fa, -), sum=;
if(ans[x]+c[x]>ANS) ANS=ans[x]+c[x], ANSNUM=;
else if(ans[x]+c[x]==ANS) ANSNUM++;
}
int main()
{
read(n);
for(int i=;i<=n;i++) read(c[i]);
scanf("%s", s+);
for(int i=;i<n;i++) read(x), read(y), add(x, y), add(y, x);
dfs1(, );
N=n; sort(b+, b++N); N=unique(b+, b++N)-b-;
for(int i=;i<=n;i++) hs[i]=lower_bound(b+, b++N, hs[i])-b;
dfs2(, , ); printf("%d\n%d", ANS, ANSNUM);
}
还可以hash之后写线段树合并,复杂度$O(NlogN)$。(真的懒得写了T T
Codeforces 601D. Acyclic Organic Compounds(四个愿望一次满足)的更多相关文章
- 【CodeForces】601 D. Acyclic Organic Compounds
[题目]D. Acyclic Organic Compounds [题意]给定一棵带点权树,每个点有一个字符,定义一个结点的字符串数为往下延伸能得到的不重复字符串数,求min(点权+字符串数),n&l ...
- Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并
D. Acyclic Organic Compounds You are given a tree T with n vertices (numbered 1 through n) and a l ...
- Acyclic Organic Compounds
题意: 给一以1为根的字符树,给出每个节点的字符与权值,记 $diff_{x}$ 为从 $x$ 出发向下走,能走到多少不同的字符串,求问最大的$diff_{x} + c_{x}$,并求有多少个 $di ...
- CF601D:Acyclic Organic Compounds
给n<=300000的树,每个点上有一个字母,一个点的权值为:从该点出发向下走到任意节点停下形成的不同字符串的数量,问最大权值. 题目本身还有一些奇怪要求在此忽略.. Trie合并的模板题. # ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- cf Round 601
A.The Two Routes(BFS) 给出n个城镇,有m条铁路,铁路的补图是公路,汽车和火车同时从1出发,通过每条路的时间为1,不能同时到达除了1和n的其它点,问他们到达n点最少要用多长时间. ...
- Codeforces Round #269 (Div. 2) A,B,C,D
CodeForces - 471A 首先要有四个数相等,然后剩下两个数不同就是Bear,否则就是Elephant. #include <bits/stdc++.h> using names ...
- 我为什么要进国企----HP大中华区总裁孙振耀退休感言
一.关于工作与生活 我有个有趣的观察,外企公司多的是25-35岁的白领,40岁以上的员工很少,二三十岁的外企员工是意气风发的,但外企公司40岁附近的经理人是很尴尬的.我见过的40岁附近的外企经理人大多 ...
- 转(HP大中华区总裁孙振耀退休感言)
开篇转发一篇好文,苦闷,消沉,寂寞,堕落的时候看看. 发现这篇文章是09年之前就有人转发到自己博客了.放到自己的地盘,容易记起有这么个心灵鸡汤. 一.关于工作与生活 我有个有趣的观察,外企公司多的 ...
随机推荐
- python装饰器简单使用
装饰器和闭包关联很大,要先明白闭包是什么 原始代码: def foo(): print('fcc') 增加装饰器 from time import ctime,sleep def w(fcc): de ...
- vue的ui库使用Element UI,纯html页面,不使用webpack那玩意
使用手册访问:https://cloud.tencent.com/developer/doc/1270 第一步:在head添加样式 <link rel="stylesheet" ...
- MapPartition和Map的区别
在Spark中有map和mapPartitions算子,处理数据上,有一些区别 主要区别: map是对rdd中的每一个元素进行操作: mapPartitions则是对rdd中的每个分区的迭代器进行操作 ...
- gevent协程、select IO多路复用、socketserver模块 改造多用户FTP程序例子
原多线程版FTP程序:http://www.cnblogs.com/linzetong/p/8290378.html 只需要在原来的代码基础上稍作修改: 一.gevent协程版本 1. 导入geven ...
- hbase 预分区
转载 http://www.cnblogs.com/bdifn/p/3801737.html
- Android 对话框(Dialogs)
对话框是提示用户作出决定或输入额外信息的小窗口. 对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件. 1.对话框设计 如需了解有关如何设计对话框的信息(包括语言建议),请阅读对话框设 ...
- c# HttpListener拒绝访问
直接记录解决步骤: 程序代码: HttpListener httpListener = new HttpListener(); httpListener.Prefixes.Add("http ...
- lintcode-512-解码方法
512-解码方法 有一个消息包含A-Z通过以下规则编码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 现在给你一个加密过后的消息,问有几种解码的方式 样例 给你的消 ...
- 运维学习笔记(七)之T02-01计算机网络 、 数制 、 网络通信参考模型
计算机网络 计算机网络概述 什么是计算机网络 硬件方面:通过线缆将网络设备和计算机连接起来 软件方面:操作系统.应用软件.应用程序通过通信线路互连 实现资源共享.信息传递 功能 数据通信/资源共享/增 ...
- vsftpd 安全性能工具
vsftpd实战(服务端192.168.23.12,客户端192.168.23.11) 1:安装vsftpdyum install -y vsftpd 2:客户端安装lftpyum install - ...