这一题有点坑。。注意longlong

SAM裸题。。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;struct Node
{int len;Node *f,*last,*ch[10];Node(){for(len=0;len<=9;len++)ch[len]=NULL;last=NULL;len=0;}
}*root,*last;inline void add(int data)
{Node *tp,*ne=new Node;ne->len=last->len+1;ne->f=last;for(tp=last;tp&&!tp->ch[data];tp=tp->last)tp->ch[data]=ne;if(!tp)ne->last=root,last=ne;else if(tp->len==tp->ch[data]->len-1)ne->last=tp->ch[data],last=ne;else{Node *a=new Node,*b=tp->ch[data];*a=*b;ne->last=b->last=a;a->len=tp->len+1;last=ne;a->f=tp;for(tp;tp&&tp->ch[data]==b;tp=tp->last)tp->ch[data]=a;}
}
struct Chain
{Chain*next;int u;Chain(){next=NULL;}
}*Head[1000001];inline void addside(int a,int b)
{Chain *tp=new Chain;tp->u=b,tp->next=Head[a],Head[a]=tp;
}
int color[100001];
void DFS(int u,int fa)
{for(Chain *tp=Head[u];tp;tp=tp->next)if(fa^tp->u)add(color[tp->u]),DFS(tp->u,u);last=last->f;
}int n,C;
char c;
inline void read(int &a)
{a=0;do c=getchar();while(c<'0'||c>'9');while(c<='9'&&c>='0')a=(a<<3)+(a<<1)+c-'0',c=getchar();
}
int Sort[1000001];
int H[1000001];
inline bool cmp(int x,int y)
{return H[x]<H[y];}
long long ans;
void Solve(Node *tp)
{if(tp->last)ans+=tp->len-tp->last->len;for(int i=0;i<=9;i++)if(tp->ch[i]&&tp->ch[i]->len==tp->len+1)Solve(tp->ch[i]);
}
int main()
{read(n),read(C);int i,j,k;for(i=1;i<=n;i++)Sort[i]=i,read(color[i]);root=last=new Node;for(k=1;k<n;k++)read(i),read(j),addside(i,j),addside(j,i),H[i]++,H[j]++;sort(Sort+1,Sort+1+n,cmp);for(i=1;i<=n;i++)if(H[Sort[i]]==1){last=root;add(color[Sort[i]]);DFS(Sort[i],0);}else break;Solve(root);printf("%lld\n",ans);return 0;
}

BZOJ3926: [Zjoi20150]诸神眷顾的幻想乡相关推荐

  1. 【bzoj3926】[Zjoi20150]诸神眷顾的幻想乡 后缀自动机+trie

    Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日. 粉丝们非常热情,自发组织表演了一系列节目给幽香看. ...

  2. [bzoj3926][Zjoi2015]诸神眷顾的幻想乡

    来自FallDream的博客,未经允许, 请勿转载,谢谢. 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日. 粉丝们非常热情 ...

  3. 【BZOJ3926】[Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机

    [BZOJ3926][Zjoi2015]诸神眷顾的幻想乡 Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝 ...

  4. bzoj3926【ZJOI2015】诸神眷顾的幻想乡

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 790  Solved: 485 [Submit][ ...

  5. [Zjoi2015]诸神眷顾的幻想乡

    [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 1537  Solved: 892 Description 幽香 ...

  6. [ZJOI2015] 诸神眷顾的幻想乡

    P3256[ZJOI2015 Day1]诸神眷顾的幻想乡 时间限制 : 20000 MS   空间限制 : 524288 KB 问题描述 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生 ...

  7. bzoj 3926 [Zjoi2015]诸神眷顾的幻想乡

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec   Memory Limit: 512 MB Submit: 1381   Solved: 811 [ Subm ...

  8. 【BZOJ 3926】【ZJOI 2015】 诸神眷顾的幻想乡

    [BZOJ 3926/ZJOI 2015] 诸神眷顾的幻想乡 [广义SA/广义SAM] 没想到陈立杰居然玩旧作- 题意: \;\;\;\;啊?~幻想乡?~一个树状太阳花田上每个点都有人穿着 c c种颜 ...

  9. bzoj 3926: [Zjoi2015]诸神眷顾的幻想乡(广义后缀自动机)

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec   Memory Limit: 512 MB Submit: 1009   Solved: 596 [ Subm ...

最新文章

  1. python运行错误怎么查找_求助,python的二分法查找,按照视频上的代码写下来,结果运行错误...
  2. java TreeMap用法
  3. 开发日记-20190718 关键词 读书笔记《Linux 系统管理技术手册(第二版)》DAY 11
  4. 8、REVOKE:删除用户权限
  5. 20145201 《信息安全系统设计基础》课程总结
  6. JVM调优:CMS的执行过程及存在的问题
  7. U-Boot顶层Makefile分析
  8. Django 缓存、序列化、信号
  9. lintcode-397-最长上升连续子序列
  10. 【扩频通信】基于matlab CDMA直接序列扩频系统仿真【含Matlab源码 1528期】
  11. c语言编程中每个指令含义,C语言编程中我们会遇见一些指令,在这里列了一些出来...
  12. Mac上实时网速、内存等显示
  13. 倾斜摄影超大场景的三维模型的顶层合并常见的问题分析
  14. windows7浏览器无法打开html,win7网页无法打开出现HTML Parsing Error的解法
  15. Android Studio 实战干货例程
  16. EASYRECOVERY_3.3.29包含注册机、都教授数据恢复含注册码
  17. Mybatis-04
  18. 质量改进实践之质量月报
  19. Caffe Layer 系列(一):Input层、Data层
  20. sql题一(空位连续座位买票)

热门文章

  1. php 解析 %e5%80%aa%e9%a3%9e,content.json
  2. 使用ashx文件响应来自JQuery的JSON请求
  3. 高精度GNSS定位的校正服务——状态域(SSR)校正和观测域(OSR)校正
  4. 开集识别(Open Set Recognition, OSR)算法:《Towards Open Set Deep Networks》OpenMax
  5. 在centeros搭建workpress
  6. ppt转换成pdf工具
  7. 【毕业设计】深度学习昆虫识别系统 - 图像识别 opencv python
  8. 计算机行业就业的发展前景怎么样?
  9. Java面试java基础,重庆java哪家培训班好
  10. Power BI中如何处理相同名称的客户