QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#192908 | #7513. Palindromic Beads | ucup-team1004# | WA | 4ms | 37596kb | C++14 | 3.6kb | 2023-09-30 15:52:18 | 2023-09-30 15:52:18 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
if(x.empty())return out<<"[]";
out<<'['<<x[0];
for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
cerr<<x<<' ',debug(y...);
}
const int N=2e5+10,K=3e7;
int n,c[N];
vector<int>pos[N],to[N];
int siz[N],fa[N],son[N],top[N],dep[N];
void dfs1(int u){
siz[u]=1,dep[u]=dep[fa[u]]+1;
for(int v:to[u])if(v^fa[u]){
fa[v]=u,dfs1(v);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])son[u]=v;
}
}
int dft,bg[N],ed[N];
void dfs2(int u,int t){
top[u]=t,bg[u]=++dft;
if(son[u])dfs2(son[u],t);
for(int v:to[u])if(v^fa[u]&&v^son[u])dfs2(v,v);
ed[u]=dft;
}
int LCA(int u,int v){
for(;top[u]^top[v];u=fa[top[u]]){
if(dep[top[u]]<dep[top[v]])swap(u,v);
}
return dep[u]<dep[v]?u:v;
}
vector<int>t[N<<2];
void insert(int x,int y,int l=1,int r=n,int rt=1){
t[rt].push_back(y);
if(l==r)return;
int mid=(l+r)>>1;
if(x<=mid)insert(x,y,l,mid,rt<<1);
else insert(x,y,mid+1,r,rt<<1|1);
}
int k;
struct tree{
int ls,rs,val;
}v[K];
int bd(int l,int r){
int rt=++k;
if(l==r)return rt;
int mid=(l+r)>>1;
v[rt].ls=bd(l,mid);
v[rt].rs=bd(mid+1,r);
return rt;
}
void upd(int rt,int L,int R,int x,int l,int r){
if(L<=l&&r<=R){
v[rt].val=max(v[rt].val,x);return;
}
int mid=(l+r)>>1;
if(L<=mid)upd(v[rt].ls,L,R,x,l,mid);
if(mid<R)upd(v[rt].rs,L,R,x,mid+1,r);
}
int qry(int rt,int x,int l,int r){
if(l==r)return v[rt].val;
int mid=(l+r)>>1,s=v[rt].val;
if(x<=mid)s=max(s,qry(v[rt].ls,x,l,mid));
else s=max(s,qry(v[rt].rs,x,mid+1,r));
return s;
}
int o[N<<2];
void build(int l=1,int r=n,int rt=1){
if(!t[rt].empty()){
sort(t[rt].begin(),t[rt].end());
t[rt].erase(unique(t[rt].begin(),t[rt].end()),t[rt].end());
o[rt]=bd(0,t[rt].size()-1);
}
if(l==r)return;
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
}
int trs(int rt,int x){
return lower_bound(t[rt].begin(),t[rt].end(),x)-t[rt].begin();
}
void update(int L,int R,int x,int y,int z,int l=1,int r=n,int rt=1){
if(L<=l&&r<=R){
if(o[rt])upd(o[rt],trs(rt,x),trs(rt,y),z,0,t[rt].size()-1);
return;
}
int mid=(l+r)>>1;
if(L<=mid)update(L,R,x,y,z,l,mid,rt<<1);
if(mid<R)update(L,R,x,y,z,mid+1,r,rt<<1|1);
}
int query(int x,int y,int l=1,int r=n,int rt=1){
int s=o[rt]?qry(o[rt],trs(rt,y),0,t[rt].size()-1):0;
if(l==r)return s;
int mid=(l+r)>>1;
if(x<=mid)s=max(s,query(x,y,l,mid,rt<<1));
else s=max(s,query(x,y,mid+1,r,rt<<1|1));
return s;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&c[i]);
for(int i=1,u,v;i<n;i++){
scanf("%d%d",&u,&v);
to[u].push_back(v),to[v].push_back(u);
}
dfs1(1),dfs2(1,1);
for(int i=1;i<=n;i++)pos[c[i]].push_back(i);
vector<tuple<int,int,int,int> >p;
for(int i=1;i<=n;i++){
if(pos[i].size()<2)continue;
int x=pos[i][0],y=pos[i][1];
if(bg[x]>bg[y])swap(x,y);
insert(bg[x],bg[y]);
int t=LCA(x,y);
p.push_back({dep[x]+dep[y]-dep[t]*2,x,y,t});
}
build();
int ans=1;
sort(p.begin(),p.end());
for(auto pt:p){
int d,x,y,t;
tie(d,x,y,t)=pt;
int res=query(bg[x],bg[y]);
if(d>1)res=max(res,1)+2;
else res=2;
ans=max(ans,res);
if(t^x){
update(bg[x],ed[x],bg[y],ed[y],res);
}else{
if(bg[x]>1)update(1,bg[x]-1,bg[y],ed[y],res);
if(ed[x]<n)update(ed[x]+1,n,bg[y],ed[y],res);
}
}
cout<<ans<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 36704kb
input:
4 1 1 2 2 1 2 2 3 2 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 37596kb
input:
5 1 3 2 2 1 1 2 2 3 3 4 4 5
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 2ms
memory: 36556kb
input:
6 1 1 2 2 3 3 1 2 2 3 3 4 4 5 5 6
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 0ms
memory: 35292kb
input:
6 1 2 3 4 5 6 1 2 2 3 3 4 4 5 5 6
output:
1
result:
ok single line: '1'
Test #5:
score: -100
Wrong Answer
time: 4ms
memory: 36928kb
input:
2000 845 1171 345 282 1181 625 754 289 681 493 423 840 1494 318 266 1267 967 379 135 14 39 191 60 972 116 1216 1205 19 194 185 1360 861 379 430 1262 1151 756 65 389 488 277 53 1283 1438 101 1465 195 714 737 980 80 298 961 1326 163 1163 1317 1152 992 35 334 802 1502 486 710 234 555 88 1278 146 46 696...
output:
15
result:
wrong answer 1st lines differ - expected: '5', found: '15'