This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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++){
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |