QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#193621#7513. Palindromic BeadsPhantomThreshold#WA 3ms31128kbC++204.0kb2023-09-30 17:35:152023-09-30 17:35:15

Judging History

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

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

answer

#include<bits/stdc++.h>
#define ll long long
//#define int long long
using namespace std;

const int maxn = 205000;
const int maxp = 100005*18;
const int maxd = 18;

vector<int>V[maxn],col[maxn];

int n;
int ci[maxn];

int dfn[maxn],dfi,siz[maxn],c1[maxn],sum1[maxn];
int fa[maxn][maxd],dep[maxn];
void dfs(const int x)
{
	for(int d=1;d<maxd;d++)
		fa[x][d]= fa[ fa[x][d-1] ][d-1];
	
	sum1[x]=sum1[fa[x][0]]+c1[x];
	dfn[x]=++dfi;
	siz[x]=1;
	for(auto y:V[x]) if(y!=fa[x][0])
	{
		fa[y][0]=x;
		dep[y]=dep[x]+1;
		dfs(y);
		siz[x]+=siz[y];
	}
}
int lca(int x,int y)
{
	if(dep[x]<dep[y]) swap(x,y);
	for(int d=maxd-1;d>=0;d--) if(dep[x]-dep[y]>=(1<<d))
		x=fa[x][d];
	if(x==y) return x;
	for(int d=maxd-1;d>=0;d--) if(fa[x][d]!=fa[y][d])
		x=fa[x][d],y=fa[y][d];
	return fa[x][0];
}

vector< pair<int,int> >Va[maxn];

inline void up(int &a,const int &b){ if(a<b)a=b; }
struct segment1
{
	int tot;
	int seg[maxp],lc[maxp],rc[maxp];
	void init()
	{
		tot=0;
	}
	int newnode()
	{
		++tot;
		seg[tot]=lc[tot]=rc[tot]=0;
		return tot;
	}
	int lx,rx,loc,c;
	void upd(int &x,const int l,const int r)
	{
		if(!x) x=newnode();
		up(seg[x],c);
		if(l==r) return;
		int mid=(l+r)>>1;
		if(loc<=mid) upd(lc[x],l,mid);
		else upd(rc[x],mid+1,r);
	}
	int query(const int x,const int l,const int r)
	{
		if(rx<l||r<lx||!x) return 0;
		if(lx<=l&&r<=rx) return seg[x];
		int mid=(l+r)>>1;
		return max(query(lc[x],l,mid),query(rc[x],mid+1,r));
	}
}seg0;
struct segment0
{
	int seg[maxn<<2];
	int lx,rx,ly,ry;
	int updx,updy,updc;
	void upd(const int x,const int l,const int r)
	{
		seg0.loc=updy,seg0.c=updc;
		seg0.upd(seg[x],1,n);
		if(l==r) return;
		int mid=(l+r)>>1,lc=x<<1,rc=lc|1;
		if(updx<=mid) upd(lc,l,mid);
		else upd(rc,mid+1,r);
	}
	int query(const int x,const int l,const int r)
	{
		if(rx<l||r<lx) return 0;
		if(lx<=l&&r<=rx)
		{
			seg0.lx=ly,seg0.rx=ry;
			return seg0.query(seg[x],1,n);
		}
		int mid=(l+r)>>1,lc=x<<1,rc=lc|1;
		return max(query(lc,l,mid),query(rc,mid+1,r));
	}
}seg1;

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin>>n;
	for(int i=1;i<=n;i++) 
	{
		cin>>ci[i];
		col[ci[i]].emplace_back(i);
	}
	for(int i=1;i<n;i++)
	{
		int x,y; cin>>x>>y;
		V[x].push_back(y);
		V[y].push_back(x);
	}
	
	for(int i=1;i<=n;i++) c1[i]= col[ci[i]].size()==1;
	dep[1]=1; dfs(1);
	
	for(int i=1;i<=n;i++) if(col[i].size()==2)
	{
		int x=col[i][0],y=col[i][1];
		int dis= dep[x]+dep[y]-2*dep[lca(x,y)];
		Va[dis].emplace_back( x,y );
	}
	
	seg0.init();
	
	int ans=0;
	for(int d=n;d>=1;d--)
	{
		for(auto [x,y]:Va[d])
		{
			if(dfn[x]>dfn[y]) swap(x,y);
			
			if(dfn[x]<dfn[y] && dfn[x]+siz[x]>dfn[y])
			{
				int val=0;
				seg1.lx=1,seg1.rx=dfn[x]-1;
				seg1.ly=dfn[y]+1,seg1.ry=dfn[y]+siz[y]-1;
				if(seg1.lx<=seg1.rx && seg1.ly<=seg1.ry)
					up(val, seg1.query(1,1,n) );
				
				seg1.lx=dfn[x]+1,seg1.rx=dfn[y]-1;
				if(seg1.lx<=seg1.rx && seg1.ly<=seg1.ry)
					up(val, seg1.query(1,1,n) );
				
				seg1.lx=dfn[y]+1,seg1.rx=dfn[y]+siz[y]-1;
				seg1.ly=dfn[y]+siz[y],seg1.ry=dfn[x]+siz[x]-1;
				if(seg1.lx<=seg1.rx && seg1.ly<=seg1.ry)
					up(val, seg1.query(1,1,n) );
				
				seg1.ly=dfn[x]+siz[x],seg1.ry=n;
				if(seg1.lx<=seg1.rx && seg1.ly<=seg1.ry)
					up(val, seg1.query(1,1,n) );
				
				val++;
				seg1.updx=dfn[x],seg1.updy=dfn[y],seg1.updc=val;
				seg1.upd(1,1,n);
				
				int ff=x;
				int tc= d>1; //( sum1[x]+sum1[y]-sum1[ff]-sum1[fa[ff][0]]-c1[x]-c1[y] )>0;
				up(ans,val*2+tc);
			}
			else
			{
				int val=0;
				seg1.lx=dfn[x]+1,seg1.rx=dfn[x]+siz[x]-1;
				seg1.ly=dfn[y]+1,seg1.ry=dfn[y]+siz[y]-1;
				if(seg1.lx<=seg1.rx && seg1.ly<=seg1.ry)
					up(val, seg1.query(1,1,n) );
				
				val++;
				seg1.updx=dfn[x],seg1.updy=dfn[y],seg1.updc=val;
				seg1.upd(1,1,n);
				
				int ff=lca(x,y);
				int tc= d>1; //( sum1[x]+sum1[y]-sum1[ff]-sum1[fa[ff][0]]-c1[x]-c1[y] )>0;
				up(ans,val*2+tc);
			}
		}
	}
	
	cout<<ans<<endl;
	
	return 0;
}

詳細信息

Test #1:

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

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

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

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

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'