QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#523694#7513. Palindromic BeadsBreakPlusWA 9ms44784kbC++143.8kb2024-08-18 16:22:362024-08-18 16:22:37

Judging History

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

  • [2024-08-18 16:22:37]
  • 评测
  • 测评结果:WA
  • 用时:9ms
  • 内存:44784kb
  • [2024-08-18 16:22:36]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pb emplace_back
#define mkp make_pair
ll read(){ ll x; scanf("%lld",&x); return x; }
const ll mod = 998244353;
ll n, c[200005], dep[200005], u[200005], v[200005], cnt, dfn[200005], tim;
ll f[25][200005]; set<ll>S[200005];
vector<ll>col[200005], E[200005];
ll sz[200005], line[200005];
ll lg2(ll x){ return __builtin_clz(x) ^ 31; }
ll get(ll x,ll y){ return dfn[x]<dfn[y]?x:y; }
void dfs(ll x,ll fa){
//	cout<<"dep"<<x<<" : "<<dep[fa]+1<<endl;
	dep[x]=dep[fa]+1, sz[x]=1;
	dfn[x]=(++tim); line[tim]=x; f[0][dfn[x]]=fa;
	for(auto y: E[x]){
		if(y==fa) continue;
		dfs(y, x);
		S[x].insert(dfn[y]);
		sz[x]+=sz[y];
	}
}
ll LCA(ll x,ll y){
	if(x==y) return x;
	if((x=dfn[x])>(y=dfn[y])) swap(x,y);
	ll p=lg2(y-x++);
	return get(f[p][x], f[p][y-(1<<p)+1]);
}
ll dis(ll x,ll y){
//	cout<<"LCA "<<x<<","<<y<<" is "<<LCA(x,y)<<endl;
//	cout<<"dist "<<x<<","<<y<<" : "<< dep[x] + dep[y] - 2*dep[LCA(x, y)]<<endl;
	return dep[x] + dep[y] - 2*dep[LCA(x, y)];
}
struct Node{
	ll p,q,len;
	Node(ll x=0, ll y=0){
		p=x, q=y;
		if(!p || !q){
			len = 0;
		}else len=dis(x,y);
	}
}seq[200005];
ll dp[200005];
bool operator< (Node A, Node B){
	return A.len < B.len;
}

struct Tree{
	ll lc, rc, tag;
}t[20000005]; ll tot;
ll rt[800005], now;
void update(ll l,ll r,ll L,ll R,ll w,ll &pos){
	if(l>r) return;
	if(!pos) pos=(++tot);
	if(l<=L && R<=r) {
		t[pos].tag = max(t[pos].tag, w);
		return;
	}
	ll mid=L+R>>1;
	if(l<=mid) update(l,r,L,mid,w,t[pos].lc);
	if(mid<r)  update(l,r,mid+1,R,w,t[pos].rc);
}
void query(ll L,ll R,ll x,ll &pos){
	
	//cout<<"querying "<<L<<","<<R<<" found "<<t[pos].tag<<endl;
	now=max(now,t[pos].tag);
	if(L==R || !pos) return;
	ll mid=L+R>>1;
	if(x<=mid) query(L,mid,x,t[pos].lc);
	else query(mid+1,R,x,t[pos].rc);
}
void modify(ll l,ll r,ll L,ll R,ll p,ll q,ll w,ll pos){
	if(l>r || p>q) return;
	if(l<=L && R<=r){
		//cout<<"add on "<<L<<","<<R<<": "<<p<<","<<q<<" with "<<w<<endl;
		update(p, q, 1, n, w, rt[pos]);
		return;
	}
	ll mid=L+R>>1;
	if(l<=mid) modify(l,r,L,mid,p,q,w,pos<<1);
	if(mid<r)  modify(l,r,mid+1,R,p,q,w,pos<<1|1);
}
void enquire(ll x,ll y,ll L,ll R,ll pos){
	//cout<<"query at "<<L<<","<<R<<" in order to "<<x<<","<<y<<endl;
	query(1, n, y, rt[pos]);
	if(L==R){
		return;
	}
	ll mid=L+R>>1;
	if(x<=mid) enquire(x, y, L, mid, pos<<1);
	else enquire(x, y, mid+1, R, pos<<1|1);
}
void add(ll l1, ll r1,ll l2,ll r2,ll w){
	//cout<<"upd: "<<l1<<","<<r1<<" : "<<l2<<","<<r2<<" w = "<<w<<endl;
	modify(l1, r1, 1, n, l2, r2, w, 1);
}
void solve(){
	n=read();
	for(ll i=1;i<=n;i++) c[i]=read(), col[c[i]].pb(i);
	for(ll i=1;i<n;i++){
		u[i]=read(), v[i]=read();
		E[u[i]].pb(v[i]); E[v[i]].pb(u[i]);
	}
	dfs(1,0);
	for(ll i=1;i<=lg2(n);i++){
		for(ll j=1;j<=n-(1<<i)+1;j++){
			f[i][j] = get(f[i-1][j], f[i-1][j+(1<<i-1)]);
		}
	}

	for(ll i=1;i<=n;i++){
		if(col[i].size()==2){
			seq[++cnt] = Node(col[i][0], col[i][1]);
		}
	}
	sort(seq+1, seq+cnt+1);
	ll ans = 0;

	//cout<<"pressed"<<endl;
	for(ll i=1;i<=cnt;i++){
		if(seq[i].len == 1) dp[i] = 2;
		else dp[i] = 3;
		now = 0;
		ll p = dfn[seq[i].p], q = dfn[seq[i].q];
		if(p > q) swap(p, q), swap(seq[i].p, seq[i].q); 
		enquire(p, q, 1, n, 1);
		dp[i] = max(dp[i], now + 2);
		ans = max(ans, dp[i]);
		if(q < p + sz[seq[i].p]){
			ll son = line[*(--S[p].upper_bound(sz[q]))];
			add(1, dfn[son]-1, q, q+sz[seq[i].q]-1, dp[i]);
			add(q, q+sz[seq[i].q]-1, dfn[son]+sz[son], n, dp[i]);
		}else{
			add(p, p+sz[seq[i].p]-1, q, q+sz[seq[i].q]-1, dp[i]);
		}
	//	cout<<"pair: "<<seq[i].p<<","<<seq[i].q<<" dp = "<<dp[i]<<endl;
	}
	printf("%lld\n", ans);
}

int main(){
	ll T=1; while(T--) solve();
}




詳細信息

Test #1:

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

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

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

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: 9ms
memory: 40820kb

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'