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年之前就有人转发到自己博客了.放到自己的地盘,容易记起有这么个心灵鸡汤. 一.关于工作与生活 我有个有趣的观察,外企公司多的 ...
随机推荐
- oss上传文件0字节
最近使用oss上传文件,不同项目中使用的版本也不同,之前的都能正常上传,最近因需要添加ObjectMetaData属性,扩展了一个方法,发现上传的文件始终是0字节的,最终跟源码发现conntentLe ...
- sqli-labs学习笔记 DAY2
DAY2 sqli-labs lesson 2 手工注入 URL:http://localhost/sqli-labs-master/Less-2/ Parameter:id 注入点检测:id=2;– ...
- PytorchZerotoAll学习笔记(五)--逻辑回归
逻辑回归: 本章内容主要讲述简单的逻辑回归:这个可以归纳为二分类的问题. 逻辑,非假即真.两种可能,我们可以联想一下在继电器控制的电信号(0 or 1) 举个栗子:比如说你花了好几个星期复习的考试(通 ...
- 联邦快递 IE和IP的区别 Fedex IE VS Fedex IP
什么是FedEx IP? FedEx IP指的是联邦快递优先服务,时效比较快些,相对来说价格也比普通的高一些. 什么是FedEx IE? FedEx IE指的是联邦快递经济服务,时效与FedEx IP ...
- 关于 WebView 知识点的详解
什么是 WebView WebView 是手机中内置了一款高性能 webkit 内核浏览器,在 SDK 中封装的一个组件.没有提供地址栏和导航栏, WebView 只是单纯的展示一个网页界面.在开发中 ...
- Python 代码调试技巧
使用 pdb 进行调试 pdb 是 python 自带的一个包,为 python 程序提供了一种交互的源代码调试功能,主要特性包括设置断点.单步调试.进入函数调试.查看当前代码.查看栈片段.动态改变变 ...
- USACO 1.4.2 Mother's Mil 母亲的牛奶(DFS)
Description 农民约翰有三个容量分别是A,B,C升的桶,A,B,C分别是三个从1到20的整数,最初,A和B桶都是空的,而C桶是装满牛奶的.有时,约翰把牛奶从一个桶倒到另一个桶中,直到被灌桶装 ...
- 进击的SDN
SDN是什么? 不再是OSI七层模型,全新的SDN三层模型. 起源于斯坦福大学博士生领导的一个项目Ethane:通过一个集中式控制器(NOX),网络管理员可以定义基于网络流的控制策略,并将这个策略用于 ...
- IT小小鸟 读书笔记
讲真的,整本书我并没有看完,翻阅了一下,然后小小的借鉴了一下! 首先设计你自己的进度条 进度条的设计是一个很多人都知道的故事:同样的耗时,如果不给任何进度提示,只是在完成之后才弹出一个完成消息,中间没 ...
- 使用fprof基本步骤
$erl -name a@localhost -setcookie abc -remsh b@localhost >fprof:trace([start, {file, "/home/ ...