/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 4ms 592.0 KiB
#2 Accepted 4ms 584.0 KiB
#3 Accepted 4ms 576.0 KiB
#4 Accepted 6ms 856.0 KiB
#5 Accepted 49ms 8.391 MiB
#6 Accepted 88ms 8.812 MiB
#7 Accepted 75ms 8.934 MiB
#8 Accepted 30ms 660.0 KiB
#9 Accepted 70ms 1.52 MiB
#10 Accepted 92ms 8.785 MiB
#11 Accepted 69ms 9.621 MiB
#12 Accepted 80ms 8.781 MiB
#13 Accepted 39ms 9.461 MiB
#14 Accepted 111ms 9.453 MiB
#15 Accepted 48ms 1.941 MiB
#16 Accepted 35ms 8.762 MiB
#17 Accepted 37ms 8.754 MiB
#18 Accepted 36ms 8.688 MiB
#19 Accepted 131ms 55.57 MiB
#20 Accepted 134ms 55.59 MiB
#21 Accepted 153ms 55.43 MiB
#22 Accepted 252ms 66.223 MiB
#23 Accepted 245ms 55.039 MiB
#24 Accepted 223ms 55.254 MiB
#25 Accepted 144ms 55.355 MiB
#26 Accepted 12ms 3.598 MiB

Code

#include<bits/stdc++.h>
using namespace std;

#define print(a) for(auto x:a)cout<<x<<' ';cout<<'\n';
#define debug(x) cout<<#x<<" "<<x<<'\n'
#define int   long long int

const int M = 1e9 + 7;
const int N = 2e5 + 10;

int anc[N][25],d[N];
vector<int>g[N];
void dfs(int u,int p=-1){
anc[u][0]=p;
for(int i=1;i<19;i++)
    anc[u][i]=~anc[u][i-1]?anc[anc[u][i-1]][i-1]:-1;
for(int v:g[u]){
    if(v==p)continue;
    d[v]=d[u]+1;
    dfs(v,u);
}
}
int lca(int u,int v){
if(d[u]<d[v])
    swap(u,v);
for(int i=18;~i;i--)
    if(d[u]-(1<<i)>=d[v])
    u=anc[u][i];
if(u==v)
    return u;
for(int i=18;~i;i--)
    if(anc[u][i]^anc[v][i])
    u=anc[u][i],v=anc[v][i];
return anc[u][0];
}
int dist(int u, int v){
    return d[u]+d[v]-2*d[lca(u,v)];
}

int a[N], tot[N];

void dfs1(int u, int p = -1){
   for(auto v : g[u]){
      if(v == p)continue;
      dfs1(v,u);
     // cout << u <<"->"<< v<<'\n';
      if(tot[v] > 0)tot[u] += (tot[v] + 2);
      else if(a[v]) tot[u] += 2;
   }
}

void solve(){
    int n; cin >> n;
    for(int i = 0; i <= n; i++){
      g[i].clear();a[i] = 0;tot[i] = 0;
    }

   
    int x = -1;
    for(int i = 0; i < n; i++){
      cin >> a[i];
      if(a[i])x = i;
    }

    for(int i = 1; i < n; i++){
      int u, v; cin >> u >> v, u--, v--;
      g[u].push_back(v);
      g[v].push_back(u);
    }
    dfs(0);

    if(x == -1){
      cout << 0 << '\n';return;
    }
    int y = x;

    for(int i = 0; i < n; i++){
      if(a[i] == 0)continue;
      if(dist(x, i) > dist(x, y)) y = i;
    }
    for(int i = 0; i < n; i++){
      if(a[i] == 0)continue;
      if(dist(y,i) > dist(y, x)) x = i;
    }

    if(x == y){
      cout << 0 << '\n';return;
    }

    dfs1(x);
   //for(int i = 0; i < n; i++)cout << tot[i] << ' ';cout << '\n';
    int res = tot[x] -  dist(x, y);
    cout << res << '\n';
}

signed main() {
   ios_base::sync_with_stdio (0);
   cin.tie (0);

   int t = 1;   cin >> t;
   for (int tc = 1; tc <= t; tc++) {
      //cout<<"Case "<<tc<<": ";
      solve();
   }
   return 0;
}

Information

Submit By
Type
Submission
Problem
P1078 Apple on Tree
Contest
Bangladesh 2.0
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-16 16:22:05
Judged At
2024-11-11 03:15:02
Judged By
Score
100
Total Time
252ms
Peak Memory
66.223 MiB