QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#523699 | #7513. Palindromic Beads | BreakPlus | RE | 6ms | 44944kb | C++14 | 3.8kb | 2024-08-18 16:27:10 | 2024-08-18 16:27:11 |
Judging History
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){
if(!pos) return;
//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){
if(l1 > r1 || l2 > r2) return;
//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 = 1;
//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: 6ms
memory: 44944kb
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: 4ms
memory: 44848kb
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: 44768kb
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: 38768kb
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: -100
Runtime Error
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...