QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198773#7513. Palindromic Beadspsc233RE 3ms19432kbC++172.7kb2023-10-03 17:09:402023-10-03 17:09:41

Judging History

你现在查看的是最新测评结果

  • [2024-03-27 16:34:54]
  • hack成功,自动添加数据
  • (/hack/584)
  • [2024-03-27 16:18:45]
  • hack成功,自动添加数据
  • (/hack/583)
  • [2023-10-03 17:09:41]
  • 评测
  • 测评结果:RE
  • 用时:3ms
  • 内存:19432kb
  • [2023-10-03 17:09:40]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
const int D=18;
struct segment{
	int lson,rson;
	int ans;
}tree[5000000];
int n,ans,cnt,dfstime;
int c[N],f[N][D+1],dep[N],root[N],sz[N],dfn[N];
vector<int>e[N],id[N];
int new_node(){
	cnt++; tree[cnt].lson=tree[cnt].rson=tree[cnt].ans=0;
	return cnt;
}
void dfs(int x,int y){
	f[x][0]=y;dep[x]=dep[y]+1;dfn[x]=++dfstime;sz[x]=1;
	for (int i=1;i<=D;i++) f[x][i]=f[f[x][i-1]][i-1];
	for (int i=0;i<e[x].size();i++){
		int u=e[x][i];
		if (u==y) continue;
		dfs(u,x);
		sz[x]+=sz[u];
	}
}
int get_lca(int x,int y){
	if (dep[x]<dep[y]) swap(x,y);
	for (int i=D;i>=0;i--) if (dep[f[x][i]]>=dep[y]) x=f[x][i];
	if (x==y) return x;
	for (int i=D;i>=0;i--) if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
	return f[x][0];
}
int up(int x,int y){
	for (int i=D;i>=0;i--) if ((y>>i)&1) x=f[x][i];
	return x;
}
int merge(int x,int y){
	if (!x||!y) return x+y;
	int t=new_node();
	tree[t].lson=merge(tree[x].lson,tree[y].lson);
	tree[t].rson=merge(tree[x].rson,tree[y].rson);
	tree[t].ans=max(tree[x].ans,tree[y].ans);
	return t;
}
int findans(int l,int r,int k,int ql,int qr){
	if (!k) return 0;
	if (l>=ql&&r<=qr) return tree[k].ans;
	else{
		int mid=(l+r)/2;
		int ans=0;
		if (ql<=mid) ans=max(ans,findans(l,mid,tree[k].lson,ql,qr));
		if (qr>mid) ans=max(ans,findans(mid+1,r,tree[k].rson,ql,qr));
		return ans;
	}
}
void change(int l,int r,int k,int x,int y){
	tree[k].ans=max(tree[k].ans,y);
	if (l==r) return;
	else{
		int mid=(l+r)/2;
		if (x<=mid){
			if (!tree[k].lson) tree[k].lson=new_node();
			change(l,mid,tree[k].lson,x,y);
		}else{
			if (!tree[k].rson) tree[k].rson=new_node();
			change(mid+1,r,tree[k].rson,x,y);
		}
	}
}
void dfs1(int x,int y){
	root[x]=new_node();
	for (int i=0;i<e[x].size();i++){
		int u=e[x][i];
		if (u==y) continue;
		dfs1(u,x);
		root[x]=merge(root[x],root[u]);
	}
	if ((int)id[c[x]].size()>=2){
		int u=id[c[x]][0]+id[c[x]][1]-x;
		int lca=get_lca(u,x);
		if (lca!=x){
			int s=0;
			int v=x;
			if (lca==u){
				x=up(x,dep[x]-dep[u]-1);
				if (dfn[x]>1) s=max(s,findans(1,n,root[v],1,dfn[x]-1));
				if (dfn[x]+sz[x]<=n) s=max(s,findans(1,n,root[v],dfn[x]+sz[x],n));
				s+=2;
			}else{
				s=max(s,findans(1,n,root[v],dfn[u],dfn[u]+sz[u]-1));
				s+=2;
			}
			if (!(f[u][0]==v||f[v][0]==u)) ans=max(ans,s+1); else ans=max(ans,s);
			change(1,n,root[v],dfn[u],s);
		}
	}
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&c[i]);
	for (int i=1;i<=n;i++) 
		id[c[i]].push_back(i);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		e[x].push_back(y);
		e[y].push_back(x);
	}
	ans=1;
	dfs(1,0);
	dfs1(1,0);
	printf("%d\n",ans);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 19284kb

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: 18356kb

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: 3ms
memory: 19376kb

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: 18316kb

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: 0ms
memory: 19432kb

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: -100
Runtime Error

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:


result: