Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并
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 .
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.
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 .
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
51
3
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树合并的更多相关文章
- 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 ...
- 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 ...
- 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. ...
- 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 ...
- Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分
B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...
- 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 ...
- 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 ...
- 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/ ...
- 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 ...
随机推荐
- POJ 2479
---恢复内容开始--- http://poj.org/problem?id=2479 #include <stdio.h> #include <iostream> using ...
- Delphi操作Excel大全
Delphi操作Excel大全 DELPHI操作excel(转)(一) 使用动态创建的方法 首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp ...
- java wait()和notify()、notifyAll()
图见<JAVA并发编程的艺术>P98-101 这三个方法都是java.lang.Object的方法,用于协调多个线程对共享数据的存取,必须在synchronized语句块中使用!这三个方法 ...
- 1. EasyUI 学习总结(一)——对话框dialog
文章参考来源:http://www.cnblogs.com/xdp-gacl/p/4075079.html 感谢博主的分享,写得非常精细,我在这边给看过的做一个记录. 一.EasyUI下载 使用eas ...
- Effective C++ -----条款36:绝不重新定义继承而来的non-virtual函数
绝对不要重新定义继承而来的non-virtual函数.
- Enum:Game of Lines(POJ 3668)
画直线 题目大意:给定一些点集,要你找两点之间的连线不平行的有多少条 数据量比较少,直接暴力枚举,然后放到set查找即可 #include <iostream> #include < ...
- mybatis setting配置
Mybatis配置报错元素类型为 "configuration" 的内容必须匹配 "(properties?,settings?,typeAliases?,typeHan ...
- 【QT】C++ GUI Qt4 学习笔记2
Go To Cell 利用QT Desinger做好界面后加入的代码有 gotocelldialog.h #ifndef GOTOCELLDIALOG_H #define GOTOCELLDIALOG ...
- IOS- 快速排序,冒泡排序,直接插入排序和折半插入排序,希尔排序,堆排序,直接选择排序
/*******************************快速排序 start**********************************///随即取 当前取第一个,首先找到第一个的位置 ...
- openssl/asn1.h file not found的解决方法
iOS 引入支付宝 缺少 #include <openssl/asn1.h> 报错 解决方法: 在 Building Settings -> Search Paths -& ...