forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD.cpp
More file actions
118 lines (100 loc) · 2.71 KB
/
D.cpp
File metadata and controls
118 lines (100 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
const int MN = 300111;
const ll MOD = 1e9 + 7;
struct Hash {
ll x, y;
Hash() { x = y = 0; }
Hash(ll x, ll y) : x(x), y(y) {}
Hash operator * (int k) {
return Hash(x*k, y*k % MOD);
}
Hash operator + (const Hash& a) {
return Hash(x+a.x, (y+a.y) % MOD);
}
} p[MN], h[MN];
bool operator < (const Hash& a, const Hash& b) {
if (a.x != b.x) return a.x < b.x;
return a.y < b.y;
}
vector<int> ke[MN];
int cost[MN];
int a[MN];
set< Hash > all[MN];
int id[MN], n, nId;
int best = 0, cnt = 0;
void init() {
p[0] = Hash(1, 1);
FOR(i,1,MN-1) p[i] = p[i-1] * 31;
}
void dfs1(int u, int fu, int height) {
h[u] = p[height] * a[u] + h[fu];
for(int v : ke[u]) {
if (v == fu) continue;
dfs1(v, u, height + 1);
}
}
void dfs2(int u, int fu) {
id[u] = -1;
for(int v : ke[u]) {
if (v == fu) continue;
dfs2(v, u);
if (id[u] < 0 || SZ(all[id[u]]) < SZ(all[id[v]])) {
id[u] = id[v];
}
}
if (id[u] < 0) {
++nId;
id[u] = nId;
}
all[id[u]].insert(h[u]);
for(int v : ke[u]) {
if (v == fu) continue;
if (id[v] != id[u]) {
all[id[u]].insert(all[id[v]].begin(), all[id[v]].end());
}
}
int cur = SZ(all[id[u]]) + cost[u];
if (cur > best) {
best = cur;
cnt = 1;
}
else if (cur == best) {
++cnt;
}
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cout << (fixed) << setprecision(9);
init();
while (scanf("%d", &n) == 1) {
FOR(i,1,n) scanf("%d", &cost[i]);
FOR(i,1,n) all[i].clear();
FOR(i,1,n) {
char c = ' ';
while (c < 'a' || c > 'z') scanf("%c", &c);
a[i] = c - 'a' + 1;
}
FOR(i,1,n) ke[i].clear();
FOR(i,2,n) {
int u, v; scanf("%d%d", &u, &v);
ke[u].push_back(v);
ke[v].push_back(u);
}
// PR(cost, n);
// PR(a, n);
dfs1(1, 0, 1);
nId = 0;
best = 0; cnt = 0;
dfs2(1, 0);
cout << best << endl << cnt << endl;
}
}