QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#236962 | #7513. Palindromic Beads | by_chance | WA | 1ms | 12104kb | C++14 | 2.9kb | 2023-11-04 12:04:48 | 2023-11-04 12:04:48 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m,pos[N][2],dp[N],ans;
struct path{int u,v,dis;}a[N];
bool operator <(const path&a,const path&b){return a.dis<b.dis;}
vector<int> G[N];
int L[N],R[N],dcnt,fa[N][20],dep[N];
void dfs(int u){
L[u]=++dcnt;
for(int v:G[u])if(v!=fa[u][0])
fa[v][0]=u,dep[v]=dep[u]+1,dfs(v);
R[u]=dcnt;
}
void pre(){
for(int j=1;j<20;j++)
for(int i=1;i<=n;i++)
fa[i][j]=fa[fa[i][j-1]][j-1];
}
int LCA(int u,int v){
if(dep[u]<dep[v])swap(u,v);int d=dep[u]-dep[v];
for(int i=19;i>=0;i--)if((d>>i)&1)u=fa[u][i];
if(u==v)return u;
for(int i=19;i>=0;i--)
if(fa[u][i]!=fa[v][i])u=fa[u][i],v=fa[v][i];
return fa[u][0];
}
int dist(int u,int v){return dep[u]+dep[v]-2*dep[LCA(u,v)];}
int mx[N*50],ls[N*50],rs[N*50],tot;
void modify2(int p,int l,int r,int L,int R,int v){
if(l>=L&&r<=R){mx[p]=max(mx[p],v);return;}
int mid=(l+r)>>1;
if(L<=mid){
if(!ls[p])ls[p]=++tot;
modify2(ls[p],l,mid,L,R,v);
}
if(R>mid){
if(!rs[p])rs[p]=++tot;
modify2(rs[p],mid+1,r,L,R,v);
}
}
int query2(int p,int l,int r,int x){
if(!p)return 0;
if(l==r)return mx[p];
int mid=(l+r)>>1;
if(x<=mid)return max(mx[p],query2(ls[p],l,mid,x));
else return max(mx[p],query2(rs[p],mid+1,r,x));
}
int root[N<<2];
void modify1(int p,int l,int r,int L,int R,int x,int y,int v){
//if(p==1)printf("%d %d %d %d %d\n",L,R,x,y,v);
if(l>=L&&r<=R){modify2(root[p],1,n,x,y,v);return;}
int mid=(l+r)>>1;
if(L<=mid)modify1(p<<1,l,mid,L,R,x,y,v);
if(R>mid)modify1(p<<1|1,mid+1,r,L,R,x,y,v);
}
int query1(int p,int l,int r,int x,int y){
int mx=query2(root[p],1,n,y);
if(l==r)return mx;
int mid=(l+r)>>1;
if(x<=mid)return max(mx,query1(p<<1,l,mid,x,y));
else return max(mx,query1(p<<1|1,mid+1,r,x,y));
}
int main(){
scanf("%d",&n);
for(int i=1,x;i<=n;i++){
scanf("%d",&x);
if(!pos[x][0])pos[x][0]=i;
else pos[x][1]=i;
}
for(int i=1;i<=n;i++)
if(pos[i][1])a[++m]={pos[i][0],pos[i][1],0};
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);pre();
for(int i=1;i<=m;i++)
a[i].dis=dist(a[i].u,a[i].v);
sort(a+1,a+m+1);
for(int i=1;i<=(n<<2);i++)root[i]=++tot;
for(int i=1;i<=m;i++){
dp[i]=2+(a[i].dis!=1);int u=a[i].u,v=a[i].v;
//printf(" %d %d\n",L[u],L[v]);
dp[i]=max(dp[i],query1(1,1,n,L[u],L[v])+2);
dp[i]=max(dp[i],query1(1,1,n,L[v],L[u])+2);
ans=max(ans,dp[i]);
if(L[u]>L[v])swap(u,v);
if(L[v]>R[u])modify1(1,1,n,L[u],R[u],L[v],R[v],dp[i]);
else{
modify1(1,1,n,1,L[u],L[v],R[v],dp[i]);
if(R[u]!=n)modify1(1,1,n,R[u]+1,n,L[v],R[v],dp[i]);
}
}
printf("%d\n",ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 12104kb
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: 1ms
memory: 12024kb
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: 0ms
memory: 9988kb
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: 10068kb
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'