QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#193484#7513. Palindromic Beadsucup-team918#WA 210ms58376kbC++204.3kb2023-09-30 17:15:262023-09-30 17:15:26

Judging History

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

  • [2024-03-27 16:34:54]
  • hack成功,自动添加数据
  • (/hack/584)
  • [2024-03-27 16:18:45]
  • hack成功,自动添加数据
  • (/hack/583)
  • [2023-09-30 17:15:26]
  • 评测
  • 测评结果:WA
  • 用时:210ms
  • 内存:58376kb
  • [2023-09-30 17:15:26]
  • 提交

answer

//#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200005
#define mod 998244353
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define ld long double
#define SZ(x) (int)(x.size())
#define debug cout<<endl<<"ff"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define fi first
#define se second
#define INF 1e9
#define pq priority_queue
#define rep(x,a,b) for(int x=a;x<=b;x++)
int qpow(int a,int b){
	int res=1;
	for(;b;b>>=1){
		if(b&1) res=res*a%mod;
		a=a*a%mod;
	}
	return res;
}
/*int fac[N],ifac[N];
int C(int n,int m){
	if(m>n||m<0||n<0) return 0;
	return fac[n]*ifac[n-m]%mod*ifac[m]%mod;
}
void init(){
	fac[0]=1;
	for(int i=1;i<N;i++) fac[i]=fac[i-1]*i%mod;
	ifac[N-1]=qpow(fac[N-1],mod-2);
	for(int i=N-2;i>=0;i--) ifac[i]=ifac[i+1]*(i+1)%mod;
}*/
/*struct node{
	int nxt,to;
}e[N<<1];
int cnt=1,head[N];
inline void add(int x,int y){
	e[++cnt].nxt=head[x];
	head[x]=cnt;
	e[cnt].to=y;
}*/
inline int lowbit(int x){return x&(-x);}
inline int read(){
  int x=0,t=1;char ch=getchar();
  while(ch<'0'||ch>'9'){
    if(ch=='-') t=-1;
    ch=getchar();
  }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch-'0');
        ch=getchar();
    }
    return x*t;
}
inline void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>=10) write(x/10);
	putchar(x%10+'0');
}
int dp[N],T,n,col[N][2],c[N],dis[N],dfn[N],dep[N],sz[N],st[20][N],cnt,num,p[N],Rt[N];
struct node{int tag,ls,rs;}tr[N*190];
vector<int>g[N];
int cmp(int x,int y){return dis[x]<dis[y];}
void dfs(int x,int fa){
	sz[x]=1;dep[x]=dep[fa]+1;st[0][x]=fa;dfn[x]=++cnt;
	for(auto t:g[x]){
		if(t==fa) continue;
		dfs(t,x);sz[x]+=sz[t];
	}
}
int LCA(int x,int y){
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=19;i>=0;i--)
		if(dep[st[i][x]]>=dep[y]) 
			x=st[i][x];
	if(x==y) return x;
	for(int i=19;i>=0;i--)
		if(st[i][x]!=st[i][y])
			x=st[i][x],y=st[i][y];
	return st[0][x];
}
void changey(int &rt,int l,int r,int x,int y,int w){
	if(!rt) rt=++num;
	if(x<=l&&r<=y){
		tr[rt].tag=max(tr[rt].tag,w);
		return;
	}
	int mid=(l+r)>>1;
	if(x<=mid) changey(tr[rt].ls,l,mid,x,y,w);
	if(y>mid) changey(tr[rt].rs,mid+1,r,x,y,w);
}
void changex(int rt,int l,int r,int lx,int rx,int ly,int ry,int w){
	if(lx<=l&&r<=rx){
		changey(Rt[rt],1,n,ly,ry,w);
		return;
	}
	int mid=(l+r)>>1;
	if(lx<=mid) changex((rt<<1),l,mid,lx,rx,ly,ry,w);
	if(rx>mid) changex(((rt<<1)|1),mid+1,r,lx,rx,ly,ry,w);
}
int queryy(int rt,int l,int r,int y){
	if(!rt) return 0;
	if(l==r) return tr[rt].tag;
	int res=tr[rt].tag,mid=(l+r)>>1;
	if(y<=mid) res=max(res,queryy(tr[rt].ls,l,mid,y));
	else res=max(res,queryy(tr[rt].rs,mid+1,r,y));
	return res;
}
int queryx(int rt,int l,int r,int x,int y){
	if(!rt) return 0;
	if(l==r) return queryy(Rt[rt],1,n,y);
	int res=queryy(Rt[rt],1,n,y),mid=(l+r)>>1;
	if(x<=mid) res=max(res,queryx((rt<<1),l,mid,x,y));
	else res=max(res,queryx(((rt<<1)|1),mid+1,r,x,y));
	return res;
} 
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>c[i];
		if(col[c[i]][0]) col[c[i]][1]=i;
		else col[c[i]][0]=i;
	}
	for(int i=1;i<n;i++){
		int x,y;cin>>x>>y;
		g[x].pb(y);g[y].pb(x);
	}
	dfs(1,0);
	for(int i=1;i<=n;i++){
		if(col[i][1]==0) dis[i]=0;
		else dis[i]=dep[col[i][0]]+dep[col[i][1]]-2*dep[LCA(col[i][0],col[i][1])];
		p[i]=i;
	}
	int ans=1;
	sort(p+1,p+n+1,cmp);
	for(int i=1;i<=n;i++){
		int x=p[i];
		if(!col[x][1]) continue;
		dp[x]=2;if(st[0][col[x][0]]!=col[x][1]&&st[0][col[x][1]]!=col[x][0]) dp[x]++;
		if(dep[col[x][0]]<dep[col[x][1]]) swap(col[x][0],col[x][1]);
		int L=col[x][0],R=col[x][1];
		dp[x]=max(dp[x],max(queryx(1,1,n,dfn[L],dfn[R]),queryx(1,1,n,dfn[R],dfn[L]))+2);
		ans=max(ans,dp[x]);
		//R是L祖先 
	//	cout<<x<<"+"<<dp[x]<<endl;
		if(dfn[col[x][1]]<=dfn[col[x][0]]&&dfn[col[x][1]]+sz[col[x][1]]-1>=dfn[col[x][0]]){
			int now=L;
			for(int j=19;j>=0;j--)
				if(dep[st[j][now]]>dep[R])
					now=st[j][now];
			R=now;
			if(dfn[R]!=1) changex(1,1,n,dfn[L],dfn[L]+sz[L]-1,1,dfn[R]-1,dp[x]);
			if(dfn[R]+sz[R]<=n) changex(1,1,n,dfn[L],dfn[L]+sz[L]-1,dfn[R]+sz[R],n,dp[x]);
		}else{changex(1,1,n,dfn[L],dfn[L]+sz[L]-1,dfn[R],dfn[R]+sz[R]-1,dp[x]);}
	} 
	cout<<ans<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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

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: 4ms
memory: 18240kb

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
Wrong Answer
time: 210ms
memory: 58376kb

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:

13

result:

wrong answer 1st lines differ - expected: '5', found: '13'