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. Shell数组相关操作

    参考:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html 创建数组 a=( ) 获得数组长度 ${#a[@]} #${#变量} ...

  2. Unity3d 残影效果(狂拽炫酷叼炸天)

    效果图,真的很叼啊 我根据别人的改进了一版,支持MeshFilter上的Mesh(需要确保Mesh的Read/Write是开启的否则不能正常工作) 非常感谢原作者给提供思路.http://blog.c ...

  3. 多线程同步_Monitor

    多线程一直在学习和理解中...... Monitor类是多线程中用以实现同步的一种技术,主要是同一进程内多线程间的同步技术. Monitor类中有以下几个方法需要注意: Monitor.Enter(o ...

  4. 2014北大研究生推免机试(校内)-复杂的整数划分(DP进阶)

    这是一道典型的整数划分题目,适合正在研究动态规划的同学练练手,但是和上一个随笔一样,我是在Coursera中评测通过的,没有找到适合的OJ有这一道题(找到的ACMer拜托告诉一声~),这道题考察得较全 ...

  5. javaURL中文乱码的问题

     中文乱码在java中URLEncoder.encode方法要调用两次解决   一.场景: 1.我在客户端要通过get方式调用服务器端的url,将中文参数做utf-8编码,需要在js中两次的进行编码, ...

  6. 【linux】学习4

    文件压缩: gzip :压缩   解压缩 zcat: 读取压缩文件 gzip text1  :压缩text1 得到 text1.gz 原文件不见了 gzip -c text1 > text1.g ...

  7. 【python】入门学习(三)

    for循环 for i in range():   #注意冒号 range中默认从0开始 或者从指定的数字开始 到给定数字的前一个数字结束 递增递减皆是如此 for循环提供变量的自动初始化 for i ...

  8. Codeforces Round #324 (Div. 2) C (二分)

    题目链接:http://codeforces.com/contest/734/problem/C 题意: 玩一个游戏,一开始升一级需要t秒时间,现在有a, b两种魔法,两种魔法分别有m1, m2种效果 ...

  9. NYOJ题目436sum of all integer numbers

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr0AAAHKCAIAAACBiWRrAAAgAElEQVR4nO3dP1LjSts34G8T5CyEFB

  10. Android缓存学习入门

    本文主要包括以下内容 利用LruCache实现内存缓存 利用DiskLruCache实现磁盘缓存 LruCache与DiskLruCache结合实例 利用了缓存机制的瀑布流实例 内存缓存的实现 pub ...