QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#198715 | #7513. Palindromic Beads | ucup-team1525 | WA | 402ms | 44592kb | C++17 | 3.6kb | 2023-10-03 16:46:47 | 2023-10-03 16:46:49 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e5;
int n;
int col[N+5],mc[N+5];
int lst[N+5];
vector<int> e[N+5];
namespace Segtr{
vector<int> tr[N*4+5];
#define lc id<<1
#define rc id<<1|1
void build(int l,int r,int id){
tr[id].clear();
tr[id].shrink_to_fit();
if(l==r) return;
int mid=l+r>>1;
build(l,mid,lc); build(mid+1,r,rc);
}
void insert(int l,int r,int st,int en,int x,int id){
// fprintf(stderr,"%d %d %d %d\n",l,r,st,en);
// if(en<l||st>r) while(1);
// while(1)
if(st<=l&&en>=r){
if(!tr[id].size()||x>=tr[id].back())
tr[id].push_back(x);
return;
}
int mid=l+r>>1;
if(st<=mid) insert(l,mid,st,en,x,lc);
if(en>mid) insert(mid+1,r,st,en,x,rc);
}
void getback(int l,int r,int st,int en,int x,int id){
if(st<=l&&en>=r){
if(tr[id].size()&&tr[id].back()==x) tr[id].pop_back();
return;
}
int mid=l+r>>1;
if(st<=mid) getback(l,mid,st,en,x,lc);
if(en>mid) getback(mid+1,r,st,en,x,rc);
}
int query(int l,int r,int p,int id){
int ans=0;
if(tr[id].size()) ans=tr[id].back();
if(l==r) return ans;
int mid=l+r>>1;
if(p<=mid) ans=max(ans,query(l,mid,p,lc));
else ans=max(ans,query(mid+1,r,p,rc));
return ans;
}
}
using namespace Segtr;
int ans;
bool used[N+5];
int fv[N+5],fu[N+5];
int sz[N+5],son[N+5];
void dfs1(int u,int tf){
sz[u]=1; son[u]=0;
for(auto v:e[u]){
if(v!=tf&&!used[v]){
dfs1(v,u);
if(sz[v]>sz[son[u]])
son[u]=v;
sz[u]+=sz[v];
}
}
}
int dfn[N+5],dfns;
void dfs2(int u,int tf,int tv,int tu){
fu[u]=tu; fv[u]=tv;
sz[u]=1;
dfn[u]=++dfns;
for(auto v:e[u])
if(!used[v]&&v!=tf){
dfs2(v,u,!tv?v:tv,tu);
sz[u]+=sz[v];
}
}
void dfs4(int u,int tf){
int res=0;
if(mc[u]){
int v=mc[u];
if(fv[v]!=fv[u]&&fu[v]==fu[u]){
res=query(1,dfns,dfn[v],1)+2;
// fprintf(stderr,"%d %d %d %d\n",1,dfns,dfn[v],dfn[v]+sz[v]-1);
// for(int i=1,j=0;i<=1000000000;i++) j++;
insert(1,dfns,dfn[v],dfn[v]+sz[v]-1,res,1);
ans=max(ans,res);
}
}
for(auto v:e[u])
if(!used[v]&&v!=tf) dfs4(v,u);
if(mc[u]){
int v=mc[u];
if(fv[v]!=fv[u]&&fu[u]==fu[v])
getback(1,dfns,dfn[v],dfn[v]+sz[v]-1,res,1);
}
}
void solve(int u){
dfs1(u,0);
int tot=sz[u];
if(tot==1){
used[u]=1; return;
}
while(sz[son[u]]*2>tot) u=son[u];
dfns=0;
dfs2(u,0,0,u);
build(1,dfns,1);
insert(1,dfns,1,dfns,1,1);
int delv=0;
if(mc[u]&&fu[mc[u]]==u){
int v=mc[u];
delv=fv[v];
insert(1,dfns,dfn[v],dfn[v]+sz[v]-1,2+(fv[v]!=v),1);
ans=max(ans,2+(fv[v]!=v));
}
for(auto v:e[u])
if(!used[v]&&v!=delv) dfs4(v,u);
used[u]=1;
for(auto v:e[u])
if(!used[v]) solve(v);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&col[i]);
if(lst[col[i]]){
int j=lst[col[i]];
mc[i]=j; mc[j]=i;
}
lst[col[i]]=i;
}
for(int i=1,u,v;i<n;i++){
scanf("%d %d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
ans=1;
solve(1);
printf("%d\n",ans);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 30544kb
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: 28500kb
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: 32504kb
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: 3ms
memory: 30484kb
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: 0
Accepted
time: 7ms
memory: 30588kb
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:
5
result:
ok single line: '5'
Test #6:
score: 0
Accepted
time: 307ms
memory: 42668kb
input:
200000 48015 47923 20609 71806 43752 68214 95683 89449 25809 58110 19878 52931 7845 45206 86245 82945 62977 37876 12456 105915 10509 92943 66950 88545 26442 26545 42278 66977 3970 9631 21524 43638 7979 58240 25719 56260 276 89721 9553 16550 52161 30307 82748 108443 36676 48581 59069 57412 62453 7965...
output:
5
result:
ok single line: '5'
Test #7:
score: 0
Accepted
time: 324ms
memory: 42740kb
input:
200000 13011 51198 65374 107045 66506 14385 35784 94265 71449 41817 24646 60714 53382 68358 9354 840 3139 71282 72215 69550 2121 41498 13675 76444 67690 40513 56439 12832 51976 35333 47208 59602 98993 9383 77866 10464 41517 89125 58804 91741 66160 74208 70991 63865 84870 14282 2441 78046 73372 36311...
output:
7
result:
ok single line: '7'
Test #8:
score: 0
Accepted
time: 349ms
memory: 43196kb
input:
200000 38715 33241 65919 39407 27500 36200 2259 42301 79147 57505 20 81399 69499 23658 14534 86934 14352 69558 59763 43318 35360 3281 38188 40058 40571 103709 75625 8434 53802 87159 98628 69421 53711 47986 18350 6079 37362 39377 71936 89573 25983 66882 48999 58918 66432 17453 82515 9588 95375 87287 ...
output:
45
result:
ok single line: '45'
Test #9:
score: -100
Wrong Answer
time: 402ms
memory: 44592kb
input:
200000 36449 57574 3145 38591 832 17710 66613 78947 27635 83275 89878 48329 94614 584 96832 9321 72046 44873 5396 61452 63224 63740 26579 13706 108490 19092 89439 85884 12016 5105 48638 74004 41569 35006 22276 45609 25350 49906 35479 15875 68938 77699 48828 21628 11242 77040 70838 45771 27704 64865 ...
output:
129
result:
wrong answer 1st lines differ - expected: '135', found: '129'