/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 5.02 MiB
#2 Accepted 5ms 5.023 MiB
#3 Accepted 5ms 5.156 MiB
#4 Accepted 8ms 5.27 MiB
#5 Accepted 46ms 12.27 MiB
#6 Accepted 75ms 12.75 MiB
#7 Accepted 74ms 12.855 MiB
#8 Accepted 33ms 5.223 MiB
#9 Accepted 73ms 6.0 MiB
#10 Accepted 97ms 12.805 MiB
#11 Accepted 75ms 13.43 MiB
#12 Accepted 83ms 12.582 MiB
#13 Accepted 41ms 13.27 MiB
#14 Accepted 116ms 13.145 MiB
#15 Accepted 48ms 6.656 MiB
#16 Accepted 34ms 12.746 MiB
#17 Accepted 41ms 12.52 MiB
#18 Accepted 41ms 12.562 MiB
#19 Accepted 135ms 55.547 MiB
#20 Accepted 141ms 55.512 MiB
#21 Accepted 172ms 55.352 MiB
#22 Accepted 252ms 66.223 MiB
#23 Accepted 261ms 55.246 MiB
#24 Accepted 221ms 55.25 MiB
#25 Accepted 151ms 55.586 MiB
#26 Accepted 13ms 7.898 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-08-16 16:22:05
Judged By
Score
100
Total Time
261ms
Peak Memory
66.223 MiB