답안 #473742

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
473742 2021-09-16T04:21:43 Z ntabc05101 Mag (COCI16_mag) C++14
84 / 120
602 ms 262148 KB
#include<bits/stdc++.h>
using namespace std;

const int MAX = 1000000;

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];
int lft[MAX], rgt[MAX];
vector<int> adj[MAX];
pair<int, int> res = {1e9, 1};

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

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

      dpup[it] = (p[node] == 1 ? (1 + max(L[lft[it]], R[rgt[it]])): 0);
    }
  }

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

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

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

  for (auto &it : adj[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() {
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }

  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--;
    adj[a].push_back(b);
    adj[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;
}

Compilation message

mag.cpp: In function 'int main()':
mag.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mag.cpp:71:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23824 KB Output is correct
2 Correct 14 ms 23804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23884 KB Output is correct
2 Correct 15 ms 23812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 523 ms 152680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23756 KB Output is correct
2 Runtime error 494 ms 262148 KB Execution killed with signal 9
# 결과 실행 시간 메모리 Grader output
1 Runtime error 498 ms 262148 KB Execution killed with signal 9
# 결과 실행 시간 메모리 Grader output
1 Correct 587 ms 75248 KB Output is correct
2 Correct 416 ms 62020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 540 ms 79812 KB Output is correct
2 Correct 120 ms 29484 KB Output is correct
3 Runtime error 517 ms 262148 KB Execution killed with signal 9
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 29380 KB Output is correct
2 Correct 569 ms 77384 KB Output is correct
3 Correct 496 ms 50488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 505 ms 75196 KB Output is correct
2 Correct 554 ms 73996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 602 ms 76692 KB Output is correct
2 Correct 497 ms 50560 KB Output is correct