D. Acyclic Organic Compounds
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
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
Copy
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
Copy
51 3
input
Copy
6 0 2 4 1 1 1 raaaba 1 2 2 3 2 4 2 5 3 6
output
Copy
6 2
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
题目大意:求以任意一个节点为根的子树所能形成不同字符串的个数和改点权之和的最大值和出现的次数。
解题思路:将1到任意一个节点的hash值存下,然后就是dsu的裸题,问题就是hash函数的选取,用ull自动取膜的方法一直在14组wa掉了。换了一种hash方法过了。另外,将将hash数组离散化后可以去掉map少一个log
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define sca(x) scanf("%d",&x)
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define ull unsigned long long
const int N = 4e5+5;
const LL mod = 233333333333333333;
int sz[N],son[N];
string str;
vector<int>V[N];
LL c[N];
LL D[N];
int Son;
LL _hash[N];
void dfs_1(int u,int fa,ull now)
{
int mx=-1;sz[u]=1;
_hash[u]=(_hash[fa]*27+str[u-1]-'a'+1)%mod;
for(int i=0;i<V[u].size();i++){
int v=V[u][i];
if(v!=fa){
dfs_1(v,u,now);
sz[u]+=sz[v];
if(sz[v]>mx){
mx=sz[v];
son[u]=v;
}
}
}
}
int cnt;
map<LL,int>mmp;
void cal(int u,int fa,int val)
{
if(val==1) cnt+=(!mmp[_hash[u]]);
mmp[_hash[u]]+=val;
for(int i=0;i<V[u].size();i++)
{
int v=V[u][i];
if(v==fa||v==Son)continue;
cal(v,u,val);
}
}
void dfs_2(int u,int fa,int f)
{
for(int i=0;i<V[u].size();i++){
int v=V[u][i];
if(son[u]==v||v==fa)continue;
dfs_2(v,u,1);
}
if(son[u]) dfs_2(son[u],u,0),Son=son[u];
cal(u,fa,1);
D[u]=cnt;
if(son[u])Son=0;
if(f) cal(u,fa,-1),cnt=0;
}
int main()
{
int n;
sca(n);
for(int i=1;i<=n;i++){
scanf("%lld",&c[i]);
}
cin>>str;
for(int i=1;i<n;i++)
{
int u,v;
sca(u),sca(v);
V[u].pb(v);
V[v].pb(u);
}
_hash[0]=1;
dfs_1(1,0,0);
dfs_2(1,0,0);
LL mx=-1;
for(int i=1;i<=n;i++){
mx=max(mx,D[i]+c[i]);
}
int ans=0;
for(int i=1;i<=n;i++){
if(D[i]+c[i]==mx)ans++;
}
printf("%lld\n%d\n",mx,ans);
}
/*
6
1 1 1 1 1 1
abdcab
1 2
1 3
2 5
2 4
4 6
*/