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,还是只需要正常插入(因为本来 ...
随机推荐
- selenium处理的操作
- 【C#】将数据库读出的数据转换为DataTable类型集合
return View(ConverDataReaderToDataTable(reader)); // 静态方法public static DataTable ConverDataReaderToD ...
- .net excel 导入 导出
哎,好好的代码今天说来个实验,结果用的是office15 气死人了,网上最高office14.dll 文章转自2012年 QQ群:13615607 MR.Young protected void Bt ...
- python工具之日志记录
''' 写日志类 日志存放目录为当前应用程序的目录下的log目录中 日志产生规则:每小时产生一个文件 write by :wujf 2017-02-24 ''' import sys import o ...
- IP地址 子网掩码 默认网关和DNS服务器的关系
在过去,男人是需要能够上房揭瓦的,是要能够修水管的.现在的男人是需要会装系统的,会设置路由器的.世界变化太快! 废话不多说,本文来讨论一下电脑上最为常见的几个网络参数:IP地址.子网掩码.默认网关和D ...
- 【HEVC简介】DB-DeBlock Filter
参考论文:HEVC Deblocking Filter <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见:http://www.cnblogs.com/DwyaneTalk/ ...
- android 解密工具
androguard. 这个是python写的 安装: pip install androguard
- vux安装
1. 在项目里安装vux cnpm install vux --save 2. 安装vux-loader cnpm install vux-loader --save-dev 3. 安装less-lo ...
- pooling需要注意的一个地方
max pooling 在不同的 depth 上是分开执行的,且不需要参数控制.也就是说,pooling之后,feature map的维度不会改变
- C-基础:关于预编译以及宏
这是没有引入任何头文件时,如果使用"NULL",编译器会报错:没有定义NULL.此时可用下面代码定义. #undef NULL //#undef 是在后面取消以前定义的宏定义#if ...