답안 #404613

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404613 2021-05-14T18:06:00 Z rama_pang Hard route (IZhO17_road) C++17
100 / 100
844 ms 101652 KB
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  int N;
  cin >> N;
  vector<vector<int>> adj(N);
  for (int i = 1; i < N; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].emplace_back(v);
    adj[v].emplace_back(u);
  }

  // Let's fix a node u, and consider the 3 maximum distances
  // from u (where each is from different children). WLOG,
  // a <= b <= c. Then, the optimal hardness is c * (a + b).
  //
  // For (a, b, c) and node u, note that we can canonize the
  // route (a, b). For a fixed route (a, b), there might be
  // a lot of count(max(c)). However, if there is more than
  // 1 occurrence of length at least c, then at node u it
  // wouldn't be (a, b, c); instead it would be (a, c + x, c),
  // (we extend b to instead use the c-route since it's longer),
  // so it would be a different route. This way, c is unique
  // for a route (a, b) and only counted once.
  //
  // Now, we can use dynamic programming to count (a, b, c) in O(N).

  using lint = long long;
  array<lint, 2> ans = {0, 0};
  vector<array<lint, 2>> dn(N);
  vector<array<lint, 2>> up(N);

  const auto Add = [&](array<lint, 2> p, array<lint, 2> q) -> array<lint, 2> {
    if (p[1] == 0) return q;
    if (q[1] == 0) return p;
    if (p[0] != q[0]) return max(p, q);
    return {p[0], p[1] + q[1]};
  };

  const auto DfsDn = [&](const auto &self, int u, int p) -> void {
    dn[u] = {0, 1};
    for (auto v : adj[u]) if (v != p) {
      self(self, v, u);
      dn[u] = Add(dn[u], {dn[v][0] + 1, dn[v][1]});
    }
  };

  const auto DfsUp = [&](const auto &self, int u, int p) -> void {
    array<lint, 2> cur[2];
    cur[0] = up[u];
    cur[1] = {0, 0};
    for (auto v : adj[u]) if (v != p) {
      bool added = false;
      for (int i = 0; i < 2; i++) {
        if (cur[i][0] == dn[v][0] + 1) {
          cur[i][1] += dn[v][1];
          added = true;
          break;
        }
      }
      if (!added && dn[v][0] + 1 > cur[1][0]) {
        cur[1] = {dn[v][0] + 1, dn[v][1]};
        if (cur[1] > cur[0]) swap(cur[1], cur[0]);
      }
    }
    for (auto v : adj[u]) if (v != p) {
      if (dn[v][0] + 1 == cur[0][0] && dn[v][1] == cur[0][1]) {
        up[v] = {cur[1][0] + 1, cur[1][1]};
      } else if (dn[v][0] + 1 == cur[0][0]) {
        up[v] = {cur[0][0] + 1, cur[0][1] - dn[v][1]};
      } else {
        up[v] = {cur[0][0] + 1, cur[0][1]};
      }
      self(self, v, u);
    }
  };

  const auto DfsSolve = [&](const auto &self, int u, int p) -> void {
    array<lint, 4> cur[3];
    // (length, cnt, cnt if pick 2 different child, cnt if pick 3 different child)

    cur[0] = {up[u][0], up[u][1], 0, 0};
    cur[1] = {0, 0, 0, 0};
    cur[2] = {0, 0, 0, 0};

    for (auto v : adj[u]) if (v != p) {
      self(self, v, u);

      bool added = false;
      for (int i = 0; i < 3; i++) {
        if (cur[i][0] == dn[v][0] + 1) {
          cur[i][3] += dn[v][1] * cur[i][2];
          cur[i][2] += dn[v][1] * cur[i][1];
          cur[i][1] += dn[v][1];
          added = true;
          break;
        }
      }

      if (!added && dn[v][0] + 1 > cur[2][0]) {
        cur[2] = {dn[v][0] + 1, dn[v][1], 0, 0};
        if (cur[2] > cur[1]) swap(cur[2], cur[1]);
        if (cur[1] > cur[0]) swap(cur[1], cur[0]);
      }
    }

    if (cur[0][3] != 0) {         // Case a = b = c
      ans = Add(ans, {cur[0][0] * (cur[0][0] + cur[0][0]), cur[0][2]});
    } else if (cur[0][2] != 0) {  // Case a < b = c
      ans = Add(ans, {cur[0][0] * (cur[0][0] + cur[1][0]), cur[0][1] * cur[1][1]});
    } else if (cur[1][2] != 0) {  // Case a = b < c
      ans = Add(ans, {cur[0][0] * (cur[1][0] + cur[1][0]), cur[1][2]});
    } else {                      // Case a < b < c
      ans = Add(ans, {cur[0][0] * (cur[1][0] + cur[2][0]), cur[1][1] * cur[2][1]});
    }
  };

  int root = -1;
  for (int i = 0; i < N; i++) {
    if (adj[i].size() > 2) {
      root = i;
      break;
    }
  }

  if (root == -1) { // tree is line graph
    cout << 0 << ' ' << 1 << '\n';
    return 0;
  }

  DfsDn(DfsDn, root, -1);
  DfsUp(DfsUp, root, -1);
  DfsSolve(DfsSolve, root, -1);

  assert(ans[0] != 0 && ans[1] != 0);
  cout << ans[0] << ' ' << ans[1] << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 4 ms 1228 KB Output is correct
26 Correct 3 ms 1228 KB Output is correct
27 Correct 4 ms 1224 KB Output is correct
28 Correct 3 ms 1304 KB Output is correct
29 Correct 4 ms 1096 KB Output is correct
30 Correct 4 ms 1220 KB Output is correct
31 Correct 3 ms 1228 KB Output is correct
32 Correct 4 ms 1100 KB Output is correct
33 Correct 3 ms 1228 KB Output is correct
34 Correct 4 ms 1228 KB Output is correct
35 Correct 3 ms 1220 KB Output is correct
36 Correct 4 ms 1228 KB Output is correct
37 Correct 3 ms 712 KB Output is correct
38 Correct 3 ms 764 KB Output is correct
39 Correct 4 ms 972 KB Output is correct
40 Correct 3 ms 844 KB Output is correct
41 Correct 3 ms 844 KB Output is correct
42 Correct 3 ms 688 KB Output is correct
43 Correct 4 ms 716 KB Output is correct
44 Correct 3 ms 716 KB Output is correct
45 Correct 3 ms 716 KB Output is correct
46 Correct 3 ms 576 KB Output is correct
47 Correct 3 ms 716 KB Output is correct
48 Correct 3 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 320 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 4 ms 1228 KB Output is correct
26 Correct 3 ms 1228 KB Output is correct
27 Correct 4 ms 1224 KB Output is correct
28 Correct 3 ms 1304 KB Output is correct
29 Correct 4 ms 1096 KB Output is correct
30 Correct 4 ms 1220 KB Output is correct
31 Correct 3 ms 1228 KB Output is correct
32 Correct 4 ms 1100 KB Output is correct
33 Correct 3 ms 1228 KB Output is correct
34 Correct 4 ms 1228 KB Output is correct
35 Correct 3 ms 1220 KB Output is correct
36 Correct 4 ms 1228 KB Output is correct
37 Correct 3 ms 712 KB Output is correct
38 Correct 3 ms 764 KB Output is correct
39 Correct 4 ms 972 KB Output is correct
40 Correct 3 ms 844 KB Output is correct
41 Correct 3 ms 844 KB Output is correct
42 Correct 3 ms 688 KB Output is correct
43 Correct 4 ms 716 KB Output is correct
44 Correct 3 ms 716 KB Output is correct
45 Correct 3 ms 716 KB Output is correct
46 Correct 3 ms 576 KB Output is correct
47 Correct 3 ms 716 KB Output is correct
48 Correct 3 ms 716 KB Output is correct
49 Correct 602 ms 101648 KB Output is correct
50 Correct 617 ms 101576 KB Output is correct
51 Correct 623 ms 101588 KB Output is correct
52 Correct 616 ms 101652 KB Output is correct
53 Correct 501 ms 100964 KB Output is correct
54 Correct 528 ms 97176 KB Output is correct
55 Correct 496 ms 85756 KB Output is correct
56 Correct 502 ms 76068 KB Output is correct
57 Correct 549 ms 101292 KB Output is correct
58 Correct 558 ms 101284 KB Output is correct
59 Correct 557 ms 101320 KB Output is correct
60 Correct 573 ms 101420 KB Output is correct
61 Correct 382 ms 50380 KB Output is correct
62 Correct 375 ms 50424 KB Output is correct
63 Correct 844 ms 70916 KB Output is correct
64 Correct 822 ms 60720 KB Output is correct
65 Correct 802 ms 55772 KB Output is correct
66 Correct 821 ms 52476 KB Output is correct
67 Correct 820 ms 51528 KB Output is correct
68 Correct 790 ms 50900 KB Output is correct
69 Correct 779 ms 50608 KB Output is correct
70 Correct 783 ms 50496 KB Output is correct
71 Correct 778 ms 50320 KB Output is correct
72 Correct 809 ms 50624 KB Output is correct
73 Correct 822 ms 50628 KB Output is correct
74 Correct 796 ms 50760 KB Output is correct
75 Correct 764 ms 50704 KB Output is correct
76 Correct 746 ms 50808 KB Output is correct
77 Correct 635 ms 51288 KB Output is correct
78 Correct 415 ms 52552 KB Output is correct