Submission #473737

# Submission time Handle Problem Language Result Execution time Memory
473737 2021-09-16T03:54:52 Z ntabc05101 Mag (COCI16_mag) C++14
84 / 120
740 ms 262148 KB
#include <bits/stdc++.h>
using namespace std;

const int MAX = 1 << 20;

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  return (long long)a.first * b.second < (long long)a.second * b.first;
}

int p[MAX];
int dpup[MAX], dpdown[MAX];
vector<int> L[MAX], R[MAX];
int lft[MAX], rgt[MAX];
vector<int> V[MAX];
pair<int, int> res = {1e9, 1};

void dfsdown(int node, int par) {
  L[node].push_back(0);
  for (auto it : V[node]) {
    if (it != par) {
      dfsdown(it, node);
      lft[it] = (int)L[node].size() - 1;
      L[node].push_back(max(L[node].back(), p[it] == 1 ? dpdown[it] + 1 : 0));
    }
  }

  R[node].push_back(0);
  for (int i = (int)V[node].size() - 1; i >= 0; i--) {
    int it = V[node][i];
    if (it != par) {
      rgt[it] = (int)R[node].size() - 1;
      R[node].push_back(max(R[node].back(), p[it] == 1 ? dpdown[it] + 1 : 0));
    }
  }

  dpdown[node] = L[node].back();
}

void dfsup(int node, int par) {
  if (node) {
    dpup[node] = (p[par] == 1 ? (1 + max(dpup[par], max(L[par][lft[node]], R[par][rgt[node]]))) : 0);
  }

  pair<int, int> a = {p[node], 1};
  if (cmp(a, res)) {
    res = a;
  }
  int mx = dpup[node];

  for (auto it : V[node]) {
    if (it == par) {
      continue;
    }

    int val = (p[it] == 1 ? dpdown[it] + 1 : 0);
    pair<int, int> tmp = {p[node], 1 + mx + val};
    if (cmp(tmp, res)) {
      res = tmp;
    }
    mx = max(mx, val);
    dfsup(it, node);
  }
}

int main()
{
  cin.tie(0)->sync_with_stdio(0);

  int n; cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b; cin >> a >> b;
    a--; b--;
    V[a].push_back(b);
    V[b].push_back(a);
  }

  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }

  dfsdown(0, -1);
  dfsup(0, -1);

  int gc = __gcd(res.first, res.second);
  cout << res.first / gc << "/" << res.second / gc << endl;

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 41 ms 74308 KB Output is correct
2 Correct 41 ms 74188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 74216 KB Output is correct
2 Correct 41 ms 74336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 591 ms 209616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 74180 KB Output is correct
2 Runtime error 583 ms 262148 KB Execution killed with signal 9
# Verdict Execution time Memory Grader output
1 Runtime error 548 ms 262148 KB Execution killed with signal 9
# Verdict Execution time Memory Grader output
1 Correct 648 ms 188136 KB Output is correct
2 Correct 504 ms 168396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 628 ms 190576 KB Output is correct
2 Correct 174 ms 87364 KB Output is correct
3 Runtime error 548 ms 262148 KB Execution killed with signal 9
# Verdict Execution time Memory Grader output
1 Correct 179 ms 87188 KB Output is correct
2 Correct 663 ms 188616 KB Output is correct
3 Correct 732 ms 140132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 622 ms 180700 KB Output is correct
2 Correct 680 ms 185556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 664 ms 189704 KB Output is correct
2 Correct 740 ms 133868 KB Output is correct