QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#243982 | #7513. Palindromic Beads | DaiRuiChen007 | WA | 2ms | 14168kb | C++14 | 2.6kb | 2023-11-08 20:04:01 | 2023-11-08 20:04:02 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+5;
int n;
struct SegmentTree {
#define mid ((l+r)>>1)
struct Node {
int ls,rs,max;
} tr[MAXN*150];
int rt[MAXN<<2],tot;
inline void ins(int ul,int ur,int k,int l,int r,int &p) {
if(!p) p=++tot;
if(ul<=l&&r<=ur) return tr[p].max=max(tr[p].max,k),void();
if(ul<=mid) ins(ul,ur,k,l,mid,tr[p].ls);
if(mid<ur) ins(ul,ur,k,mid+1,r,tr[p].rs);
}
inline int qry(int u,int l,int r,int p) {
if(!p||l==r) return tr[p].max;
return max(tr[p].max,u<=mid?qry(u,l,mid,tr[p].ls):qry(u,mid+1,r,tr[p].rs));
}
inline void Ins(int ul,int ur,int vl,int vr,int k,int l=1,int r=n,int p=1) {
// printf("Ins[%d,%d][%d,%d] <- [%d,%d] %d\n",ul,ur,vl,vr,l,r,p);
if(ul<=l&&r<=ur) return ins(vl,vr,k,1,n,rt[p]);
if(ul<=mid) Ins(ul,ur,vl,vr,k,l,mid,p<<1);
if(mid<ur) Ins(ul,ur,vl,vr,k,mid+1,r,p<<1|1);
}
inline int Qry(int u,int v,int l=1,int r=n,int p=1) {
// printf("Qry[%d][%d] <- [%d,%d] %d\n",u,v,l,r,p);
if(l==r) return qry(v,1,n,rt[p]);
return max(qry(v,1,n,rt[p]),u<=mid?Qry(u,v,l,mid,p<<1):Qry(u,v,mid+1,r,p<<1|1));
}
#undef mid
} T;
vector <int> G[MAXN];
int c[MAXN],fa[MAXN],L[MAXN],R[MAXN],dfn[MAXN],dep[MAXN],up[MAXN][20],dcnt,dp[MAXN];
inline void dfs(int u) {
L[u]=dfn[u]=++dcnt,dep[u]=dep[fa[u]]+1;
for(int k=1;k<20;++k) up[u][k]=up[up[u][k-1]][k-1];
for(int v:G[u]) if(v^fa[u]) fa[v]=up[v][0]=u,dfs(v);
R[u]=dcnt;
}
inline int LCA(int u,int v) {
if(dep[u]<dep[v]) swap(u,v);
for(int k=19;~k;--k) if(dep[up[u][k]]>=dep[v]) u=up[u][k];
if(u==v) return u;
for(int k=19;~k;--k) if(up[u][k]^up[v][k]) u=up[u][k],v=up[v][k];
return up[u][0];
}
inline int jump(int u,int rt) {
for(int k=19;~k;--k) if(dep[up[u][k]]>dep[rt]) u=up[u][k];
return u;
}
inline int dist(int u,int v) { return dep[u]+dep[v]-2*dep[LCA(u,v)]; }
int e[MAXN][2],ans=0;
struct Path { int u,v,w; };
vector <Path> P;
signed main() {
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%d",&c[i]),e[c[i]][e[c[i]][0]>0]=i;
for(int i=1,u,v;i<n;++i) scanf("%d%d",&u,&v),G[u].push_back(v),G[v].push_back(u);
dfs(1);
for(int i=1;i<=n;++i) if(e[i][1]) P.push_back({e[i][0],e[i][1],dist(e[i][0],e[i][1])});
sort(P.begin(),P.end(),[&](auto u,auto v){ return u.w<v.w; });
for(auto p:P) {
int len=2+(p.w>1),u=p.u,v=p.v;
if(dfn[u]>dfn[v]) swap(u,v);
len=max(len,T.Qry(dfn[u],dfn[v])+2),ans=max(ans,len);
// printf("len[%d,%d] = %d\n",u,v,len);
if(p.w==dep[v]-dep[u]) {
int z=jump(v,u);
if(1<L[z]) T.Ins(1,L[z]-1,L[v],R[v],len);
if(R[z]<n) T.Ins(L[v],R[v],R[z]+1,n,len);
} else T.Ins(L[u],R[u],L[v],R[v],len);
}
printf("%d\n",ans);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 14168kb
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: 2ms
memory: 10004kb
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: 14152kb
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: -100
Wrong Answer
time: 0ms
memory: 12040kb
input:
6 1 2 3 4 5 6 1 2 2 3 3 4 4 5 5 6
output:
0
result:
wrong answer 1st lines differ - expected: '1', found: '0'