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,还是只需要正常插入(因为本来 ...
随机推荐
- Android-apk文件反编译
一:工具介绍及下载 1:apktool 作用:资源文件获取,可以提取出图片文件和布局文件进行使用查看 2:dex2jar 作用:将apk反编译成java源码(classes ...
- 微信小程序开发常见的拉起外部地图软件进行导航的功能
<view class="dh" bindtap="mapNavigation" data-addr="{{address}}"> ...
- Could not open logfile" occurred when run "datapatch -verbose"
CAUSE Due to Bug 25459405 - DATAPATCH FAILS WITH SP2-0768 IF NLS_LANGUAGE IS NOT SET TO AMERICANwhic ...
- 152 Maximum Product Subarray 乘积最大子序列
找出一个序列中乘积最大的连续子序列(该序列至少包含一个数).例如, 给定序列 [2,3,-2,4],其中乘积最大的子序列为 [2,3] 其乘积为 6.详见:https://leetcode.com/p ...
- Problem D: 勤奋的涟漪2 dp + 求导
http://www.gdutcode.sinaapp.com/problem.php?cid=1049&pid=3 dp[i][state]表示处理了前i个,然后当前状态是state的时候的 ...
- vue-cli下配置项目访问ip和服务器ip
一.配置项目访问ip,让本次代码支持localhost以外的ip地址访问模式:修改里的host配置,代码如下. 修改完记得 "npm run dev"重启服务. 二.在本地架设服务 ...
- sql通过 openrowset查询csv文件
两步即可完成 第一步. 创建cmmData.xml文件,并存入到能与sql服务器共享的文件夹中(如:\\10.252.21.6\sharedfolder) <?xml version=" ...
- react基础语法(一)元素渲染和基础语法规则
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 学习Python的一些Tips
0. Python安装 官网提供多种方式,一般Windows下直接安装exe即可:Linux下基本上自带python:另外也提供源码,也可自行编译: 若安装后无法使用,则检查一下环境变量是否设置正确. ...
- 第 6 章 Cinder - 061 - Boot from Volume
Boot from Volume Volume 除了可以用作 instance 的数据盘,也可以作为启动盘(Bootable Volume). 打开 instance 的 launch 操作界面. 这 ...