/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 4.57 MiB
#2 Accepted 3ms 4.57 MiB
#3 Accepted 4ms 4.562 MiB
#4 Accepted 7ms 4.484 MiB
#5 Accepted 61ms 10.977 MiB
#6 Accepted 108ms 13.105 MiB
#7 Accepted 107ms 13.039 MiB
#8 Accepted 42ms 2.594 MiB
#9 Accepted 108ms 4.914 MiB
#10 Accepted 132ms 11.094 MiB
#11 Accepted 98ms 13.051 MiB
#12 Accepted 108ms 11.051 MiB
#13 Accepted 48ms 12.812 MiB
#14 Accepted 148ms 13.32 MiB
#15 Accepted 61ms 4.961 MiB
#16 Accepted 43ms 10.809 MiB
#17 Accepted 50ms 12.613 MiB
#18 Accepted 48ms 12.785 MiB
#19 Accepted 181ms 55.855 MiB
#20 Accepted 186ms 55.602 MiB
#21 Accepted 220ms 55.402 MiB
#22 Accepted 335ms 66.215 MiB
#23 Accepted 377ms 55.242 MiB
#24 Accepted 335ms 55.246 MiB
#25 Accepted 215ms 55.582 MiB
#26 Accepted 14ms 5.492 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-10-03 13:27:23
Judged By
Score
100
Total Time
377ms
Peak Memory
66.215 MiB