Codeforces 601D. Acyclic Organic Compounds(四个愿望一次满足)
2017-12-12 13:56
453 查看
trie合并的裸题...因为最多只有n个点,所以最多合并n次,复杂度$O(N*26)$。
#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<algorithm> #define ll long long using namespace std; const int maxn=500010, inf=1e9; const ll mod=233333333333333333; struct poi{int too, pre;}e[maxn<<1]; 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=1; k=0; char c=getchar(); while(c<'0' || c>'9') c=='-' && (f=-1), c=getchar(); while(c<='9' && c>='0') k=k*10+c-'0', 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]=1; b[x]=hs[x]=(hs[fa]*27+s[x]-'a'+1)%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==1) 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, 0); if(son[x]) dfs2(son[x], x, 1), skip=son[x]; update(x, fa, 1); ans[x]=sum; skip=0; if(!heavy) update(x, fa, -1), sum=0; if(ans[x]+c[x]>ANS) ANS=ans[x]+c[x], ANSNUM=1; else if(ans[x]+c[x]==ANS) ANSNUM++; } int main() { read(n); for(int i=1;i<=n;i++) read(c[i]); scanf("%s", s+1); for(int i=1;i<n;i++) read(x), read(y), add(x, y), add(y, x); dfs1(1, 0); N=n; sort(b+1, b+1+N); N=unique(b+1, b+1+N)-b-1; for(int i=1;i<=n;i++) hs[i]=lower_bound(b+1, b+1+N, hs[i])-b; dfs2(1, 0, 1); printf("%d\n%d", ANS, ANSNUM); }View Code
还可以hash之后写线段树合并,复杂度$O(NlogN)$。(真的懒得写了T T
相关文章推荐
- 【CodeForces】601 D. Acyclic Organic Compounds
- CF 601D(Acyclic Organic Compounds-字典树)
- Acyclic Organic Compounds
- Codeforces 601D Acyclic Organic Compounds(dfs+字典树合并)
- Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并
- Codeforces Round#333 div.1 D Acyclic Organic Compounds
- codeforces 722F - Cyclic Cipher
- 【codeforces 709C】Letters Cyclic Shift
- codeforces 601B 斜率+单调栈
- 【67.24%】【codeforces 745A】Hongcow Learns the Cyclic Shift
- 【codeforces 691 E】【矩阵快速幂 思维题】【给定序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二进制表示中1的个数是3的倍数。问满足条件的序列个数】
- codeforces 708A - Letters Cyclic Shift
- 【codeforces 721B】B. Passwords【输入密码按长度非递减顺序输入,每输一次耗时1秒,输错k次等待5秒,最后一行为正确密码,问消耗的最少和最多时间】
- codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
- codeforces 601 A. The Two Routes (最短路d+f)
- 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环
- Codeforces 754E:Dasha and cyclic table
- Codeforces 601A:The Two Routes 宽搜最短路径
- CodeForces 601 C.Kleofáš and the n-thlon(概率DP)
- Almost Acyclic Graph(【CodeForces】915 D)