BZOJ 3926: [Zjoi20150]诸神眷顾的幻想乡
3926: [Zjoi20150]诸神眷顾的幻想乡
Time Limit: 10 Sec Memory Limit: 512 MB
Submit: 438 Solved: 273
Description
幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日。
Input
第一行两个正整数n,c。表示空地数量和颜色数量。
Output
一行,输出一个整数,表示答案。
Sample Input
0 2 1 2 1 0 0
1 2
3 4
3 5
4 6
5 7
2 5
Sample Output
HINT
对于所有数据,1<=n<=100000, 1<=c<=10。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
vector<int>g[maxn];
int du[maxn];
struct SAM {
struct node {
int son[],f,len;
void init(int _len = ) {
memset(son,-,sizeof son);
f = -;
len = _len;
}
} e[];
int tot,last;
int newnode(int len = ) {
e[tot].init(len);
return tot++;
}
void init() {
tot = last = ;
newnode();
}
int extend(int p,int c) {
int np = newnode(e[p].len + );
while(p != - && e[p].son[c] == -) {
e[p].son[c] = np;
p = e[p].f;
}
if(p == -) e[np].f = ;
else {
int q = e[p].son[c];
if(e[p].len + == e[q].len) e[np].f = q;
else {
int nq = newnode();
e[nq] = e[q];
e[nq].len = e[p].len + ;
e[np].f = e[q].f = nq;
while(p != - && e[p].son[c] == q) {
e[p].son[c] = nq;
p = e[p].f;
}
}
}
return np;
}
LL count(LL ret = ) {
for(int i = ; i < tot; ++i)
ret += e[i].len - e[e[i].f].len;
return ret;
}
} sam; int color[maxn];
void dfs(int u,int fa,int p){
int x = sam.extend(p,color[u]);
for(int i = g[u].size()-; i >= ; --i)
if(g[u][i] != fa) dfs(g[u][i],u,x);
}
int main() {
int n,c,u,v;
while(~scanf("%d%d",&n,&c)) {
sam.init();
for(int i = ; i <= n; ++i) {
g[i].clear();
du[i] = ;
}
for(int i = ; i <= n; ++i)
scanf("%d",color + i);
for(int i = ; i < n; ++i){
scanf("%d%d",&u,&v);
++du[u];
++du[v];
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = ; i <= n; ++i)
if(du[i] == ) dfs(i,,);
printf("%lld\n",sam.count());
}
return ;
}
BZOJ 3926: [Zjoi20150]诸神眷顾的幻想乡的更多相关文章
- BZOJ 3926: [Zjoi20150]诸神眷顾的幻想乡(后缀自动机)
被这道题坑了= =只与一个空地相连的空地不超过20个只与一个空地相连的空地不超过20个 因为很重要所以说两遍 就是说儿子节点最多只有20个 把这20个节点作为根遍历一遍所得到的tire所得到的所有不同 ...
- BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡
3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1017 Solved: 599[Submit][S ...
- 字符串(广义后缀自动机):BZOJ 3926 [Zjoi2015]诸神眷顾的幻想乡
3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 843 Solved: 510[Submit][St ...
- BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡 [广义后缀自动机 Trie]
3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1124 Solved: 660[Submit][S ...
- bzoj 3926 [Zjoi2015]诸神眷顾的幻想乡(SAM)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3926 [题意] 给定一棵树,每个节点都有相应的颜色,且保证叶子数不超过20,问 ...
- ●BZOJ 3926 [Zjoi2015]诸神眷顾的幻想乡
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3926题解&&代码: 后缀自动机,Trie树 如果以每个叶子为根,所有的子串一 ...
- BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机 后缀自动机 字符串
https://www.lydsy.com/JudgeOnline/problem.php?id=3926 广义后缀自动机是一种可以处理好多字符串的一种数据结构(不像后缀自动机只有处理一到两种的时候比 ...
- 【刷题】BZOJ 3926 [Zjoi2015]诸神眷顾的幻想乡
Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日. 粉丝们非常热情,自发组织表演了一系列节目给幽香看. ...
- BZOJ.3926.[ZJOI2015]诸神眷顾的幻想乡(广义后缀自动机)
题目链接 要对多个串同时建立SAM,有两种方法: 1.将所有串拼起来,中间用分隔符隔开,插入字符正常插入即可. 2.在这些串的Trie上建SAM.实际上并不需要建Trie,还是只需要正常插入(因为本来 ...
随机推荐
- h5-27-存储/读取JS对象
存储JS对象 <script type="text/javascript"> /*封装人员信息*/ function Person(id,name,age) { thi ...
- MySQLDump在使用之前一定要想到的事情 [转载]
转载于:http://blog.itpub.net/29254281/viewspace-1392757/ MySQLDump经常用于迁移数据和备份. 下面创建实验数据,两个数据库和若干表create ...
- Unity基础知识
hierarchy视图选中,点击scene视图,按f键聚焦 persp相当于是透视视野 在persp模式下,物体在scene界面上所呈现的画面是给人一种距离摄像头近的物体显示的大,距离摄像头远的物体显 ...
- java 替换字符串模板(模板渲染)
java渲染字符串模板,也就是说在java字符串模板中设置变量字符串,使用变量去渲染指定模板中设置好的变量字符串.下面介绍4种替换模板方式: 1.使用内置String.format String me ...
- LN : leetcode 215 Kth Largest Element in an Array
lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...
- jsonp对付同源策略
当 协议不同或者域名/ip不同或者端口号不同 , 都不算是同源 这时候 源生的ajax 就不能进行数据请求了 JSONP json with padding 在平时的开发中也发现了 ,当我们请求 ...
- FPGA内部RAM的初始化
Altera的RAM初始化文件格式是mif和hex. QuartusII自带的RAM初始化工具很方便产生初始化文件. Xilinx的RAM初始化文件格式是coe, 在vivado中软件会将coe文件变 ...
- IOS之pageControl
用户点击页面控件,会触发UIControlEventValueChanged事件,并启动设置为控件动作的任何方法.可以通过调用currentPage查询控件的新值,并通过调整numberOfPages ...
- 【HEVC帧间预测论文】P1.8 Complexity Control of High Efficiency Video Encoders for Power-Constrained Devices
参考:Complexity Control of High Efficiency Video Encoders for Power-Constrained Devices <HEVC标准介绍.H ...
- python基础一day4 元组
结果: join:返回一个字符串 列表转化为字符串 可迭代对象都可以 结果: 不报错什么也不执行 结果: