D. Acyclic Organic Compounds
 

You are given a tree T with n vertices (numbered 1 through n) and a letter in each vertex. The tree is rooted at vertex 1.

Let's look at the subtree Tv of some vertex v. It is possible to read a string along each simple path starting at v and ending at some vertex in Tv (possibly v itself). Let's denote the number of distinct strings which can be read this way as .

Also, there's a number cv assigned to each vertex v. We are interested in vertices with the maximum value of .

You should compute two statistics: the maximum value of  and the number of vertices v with the maximum .

Input

The first line of the input contains one integer n (1 ≤ n ≤ 300 000) — the number of vertices of the tree.

The second line contains n space-separated integers ci (0 ≤ ci ≤ 109).

The third line contains a string s consisting of n lowercase English letters — the i-th character of this string is the letter in vertex i.

The following n - 1 lines describe the tree T. Each of them contains two space-separated integers u and v (1 ≤ u, v ≤ n) indicating an edge between vertices u and v.

It's guaranteed that the input will describe a tree.

Output

Print two lines.

On the first line, print  over all 1 ≤ i ≤ n.

On the second line, print the number of vertices v for which .

Examples
input
10
1 2 7 20 20 30 40 50 50 50
cacabbcddd
1 2
6 8
7 2
6 2
5 4
5 9
3 10
2 5
2 3
output
51
3
Note

In the first sample, the tree looks like this:

The sets of strings that can be read from individual vertices are:

Finally, the values of  are:

In the second sample, the values of  are (5, 4, 2, 1, 1, 1). The distinct strings read in T2 are ; note that can be read down to vertices 3 or 4.

trie树合并

比较考验代码能力

显然我就比较low

这份代码是cf抠来的

#include<iostream>
#include<vector>
#include<cassert>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; const int N = + ;
const int V = N * ; int n, tot;
int ch[V][], size[V];
int c[N];
int father[N];
vector<int> adj[N];
char label[N]; int merge(int u, int v)
{
if (u < ) return v;
if (v < ) return u;
int t = tot ++;
size[t] = ;
for(int c = ; c < ; ++ c) {
ch[t][c] = merge(ch[u][c], ch[v][c]);
if (ch[t][c] >= ) {
size[t] += size[ch[t][c]];
}
}
return t;
} void dfs(int u)
{
for(int c = ; c < ; ++ c) {
ch[u][c] = -;
}
for(int e = ; e < adj[u].size(); ++e) {
int v = adj[u][e];
if (v == father[u]) continue;
father[v] = u;
dfs(v);
int lab = label[v] - 'a';
ch[u][lab] = merge(ch[u][lab], v);
}
size[u] = ;
for(int x = ; x < ; ++ x) {
if (ch[u][x] >= ) {
size[u] += size[ch[u][x]];
}
}
c[u] += size[u];
} void solve()
{
cin >> n;
for(int i = ; i < n; ++ i) {
scanf("%d", c + i);
}
scanf("%s", label);
for(int i = ; i < n - ; ++ i) {
int u, v;
scanf("%d%d", &u, &v);
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
father[] = -;
tot = n;
dfs();
int ret = *max_element(c, c + n);
int cnt = ;
for(int i = ; i < n; ++ i) {
if (c[i] == ret) ++ cnt;
}
cout << ret << ' ' << cnt << endl;
} int main()
{
solve();
return ;
}

Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并的更多相关文章

  1. Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp

    C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  2. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树

    D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...

  3. Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树

    C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...

  4. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie

    题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...

  5. Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分

    B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...

  6. Codeforces Round #333 (Div. 2) C. The Two Routes flyod

    C. The Two Routes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/pro ...

  7. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  8. Codeforces Round #333 (Div. 2) A. Two Bases 水题

    A. Two Bases Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/problem/ ...

  9. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

随机推荐

  1. Binary Tree Vertical Order Traversal

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  2. cas 单点登录出现org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接 Connection refused

    cas 单点登录出现org.jasig.cas.client.util.CommonUtils.getResponseFromServer - 拒绝连接 Connection refused 环境: ...

  3. ffmpeg-20160517-git-bin-v2

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  4. diff & pattch 命令

    基础知识 该命令的功能为逐行比较两个文本文件,列出其不同之处.它比comm命令完成更复杂的检查.它对给出的文件进行系统的检查,并显示出两个文件中所有不同的行,不要求事先对文件进行排序. 语法:diff ...

  5. ASM:《X86汇编语言-从实模式到保护模式》1-4章:处理器,内存和硬盘基础

    其实很久之前就学完了实模式了,但是一直没有总结,感觉现在直接在书上做笔记的弊端就是有些知识点不能很很深刻地记下来(毕竟手写最明显的优点就是能深刻地记住知识,但是就是用太多的时间罢了).一下内容都是一些 ...

  6. 关于js事件委托

    由于事件处理程序可以为现代 Web 应用程序提供交互能力,因此许多开发人员会不分青红皂白地 向页面中添加大量的处理程序. 在创建 GUI 的语言(如 C#)中,为 GUI 中的每个按钮添加一个 onc ...

  7. 获得同级iframe页面的指定ID元素的几种实现方法

    1.JS实现: var object= window.parent.frames("要获得的iframe的name").contentDocument.getElementById ...

  8. HTML标记语法之列表元素

    1.无序列表 <ul> <li type=”项目符号类型”></li> <li type=”项目符号类型”></li> <li typ ...

  9. 模拟操作网页 webBrowser

    C# 获取IFrame中body元素 (winform) 方法1. 找出iframe的b.html的src , 利用webbrowser去加载b.html HtmlElementCollection ...

  10. c语言字符集

    一.字符常量 'A', 'B','\n','\'','1' 二.字符类型变量的赋值 char c1='A'; char c2='b'; char c3=65; c2='\''; c2='\n'; 三. ...