#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define endl '\n'
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define rep(x,s,e) for (auto x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n;
string s;
string t;
int p[200005];
int idx[200005];
int ans[200005];
vector<int> al[200005];
int in[200005];
int out[200005];
int par(int i){
if (p[i]==i) return i;
else return p[i]=par(p[i]);
}
int __TIME=0;
void dfs(int i){
in[i]=__TIME;
if (i<n) t+=s[i];
else{
rep(x,0,2){
dfs(al[i][x]);
if (x==0) __TIME++;
}
}
out[i]=__TIME;
}
int edges[100005][2];
int backedge[100005][18];
int len[100005];
int l[100005];
int r[100005];
int PREV;
int INDEX=1;
int getl(int L,int R){
int curr=l[L];
rep(x,18,0) if (backedge[curr][x]!=-1 && len[backedge[curr][x]]>(R-L+1)){
curr=backedge[curr][x];
}
if (len[curr]>(R-L+1)) curr=backedge[curr][0];
return curr;
}
int getr(int L,int R){
int curr=r[R];
rep(x,18,0) if (backedge[curr][x]!=-1 && len[backedge[curr][x]]>(R-L+1)){
curr=backedge[curr][x];
}
if (len[curr]>(R-L+1)) curr=backedge[curr][0];
return curr;
}
set<int> pos[200005];
void dfs2(int i){
if (i<n){
pos[i].insert(getl(in[i],in[i]));
}
else{
rep(x,0,2) dfs2(al[i][x]);
int l1=out[al[i][0]]-in[al[i][0]];
int l2=out[al[i][1]]-in[al[i][1]];
if (l1>l2){
swap(pos[i],pos[al[i][0]]);
rep(x,in[al[i][1]],out[al[i][1]]+1){
pos[i].insert(getr(in[i],x));
}
}
else{
swap(pos[i],pos[al[i][1]]);
rep(x,in[al[i][0]],out[al[i][0]]+1){
pos[i].insert(getl(x,out[i]));
}
}
//cout<<idx[i]<<" "<<sz(pos[i])<<endl;
ans[idx[i]]=sz(pos[i]);
}
//cout<<i<<" "<<in[i]<<" "<<out[i]<<endl;
//for (auto it:pos[i]) cout<<it<<" "; cout<<endl;
//cout<<endl;
}
signed main(){
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(false);
cin>>n;
cin>>s;
memset(idx,-1,sizeof(idx));
rep(x,0,200005) p[x]=x;
int IDX=n;
int a,b;
rep(x,1,n){
cin>>a>>b;
a--,b--;
int aa=par(a),bb=par(b);
al[IDX]={aa,bb};
p[aa]=p[bb]=IDX;
idx[IDX]=x;
IDX++;
}
dfs(IDX-1);
//cout<<t<<endl;
//rep(x,0,IDX) cout<<in[x]<<" "<<out[x]<<endl;
memset(edges,-1,sizeof(edges));
memset(backedge,-1,sizeof(backedge));
backedge[0][0]=0,backedge[1][0]=0;
len[0]=-1,len[1]=0;
PREV=0;
for (int x=0;x<t.size();x++){
while (t[x-len[PREV]-1]!=t[x])
PREV=backedge[PREV][0];
if (edges[PREV][t[x]-'0']==-1)
edges[PREV][t[x]-'0']=++INDEX;
int curr=edges[PREV][t[x]-'0'];
len[curr]=len[PREV]+2;
if (len[curr]==1){
backedge[curr][0]=1;
}
else{
int b=backedge[PREV][0];
while (t[x-len[b]-1]!=t[x]) b=backedge[b][0];
backedge[curr][0]=edges[b][t[x]-'0'];
}
int temp=backedge[curr][0];
rep(x,0,17){
if (temp==-1) break;
temp=backedge[curr][x+1]=backedge[temp][x];
}
PREV=r[x]=curr;
}
PREV=0;
for (int x=t.size()-1;x>=0;x--){
while (t[x+len[PREV]+1]!=t[x])
PREV=backedge[PREV][0];
int curr=edges[PREV][t[x]-'0'];
PREV=l[x]=curr;
}
//rep(x,0,n) cout<<l[x]<<" "; cout<<endl;
//rep(x,0,n) cout<<r[x]<<" "; cout<<endl;
dfs2(IDX-1);
rep(x,1,n) cout<<ans[x]<<endl;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:159:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
159 | for (int x=0;x<t.size();x++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
33236 KB |
Output is correct |
2 |
Correct |
18 ms |
33236 KB |
Output is correct |
3 |
Correct |
19 ms |
33220 KB |
Output is correct |
4 |
Correct |
17 ms |
33232 KB |
Output is correct |
5 |
Correct |
15 ms |
33236 KB |
Output is correct |
6 |
Correct |
16 ms |
33212 KB |
Output is correct |
7 |
Correct |
17 ms |
33144 KB |
Output is correct |
8 |
Correct |
19 ms |
33160 KB |
Output is correct |
9 |
Correct |
24 ms |
33264 KB |
Output is correct |
10 |
Correct |
18 ms |
33252 KB |
Output is correct |
11 |
Correct |
18 ms |
33236 KB |
Output is correct |
12 |
Correct |
24 ms |
33260 KB |
Output is correct |
13 |
Correct |
17 ms |
33192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
33236 KB |
Output is correct |
2 |
Correct |
18 ms |
33236 KB |
Output is correct |
3 |
Correct |
19 ms |
33220 KB |
Output is correct |
4 |
Correct |
17 ms |
33232 KB |
Output is correct |
5 |
Correct |
15 ms |
33236 KB |
Output is correct |
6 |
Correct |
16 ms |
33212 KB |
Output is correct |
7 |
Correct |
17 ms |
33144 KB |
Output is correct |
8 |
Correct |
19 ms |
33160 KB |
Output is correct |
9 |
Correct |
24 ms |
33264 KB |
Output is correct |
10 |
Correct |
18 ms |
33252 KB |
Output is correct |
11 |
Correct |
18 ms |
33236 KB |
Output is correct |
12 |
Correct |
24 ms |
33260 KB |
Output is correct |
13 |
Correct |
17 ms |
33192 KB |
Output is correct |
14 |
Correct |
19 ms |
33236 KB |
Output is correct |
15 |
Correct |
22 ms |
33388 KB |
Output is correct |
16 |
Correct |
20 ms |
33424 KB |
Output is correct |
17 |
Correct |
20 ms |
33380 KB |
Output is correct |
18 |
Correct |
22 ms |
33376 KB |
Output is correct |
19 |
Correct |
20 ms |
33408 KB |
Output is correct |
20 |
Correct |
27 ms |
33488 KB |
Output is correct |
21 |
Correct |
17 ms |
33492 KB |
Output is correct |
22 |
Correct |
17 ms |
33492 KB |
Output is correct |
23 |
Correct |
19 ms |
33444 KB |
Output is correct |
24 |
Correct |
18 ms |
33384 KB |
Output is correct |
25 |
Correct |
25 ms |
33524 KB |
Output is correct |
26 |
Correct |
16 ms |
33388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
56408 KB |
Output is correct |
2 |
Correct |
155 ms |
61328 KB |
Output is correct |
3 |
Correct |
70 ms |
56152 KB |
Output is correct |
4 |
Correct |
123 ms |
61732 KB |
Output is correct |
5 |
Correct |
92 ms |
57436 KB |
Output is correct |
6 |
Correct |
132 ms |
60720 KB |
Output is correct |
7 |
Correct |
81 ms |
57240 KB |
Output is correct |
8 |
Correct |
151 ms |
60244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
33236 KB |
Output is correct |
2 |
Correct |
18 ms |
33236 KB |
Output is correct |
3 |
Correct |
19 ms |
33220 KB |
Output is correct |
4 |
Correct |
17 ms |
33232 KB |
Output is correct |
5 |
Correct |
15 ms |
33236 KB |
Output is correct |
6 |
Correct |
16 ms |
33212 KB |
Output is correct |
7 |
Correct |
17 ms |
33144 KB |
Output is correct |
8 |
Correct |
19 ms |
33160 KB |
Output is correct |
9 |
Correct |
24 ms |
33264 KB |
Output is correct |
10 |
Correct |
18 ms |
33252 KB |
Output is correct |
11 |
Correct |
18 ms |
33236 KB |
Output is correct |
12 |
Correct |
24 ms |
33260 KB |
Output is correct |
13 |
Correct |
17 ms |
33192 KB |
Output is correct |
14 |
Correct |
19 ms |
33236 KB |
Output is correct |
15 |
Correct |
22 ms |
33388 KB |
Output is correct |
16 |
Correct |
20 ms |
33424 KB |
Output is correct |
17 |
Correct |
20 ms |
33380 KB |
Output is correct |
18 |
Correct |
22 ms |
33376 KB |
Output is correct |
19 |
Correct |
20 ms |
33408 KB |
Output is correct |
20 |
Correct |
27 ms |
33488 KB |
Output is correct |
21 |
Correct |
17 ms |
33492 KB |
Output is correct |
22 |
Correct |
17 ms |
33492 KB |
Output is correct |
23 |
Correct |
19 ms |
33444 KB |
Output is correct |
24 |
Correct |
18 ms |
33384 KB |
Output is correct |
25 |
Correct |
25 ms |
33524 KB |
Output is correct |
26 |
Correct |
16 ms |
33388 KB |
Output is correct |
27 |
Correct |
139 ms |
56408 KB |
Output is correct |
28 |
Correct |
155 ms |
61328 KB |
Output is correct |
29 |
Correct |
70 ms |
56152 KB |
Output is correct |
30 |
Correct |
123 ms |
61732 KB |
Output is correct |
31 |
Correct |
92 ms |
57436 KB |
Output is correct |
32 |
Correct |
132 ms |
60720 KB |
Output is correct |
33 |
Correct |
81 ms |
57240 KB |
Output is correct |
34 |
Correct |
151 ms |
60244 KB |
Output is correct |
35 |
Correct |
16 ms |
33168 KB |
Output is correct |
36 |
Correct |
280 ms |
60616 KB |
Output is correct |
37 |
Correct |
260 ms |
53328 KB |
Output is correct |
38 |
Correct |
339 ms |
61316 KB |
Output is correct |
39 |
Correct |
196 ms |
54136 KB |
Output is correct |
40 |
Correct |
194 ms |
60724 KB |
Output is correct |
41 |
Correct |
122 ms |
57192 KB |
Output is correct |
42 |
Correct |
133 ms |
60220 KB |
Output is correct |
43 |
Correct |
89 ms |
57256 KB |
Output is correct |
44 |
Correct |
116 ms |
60108 KB |
Output is correct |
45 |
Correct |
82 ms |
57088 KB |
Output is correct |
46 |
Correct |
299 ms |
82320 KB |
Output is correct |
47 |
Correct |
171 ms |
59100 KB |
Output is correct |