/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 13ms 636.0 KiB
#2 Accepted 14ms 632.0 KiB
#3 Accepted 14ms 636.0 KiB
#4 Accepted 15ms 580.0 KiB
#5 Accepted 44ms 3.734 MiB
#6 Accepted 68ms 4.004 MiB
#7 Accepted 55ms 3.855 MiB
#8 Accepted 30ms 660.0 KiB
#9 Accepted 58ms 1.043 MiB
#10 Accepted 83ms 3.824 MiB
#11 Accepted 65ms 4.293 MiB
#12 Accepted 67ms 3.965 MiB
#13 Accepted 41ms 3.875 MiB
#14 Accepted 90ms 4.336 MiB
#15 Accepted 39ms 1.148 MiB
#16 Accepted 29ms 3.797 MiB
#17 Accepted 29ms 3.57 MiB
#18 Accepted 29ms 3.789 MiB
#19 Accepted 83ms 21.895 MiB
#20 Accepted 85ms 22.043 MiB
#21 Accepted 85ms 22.047 MiB
#22 Accepted 106ms 32.734 MiB
#23 Accepted 184ms 21.664 MiB
#24 Accepted 194ms 21.707 MiB
#25 Accepted 95ms 22.035 MiB
#26 Accepted 17ms 2.0 MiB

Code

#include <bits/stdc++.h>
#define int long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int mod = 1e9 + 7, N = 1e6;
int n;
vector<int> g[N + 3];

int mx1, mx2;
int ar[N + 3], vis[N + 3];
int dis[N + 3];
int lastNode;
int tracked[N + 3];
int par[N + 3];
int cost[N + 3];
int node2 = 0;
int mx = -1;
int node1 = 0;
void dfs1(int u)
{
  vis[u] = 1;

  if (dis[u] > mx && ar[u])
  {
    mx = dis[u];
    node1 = u;
  }

  for (auto v : g[u])
  {
    if (vis[v] == 0)
    {
      dis[v] = dis[u] + 1;

      dfs1(v);
    }
  }
}

void dfs2(int u)
{
  vis[u] = 1;

  if (dis[u] > mx && ar[u])
  {
    mx = dis[u];
    node2 = u;
  }

  for (auto v : g[u])
  {
    if (vis[v] == 0)
    {
      par[v] = u;
      dis[v] = dis[u] + 1;
      dfs2(v);
    }
  }
}
void dfs3(int u)
{
  vis[u] = 1;

  for (auto v : g[u])
  {
    if (vis[v] == 0)
    {

      dfs3(v);
    }
  }
  // cout << cost[u] <<
  if (cost[u])
  {
    cost[par[u]] += (cost[u] + 2);
  }
  else
  {
    if (ar[u])
    {
      cost[par[u]] += 2;
    }
  }

  // cout << u << "__ " << cost[par[u]] << endll;
}

void solve()
{
  cin >> n;
  mx = -1;
  node1 = 0;
  node2 = 0;
  for (int i = 1; i <= n; i++)
  {
    g[i].clear();
    vis[i] = par[i] = cost[i] = ar[i] = dis[i] = lastNode = tracked[i] = 0;
    mx1 = -1,
    mx2 = 0;
  }
  int c = 0;
  for (int i = 1; i <= n; i++)
  {
    cin >> ar[i];
    if (ar[i])
      c++;
  }

  for (int i = 1; i < n; i++)
  {
    int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  if (c <= 1)
  {
    cout << 0 << endl;
    return;
  }

  // node1
  dfs1(1);
  for (int i = 1; i <= n; i++)
    vis[i] = 0, dis[i] = 0;
  mx = -1;
  // node2
  dfs2(node1);
  int ans = -dis[node2];
  for (int i = 1; i <= n; i++)
    vis[i] = 0, dis[i] = 0;
  mx = -1;

  dfs3(node1);
  // cout << ans << " " << cost[node1] << endl;
  cout << ans + cost[node1] << endll;
}

int32_t main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--)
  {
    solve();
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1078 Apple on Tree
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-17 07:55:47
Judged At
2024-11-11 03:09:11
Judged By
Score
100
Total Time
194ms
Peak Memory
32.734 MiB