답안 #881780

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
881780 2023-12-01T22:08:10 Z MattTheNub Amusement Park (JOI17_amusement_park) C++17
98 / 100
19 ms 3756 KB
#include <bits/stdc++.h>
using namespace std;

template <class T> using v = vector<T>;

#include "Joi.h"

#define all(x) begin(x), end(x)
#define f first
#define s second
using int2 = pair<int, int>;

// communicate 60 bits of information in 120 moves
struct DSU {
  v<int> e;
  DSU(int n) { e = v<int>(n, -1); }

  int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
  bool same_set(int a, int b) { return get(a) == get(b); }
  int size(int x) { return -e[get(x)]; }

  bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x == y) {
      return false;
    }
    if (e[x] > e[y]) {
      swap(x, y);
    }
    e[x] += e[y];
    e[y] = x;
    return true;
  }
};

void Joi(int N, int M, int A[], int B[], long long X, int T) {
  v<int> depth(N);
  v<v<int>> adj(N);
  DSU dsu(N);
  for (int i = 0; i < M; i++) {
    if (dsu.unite(A[i], B[i])) {
      adj[A[i]].push_back(B[i]);
      adj[B[i]].push_back(A[i]);
    }
  }

  auto dfs = [&](int u, int p, auto &&dfs) -> void {
    for (int v : adj[u]) {
      if (v != p) {
        depth[v] = depth[u] + 1;
        dfs(v, u, dfs);
      }
    }
  };
  dfs(0, -1, dfs);
  int U = max_element(all(depth)) - depth.begin();
  depth.assign(N, 0);
  dfs(U, -1, dfs);
  int V = max_element(all(depth)) - depth.begin();
  int d = depth[V];
  if (d >= 60) {
    for (int i = 0; i < N; i++) {
      MessageBoard(i, (X >> (depth[i] % 60)) & 1);
    }
  } else {
    int r = find(all(depth), d / 2) - depth.begin();
    v<int> len(N);
    auto dfs_len = [&](int u, int p, auto &&dfs_len) -> void {
      for (int v : adj[u]) {
        if (v != p) {
          dfs_len(v, u, dfs_len);
          len[u] = max(len[u], len[v] + 1);
        }
      }
    };
    dfs_len(U, -1, dfs_len);

    v<int> num(N, -1);
    int cnt = 0;
    auto dfs = [&](int u, int p, auto &&dfs) -> void {
      if (cnt < 60) {
        num[u] = cnt++;
      }
      v<int2> ch;
      for (int v : adj[u]) {
        if (v != p) {
          ch.push_back({len[v], v});
        }
      }

      sort(all(ch), greater<int2>());
      for (auto v : ch) {
        dfs(v.s, u, dfs);
      }
    };

    dfs(r, -1, dfs);

    for (int i = 0; i < N; i++) {
      if (num[i] != -1) {
        MessageBoard(i, (X >> num[i]) & 1);
      } else {
        MessageBoard(i, 0);
      }
    }
  }
}
#include "Ioi.h"

#include <bits/stdc++.h>
using namespace std;

template <class T> using v = vector<T>;
#define all(x) begin(x), end(x)
using int2 = pair<int, int>;
using ll = long long;
#define f first
#define s second

// communicate 60 bits of information in 120 moves
struct DSU {
  v<int> e;
  DSU(int n) { e = v<int>(n, -1); }

  int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
  bool same_set(int a, int b) { return get(a) == get(b); }
  int size(int x) { return -e[get(x)]; }

  bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x == y) {
      return false;
    }
    if (e[x] > e[y]) {
      swap(x, y);
    }
    e[x] += e[y];
    e[y] = x;
    return true;
  }
};

long long Ioi(int N, int M, int A[], int B[], int P, int VI, int T) {
  v<int> depth(N);
  v<v<int>> adj(N);
  DSU dsu(N);
  for (int i = 0; i < M; i++) {
    if (dsu.unite(A[i], B[i])) {
      adj[A[i]].push_back(B[i]);
      adj[B[i]].push_back(A[i]);
    }
  }

  v<int> up(N, -1);
  auto dfs = [&](int u, int p, auto &&dfs) -> void {
    up[u] = p;
    for (int v : adj[u]) {
      if (v != p) {
        depth[v] = depth[u] + 1;
        dfs(v, u, dfs);
      }
    }
  };
  dfs(0, -1, dfs);
  int U = max_element(all(depth)) - depth.begin();
  depth.assign(N, 0);
  dfs(U, -1, dfs);
  int V = max_element(all(depth)) - depth.begin();
  int d = depth[V];
  if (d >= 60) {
    if (depth[P] >= 60) {
      cerr << "case 1\n";
      ll ans = 0;
      for (int _ = 0; _ < 60; _++) {
        ans ^= (ll)VI << (depth[P] % 60);
        P = up[P];
        VI = Move(P);
      }
      return ans;
    } else {
      cerr << "case 2\n";
      while (P != U) {
        P = up[P];
        VI = Move(P);
      }
      v<int> down(N, -1);
      auto path = [&](int u, int p, auto &&path) -> void {
        for (int v : adj[u]) {
          if (v != p) {
            path(v, u, path);
            if (v == V) {
              down[u] = v;
            } else if (down[v] != -1) {
              down[u] = v;
            }
          }
        }
      };
      path(P, -1, path);
      ll ans = 0;
      for (int i = 0; i < 60; i++) {
        ans ^= (ll)VI << i;
        P = down[P];
        VI = Move(P);
      }
      return ans;
    }
  } else {
    cerr << "case 3\n";
    int r = find(all(depth), d / 2) - depth.begin();
    v<int> len(N);
    auto dfs_len = [&](int u, int p, auto &&dfs_len) -> void {
      for (int v : adj[u]) {
        if (v != p) {
          dfs_len(v, u, dfs_len);
          len[u] = max(len[u], len[v] + 1);
        }
      }
    };
    dfs_len(U, -1, dfs_len);

    v<int> num(N, -1);
    int cnt = 0;
    auto dfs = [&](int u, int p, auto &&dfs) -> void {
      up[u] = p;
      if (cnt < 60) {
        num[u] = cnt++;
      }
      v<int2> ch;
      for (int v : adj[u]) {
        if (v != p) {
          ch.push_back({len[v], v});
        }
      }

      sort(all(ch), greater<int2>());
      for (auto v : ch) {
        dfs(v.s, u, dfs);
      }
    };

    dfs(r, -1, dfs);
    while (P != r) {
      P = up[P];
      VI = Move(P);
    }
    cnt = 0;
    ll ans = 0;
    auto trav = [&](int u, int p, auto &&trav) -> void {
      cnt++;
      ans ^= (ll)VI << num[u];
      v<int2> ch;
      for (int v : adj[u]) {
        if (v != p) {
          ch.push_back({len[v], v});
        }
      }

      sort(all(ch));
      for (auto v : ch) {
        if (num[v.s] != -1) {
          VI = Move(v.s);
          trav(v.s, u, trav);
          if (cnt < 60) {
            VI = Move(u);
          }
        }
      }
    };
    trav(r, -1, trav);

    return ans;
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 800 KB Output is correct
2 Correct 0 ms 800 KB Output is correct
3 Correct 1 ms 788 KB Output is correct
4 Correct 0 ms 800 KB Output is correct
5 Correct 0 ms 788 KB Output is correct
6 Correct 0 ms 788 KB Output is correct
7 Correct 1 ms 796 KB Output is correct
8 Correct 1 ms 796 KB Output is correct
9 Correct 0 ms 796 KB Output is correct
10 Correct 1 ms 800 KB Output is correct
11 Correct 3 ms 1128 KB Output is correct
12 Correct 0 ms 788 KB Output is correct
13 Correct 1 ms 792 KB Output is correct
14 Correct 2 ms 796 KB Output is correct
15 Correct 0 ms 892 KB Output is correct
16 Correct 1 ms 796 KB Output is correct
17 Correct 1 ms 792 KB Output is correct
18 Correct 1 ms 796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 3476 KB Output is correct
2 Correct 16 ms 3656 KB Output is correct
3 Correct 18 ms 3648 KB Output is correct
4 Correct 12 ms 3416 KB Output is correct
5 Correct 12 ms 3644 KB Output is correct
6 Correct 11 ms 3164 KB Output is correct
7 Correct 11 ms 3172 KB Output is correct
8 Correct 12 ms 3164 KB Output is correct
9 Correct 11 ms 3136 KB Output is correct
10 Correct 11 ms 3660 KB Output is correct
11 Correct 12 ms 3664 KB Output is correct
12 Correct 10 ms 2868 KB Output is correct
13 Correct 11 ms 2644 KB Output is correct
14 Correct 11 ms 2884 KB Output is correct
15 Correct 12 ms 3316 KB Output is correct
16 Correct 11 ms 3160 KB Output is correct
17 Correct 12 ms 3164 KB Output is correct
18 Correct 12 ms 3140 KB Output is correct
19 Correct 12 ms 3172 KB Output is correct
20 Correct 8 ms 3160 KB Output is correct
21 Correct 9 ms 3160 KB Output is correct
22 Correct 11 ms 3680 KB Output is correct
23 Correct 11 ms 3160 KB Output is correct
24 Correct 11 ms 3168 KB Output is correct
25 Correct 11 ms 3168 KB Output is correct
26 Correct 13 ms 3412 KB Output is correct
27 Correct 12 ms 3168 KB Output is correct
28 Correct 12 ms 3196 KB Output is correct
29 Correct 9 ms 3124 KB Output is correct
30 Correct 10 ms 3160 KB Output is correct
31 Correct 0 ms 800 KB Output is correct
32 Correct 1 ms 796 KB Output is correct
33 Correct 1 ms 804 KB Output is correct
34 Correct 0 ms 788 KB Output is correct
35 Correct 0 ms 800 KB Output is correct
36 Correct 0 ms 788 KB Output is correct
37 Correct 0 ms 884 KB Output is correct
38 Correct 0 ms 788 KB Output is correct
39 Correct 0 ms 800 KB Output is correct
40 Correct 0 ms 788 KB Output is correct
41 Correct 0 ms 788 KB Output is correct
42 Correct 0 ms 796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 788 KB Output is correct
2 Correct 1 ms 788 KB Output is correct
3 Correct 0 ms 796 KB Output is correct
4 Correct 2 ms 1352 KB Output is correct
5 Correct 2 ms 1352 KB Output is correct
6 Correct 2 ms 1348 KB Output is correct
7 Correct 2 ms 1352 KB Output is correct
8 Correct 2 ms 1344 KB Output is correct
9 Correct 10 ms 3680 KB Output is correct
10 Correct 9 ms 3544 KB Output is correct
11 Correct 10 ms 3656 KB Output is correct
12 Correct 0 ms 960 KB Output is correct
13 Correct 1 ms 808 KB Output is correct
14 Correct 0 ms 800 KB Output is correct
15 Correct 0 ms 800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 3652 KB Output is correct
2 Correct 16 ms 3692 KB Output is correct
3 Correct 16 ms 3652 KB Output is correct
4 Correct 12 ms 3160 KB Output is correct
5 Correct 11 ms 3660 KB Output is correct
6 Correct 11 ms 3160 KB Output is correct
7 Correct 11 ms 3156 KB Output is correct
8 Correct 12 ms 3108 KB Output is correct
9 Correct 12 ms 3172 KB Output is correct
10 Correct 13 ms 3652 KB Output is correct
11 Correct 12 ms 3656 KB Output is correct
12 Correct 11 ms 2748 KB Output is correct
13 Partially correct 11 ms 2652 KB Partially correct
14 Partially correct 11 ms 2880 KB Partially correct
15 Correct 13 ms 3160 KB Output is correct
16 Correct 11 ms 3164 KB Output is correct
17 Correct 11 ms 3164 KB Output is correct
18 Correct 14 ms 3160 KB Output is correct
19 Correct 13 ms 3164 KB Output is correct
20 Correct 10 ms 3160 KB Output is correct
21 Correct 8 ms 3164 KB Output is correct
22 Correct 12 ms 3164 KB Output is correct
23 Correct 11 ms 3112 KB Output is correct
24 Correct 11 ms 3216 KB Output is correct
25 Correct 13 ms 3164 KB Output is correct
26 Correct 11 ms 3156 KB Output is correct
27 Correct 11 ms 3172 KB Output is correct
28 Correct 11 ms 3128 KB Output is correct
29 Correct 9 ms 3164 KB Output is correct
30 Correct 10 ms 3164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 3540 KB Output is correct
2 Correct 16 ms 3648 KB Output is correct
3 Correct 16 ms 3756 KB Output is correct
4 Correct 13 ms 3168 KB Output is correct
5 Correct 11 ms 3408 KB Output is correct
6 Correct 11 ms 3188 KB Output is correct
7 Correct 13 ms 3080 KB Output is correct
8 Correct 11 ms 3164 KB Output is correct
9 Correct 11 ms 3160 KB Output is correct
10 Correct 12 ms 3652 KB Output is correct
11 Correct 13 ms 3668 KB Output is correct
12 Correct 11 ms 2868 KB Output is correct
13 Correct 11 ms 2652 KB Output is correct
14 Correct 11 ms 2880 KB Output is correct
15 Correct 12 ms 3028 KB Output is correct
16 Correct 13 ms 3160 KB Output is correct
17 Correct 12 ms 3168 KB Output is correct
18 Correct 13 ms 3160 KB Output is correct
19 Correct 14 ms 3172 KB Output is correct
20 Correct 8 ms 3168 KB Output is correct
21 Correct 8 ms 3084 KB Output is correct
22 Correct 11 ms 3168 KB Output is correct
23 Correct 11 ms 3172 KB Output is correct
24 Correct 11 ms 3168 KB Output is correct
25 Correct 13 ms 3164 KB Output is correct
26 Correct 12 ms 3752 KB Output is correct
27 Correct 11 ms 3176 KB Output is correct
28 Correct 11 ms 3168 KB Output is correct
29 Correct 9 ms 3156 KB Output is correct
30 Correct 10 ms 3168 KB Output is correct
31 Correct 1 ms 788 KB Output is correct
32 Correct 0 ms 788 KB Output is correct
33 Correct 0 ms 804 KB Output is correct
34 Correct 1 ms 796 KB Output is correct
35 Correct 1 ms 800 KB Output is correct
36 Correct 0 ms 800 KB Output is correct
37 Correct 0 ms 788 KB Output is correct
38 Correct 0 ms 800 KB Output is correct
39 Correct 0 ms 800 KB Output is correct
40 Correct 0 ms 804 KB Output is correct
41 Correct 0 ms 800 KB Output is correct
42 Correct 0 ms 800 KB Output is correct