BZOJ 3926: [Zjoi20150]诸神眷顾的幻想乡(后缀自动机)
被这道题坑了= =
只与一个空地相连的空地不超过20个
只与一个空地相连的空地不超过20个
因为很重要所以说两遍
就是说儿子节点最多只有20个
把这20个节点作为根遍历一遍所得到的tire所得到的所有不同子串就是答案了
怎么求?
这可是CLJ出的啊
想想她讲过什么
后缀自动机或可持久化后缀数组的经典应用
由于不会打可持久化后缀数组,就打了个自动机
自己对后缀自动机根本不熟,找时间在多做几道题
CODE:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
struct node{
node* c[];int id;
node(){memset(c,,sizeof(c));}
};
#define maxn 101000
vector<int> e[maxn];
#define pb push_back
int a[maxn],pre[maxn];
inline void bfs(int x,node *&y){
if (!y) y=new node;
static int q[maxn];
static node * p[maxn];
q[]=x;
p[]=y;
pre[x]=;
for (int l=,r=,u=q[];l<=r;u=q[++l]) {
for (vector<int>::iterator i=e[u].begin();i!=e[u].end();++i) {
if (*i==pre[u]) continue;
pre[*i]=u;q[++r]=*i;
if (!p[l]->c[a[*i]]) p[l]->c[a[*i]]=new node;
p[r]=p[l]->c[a[*i]];
}
}
}
struct snode{int ch[],l,fa;}s[maxn*];
typedef long long ll;
int cnt;ll ans;
inline void add(int x,node* u) {
int p=++cnt,t=u->id;
u->c[x]->id=p;
s[p].l=s[t].l+;
for (;t!=-&&!s[t].ch[x];t=s[t].fa) s[t].ch[x]=p;
if (t==-) s[p].fa=;
else if (s[t].l+==s[s[t].ch[x]].l) s[p].fa=s[t].ch[x];
else {
int r=++cnt,q=s[t].ch[x];
s[r]=s[q];s[r].l=s[t].l+;
s[p].fa=s[q].fa=r;
for (;t!=-&&s[t].ch[x]==q;t=s[t].fa) s[t].ch[x]=r;
}
ans+=s[p].l-s[s[p].fa].l;
}
int c;
node *root;
inline void build(){
static node *q[maxn*];
q[]=root;
root->id=;
s[].fa=-;
for (int l=,r=;l<=r;l++) {
node *u=q[l];
for (int i=;i<=c;i++)
if (u->c[i]) {
add(i,u);
q[++r]=u->c[i];
}
}
}
int main(){
int n;
scanf("%d%d",&n,&c);
for (int i=;i<=n;i++) scanf("%d",a+i);
for (int i=;i<=n;i++) a[i]++;
root=new node;
for (int i=;i<n;i++) {
int x,y;
scanf("%d%d",&x,&y);
e[x].pb(y);e[y].pb(x);
}
for (int i=;i<=n;i++) if (e[i].size()==) bfs(i,root->c[a[i]]);
build();
printf("%lld\n",ans);
return ;
}
X$MX.png)
BZOJ 3926: [Zjoi20150]诸神眷顾的幻想乡(后缀自动机)的更多相关文章
- BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡(后缀自动机)
传送门 解题思路 因为叶节点不超过\(20\)个,所以可以枚举这些叶节点,并把这些节点当做根扫整棵树.可以证明所有的子串一定可以被便利到,然后可以对这些串建广义后缀自动机.\(dfs\)的时候要记录一 ...
- BZOJ 3926: [Zjoi20150]诸神眷顾的幻想乡
3926: [Zjoi20150]诸神眷顾的幻想乡 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 438 Solved: 273 Descripti ...
- 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岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日. 粉丝们非常热情,自发组织表演了一系列节目给幽香看. ...
随机推荐
- web前端面试第三波~
快来测试测试自己掌握能力吧! 1. class.forname的作用?为什么要用? 1).获取Class对象的方式:类名.class.对象.getClass().Class.forName(" ...
- [Angular Tutorial] 9 -Routing & Multiple Views
在这一步中,您将学到如何创建一个布局模板,并且学习怎样使用一个叫做ngRoute的Angular模块来构建一个具有多重视图的应用. ·当您现在访问/index.html,您将被重定向到/index.h ...
- BZOJ1237: [SCOI2008]配对
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1237 题目大意:你有n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一 ...
- centos 软件库安装
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the mo ...
- Mysql中各种常见数据库存储引擎对比
- 【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)
HTTP is not just for serving up web pages. It's also a powerful platform for building APIs that expo ...
- UGUI batch 规则和性能优化
UGUI batch 规则和性能优化 (基础) Unity 绘图性能优化 - Draw Call Batching : http://docs.unity3d.com/Manual/DrawCallB ...
- jQuery 在Table中选择input之类的东西注意事项
jQuery 在Table中选择input之类的东西注意事项: 如果不在td标签中,是不能进行正确选择的: <table id="tblFormId"> <tr& ...
- 关于Vue.js 使用v-cloak后仍显示变量的解决方法
v-cloak 这个指令是防止页面加载时出现 vuejs 的变量名而设计的,但有时候添加了这个指令仍会显示变量,这是怎么回事呢?. v-cloak 用法: HTML代码: <div v-cl ...
- wx小程序初体验
小程序最近太火,不过相比较刚发布时,已经有点热度散去的感觉,不过这不影响我们对小程序的热情,开发之前建议通读下官网文档,附链接:https://mp.weixin.qq.com/debug/wxado ...