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年之前就有人转发到自己博客了.放到自己的地盘,容易记起有这么个心灵鸡汤. 一.关于工作与生活 我有个有趣的观察,外企公司多的 ...
随机推荐
- 程序员必备神器--vps主机
今天推荐一个功能强大.居家必备的神器给刚入行或还不了解它的同学们.且不说它有什么功能,它有多好用,先说先你有没有碰到过这些问题吧. 用百度查技术问题,发现都是互相抄袭和广告,大佬都说google好,但 ...
- leetcode13_C++罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- Python基础灬高阶函数(lambda,filter,map,reduce,zip)
高阶函数 lambda函数 关键字lambda表示匿名函数,当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. lambda函数省略函数名,冒号前为参数,冒号后函数体. # ...
- Python多重赋值
可以将变量名视对象的一个链接 >>>foo1 = foo2 = 4.3 >>>foo1 is foo2 True >>>foo1 = 4.3 &g ...
- presto——java.sql.SQLException: Error executing query与javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?异常问题
使用presto的时候以mysql为presto的数据源 安装的presto是0.95版本:使用的presto-jdbc是0.202的,这里使用jdbc去访问时候,connection可以链接成功,但 ...
- 第7章 监听器Listener
Listener概述 Listener的使用 使用Listener需要实现相应的Listener接口. public class SessionListenerTest implements Http ...
- Java 线程安全问题
线程安全问题产生原因: 1.多个线程操作共享的数据: 2.操作共享数据的线程代码有多条. 当一个线程正在执行操作共享数据的多条代码过程中,其它线程也参与了运算, 就会导致线程安全问题的发生. cl ...
- find . -name file -exec echo abc > {} \; fail
find . -name file -exec echo abc > {} \; fail 应该改用: find . -name file -exec bash -c 'echo abc > ...
- 团队项目-BUG挖掘
测试硬件: 华为畅享5 测试平台: 安卓5.1 测试项目Git地址: https://github.com/RABITBABY/We-have-bing 测试Apk来源地址: http://www.a ...
- 3dContactPointAnnotationTool开发日志(二七)
今天的主要工作是把选中物体以及复制删除物体和右边三个面板联系起来,就是通过鼠标框选住物体,右边面板的对应项的颜色也会改变,而且通过右边面板也能控制物体的选中状态,被选中的物体成cyan青色,并且包 ...