Submission #223701

# Submission time Handle Problem Language Result Execution time Memory
223701 2020-04-16T06:21:19 Z rama_pang Split the Attractions (IOI19_split) C++14
100 / 100
150 ms 18024 KB
#include "split.h"
#include <bits/stdc++.h>
using namespace std;

class DisjointSet {
 private:
  vector<int> p, sz;

 public:
  DisjointSet(int n) {
    p.assign(n, 0);
    iota(begin(p), end(p), 0);
    sz.assign(n, 1);
  }

  int Find(int x) {
    return p[x] == x ? x : p[x] = Find(p[x]);
  }

  bool Unite(int x, int y) {
    x = Find(x), y = Find(y);
    if (x == y) return false;
    if (sz[x] > sz[y]) swap(x, y);
    p[x] = y;
    sz[y] += sz[x];
    return true;
  }

  int Size(int x) {
    return sz[Find(x)];
  }

  bool SameSet(int x, int y) {
    return Find(x) == Find(y);
  }
};

int find_root(int mxsize, const vector<vector<int>> &adj) {
  vector<int> sz(adj.size());
  vector<int> par(adj.size(), -1);

  function<void(int)> dfs = [&](int u) {
    sz[u] = 1;
    for (auto v : adj[u]) if (v != par[u]) {
      par[v] = u;
      dfs(v);
      sz[u] += sz[v];
    }
  };

  dfs(0);

  int u = 0;
  while (true) {
    pair<int, int> mx = {-1, -1};
    for (auto v : adj[u]) if (v != par[u]) {
      mx = max(mx, {sz[v], v});
    }
    if (mx.first <= mxsize) return u;
    u = mx.second;
  }
}

vector<int> find_split(int n, int a, int b, int c, 
                       vector<int> p, vector<int> q) {
  DisjointSet d(n);
  vector<int> splits(n, 0);
  int m = p.size();
  int ca, cb, cc; // color of a, b, c

  { // recolor a, b, c such that a <= b <= c holds
    vector<pair<int, int>> split_sizes = {{a, 1}, {b, 2}, {c, 3}};
    sort(begin(split_sizes), end(split_sizes));
    tie(a, ca) = split_sizes[0];
    tie(b, cb) = split_sizes[1];
    tie(c, cc) = split_sizes[2];
  }

  vector<vector<int>> adj(n); // spanning tree adjacency list
  for (int i = 0; i < m; i++) {
    if (d.Unite(p[i], q[i])) {
      adj[p[i]].emplace_back(q[i]);
      adj[q[i]].emplace_back(p[i]);
    }
  }

  d = DisjointSet(n);
  int r = find_root(n - b, adj);

  // Unite all subtrees of children of root
  for (int i = 0; i < n; i++) {
    for (auto j : adj[i]) {
      if (i == r || j == r) continue;
      d.Unite(i, j);
    }
  }

  for (int i = 0; i < m; i++) {
    if (d.SameSet(p[i], q[i])) continue;
    if (p[i] == r || q[i] == r) continue;
    if (d.Size(p[i]) >= a || d.Size(q[i]) >= a) continue;
    d.Unite(p[i], q[i]);
    adj[p[i]].emplace_back(q[i]);
    adj[q[i]].emplace_back(p[i]);
  }
  
  function<void(int, int&, int, int)> color = [&](int u, int &n, int c, int bad) {
    if (u == bad || splits[u] != 0 || n == 0) return;
    splits[u] = c, n--;
    for (auto v : adj[u]) color(v, n, c, bad);
  };

  for (auto v : adj[r]) if (d.Size(v) >= a) {
    color(v, a, ca, r); // color merged subtree for set A
    color(r, b, cb, -1); // color root and its surrounding subgraph for set B
    replace(begin(splits), end(splits), 0, cc); // color vertices not in A or B for set C
    break;
  }
  
  return splits;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB ok, correct split
2 Correct 4 ms 384 KB ok, correct split
3 Correct 5 ms 256 KB ok, correct split
4 Correct 4 ms 256 KB ok, correct split
5 Correct 5 ms 256 KB ok, correct split
6 Correct 5 ms 256 KB ok, correct split
7 Correct 109 ms 17896 KB ok, correct split
8 Correct 97 ms 15720 KB ok, correct split
9 Correct 110 ms 14956 KB ok, correct split
10 Correct 100 ms 18024 KB ok, correct split
11 Correct 109 ms 16744 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 5 ms 256 KB ok, correct split
2 Correct 5 ms 256 KB ok, correct split
3 Correct 5 ms 256 KB ok, correct split
4 Correct 108 ms 11896 KB ok, correct split
5 Correct 92 ms 10616 KB ok, correct split
6 Correct 105 ms 18024 KB ok, correct split
7 Correct 146 ms 15592 KB ok, correct split
8 Correct 122 ms 13048 KB ok, correct split
9 Correct 94 ms 10600 KB ok, correct split
10 Correct 67 ms 10480 KB ok, correct split
11 Correct 72 ms 10480 KB ok, correct split
12 Correct 66 ms 10864 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB ok, correct split
2 Correct 97 ms 10616 KB ok, correct split
3 Correct 33 ms 4472 KB ok, correct split
4 Correct 5 ms 256 KB ok, correct split
5 Correct 103 ms 13160 KB ok, correct split
6 Correct 102 ms 12904 KB ok, correct split
7 Correct 110 ms 12648 KB ok, correct split
8 Correct 103 ms 14056 KB ok, correct split
9 Correct 104 ms 12632 KB ok, correct split
10 Correct 28 ms 3840 KB ok, no valid answer
11 Correct 40 ms 5504 KB ok, no valid answer
12 Correct 74 ms 10828 KB ok, no valid answer
13 Correct 85 ms 10616 KB ok, no valid answer
14 Correct 76 ms 10976 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB ok, correct split
2 Correct 5 ms 384 KB ok, no valid answer
3 Correct 4 ms 384 KB ok, correct split
4 Correct 4 ms 256 KB ok, correct split
5 Correct 5 ms 256 KB ok, correct split
6 Correct 4 ms 256 KB ok, correct split
7 Correct 4 ms 256 KB ok, correct split
8 Correct 4 ms 256 KB ok, correct split
9 Correct 7 ms 640 KB ok, correct split
10 Correct 7 ms 640 KB ok, correct split
11 Correct 5 ms 384 KB ok, correct split
12 Correct 7 ms 640 KB ok, correct split
13 Correct 5 ms 384 KB ok, correct split
14 Correct 4 ms 384 KB ok, correct split
15 Correct 5 ms 384 KB ok, correct split
16 Correct 5 ms 384 KB ok, correct split
17 Correct 5 ms 384 KB ok, correct split
18 Correct 4 ms 256 KB ok, correct split
19 Correct 5 ms 384 KB ok, correct split
20 Correct 5 ms 384 KB ok, correct split
21 Correct 7 ms 640 KB ok, correct split
22 Correct 7 ms 640 KB ok, correct split
23 Correct 6 ms 640 KB ok, correct split
24 Correct 6 ms 640 KB ok, correct split
25 Correct 6 ms 640 KB ok, correct split
26 Correct 7 ms 640 KB ok, correct split
27 Correct 7 ms 640 KB ok, correct split
28 Correct 7 ms 640 KB ok, correct split
29 Correct 5 ms 384 KB ok, correct split
30 Correct 7 ms 640 KB ok, correct split
31 Correct 5 ms 384 KB ok, correct split
32 Correct 5 ms 384 KB ok, correct split
33 Correct 5 ms 384 KB ok, correct split
34 Correct 6 ms 640 KB ok, correct split
35 Correct 6 ms 640 KB ok, correct split
36 Correct 7 ms 640 KB ok, correct split
37 Correct 7 ms 640 KB ok, correct split
38 Correct 8 ms 736 KB ok, correct split
39 Correct 7 ms 640 KB ok, correct split
40 Correct 9 ms 640 KB ok, correct split
41 Correct 6 ms 512 KB ok, correct split
42 Correct 6 ms 512 KB ok, correct split
43 Correct 7 ms 640 KB ok, correct split
44 Correct 7 ms 640 KB ok, correct split
45 Correct 7 ms 640 KB ok, correct split
46 Correct 6 ms 640 KB ok, correct split
47 Correct 6 ms 640 KB ok, no valid answer
48 Correct 6 ms 616 KB ok, correct split
49 Correct 6 ms 640 KB ok, correct split
50 Correct 5 ms 256 KB ok, no valid answer
51 Correct 5 ms 384 KB ok, no valid answer
52 Correct 6 ms 512 KB ok, no valid answer
53 Correct 7 ms 640 KB ok, no valid answer
54 Correct 6 ms 640 KB ok, no valid answer
55 Correct 7 ms 640 KB ok, no valid answer
56 Correct 6 ms 640 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB ok, correct split
2 Correct 4 ms 384 KB ok, correct split
3 Correct 5 ms 256 KB ok, correct split
4 Correct 4 ms 256 KB ok, correct split
5 Correct 5 ms 256 KB ok, correct split
6 Correct 5 ms 256 KB ok, correct split
7 Correct 109 ms 17896 KB ok, correct split
8 Correct 97 ms 15720 KB ok, correct split
9 Correct 110 ms 14956 KB ok, correct split
10 Correct 100 ms 18024 KB ok, correct split
11 Correct 109 ms 16744 KB ok, correct split
12 Correct 5 ms 256 KB ok, correct split
13 Correct 5 ms 256 KB ok, correct split
14 Correct 5 ms 256 KB ok, correct split
15 Correct 108 ms 11896 KB ok, correct split
16 Correct 92 ms 10616 KB ok, correct split
17 Correct 105 ms 18024 KB ok, correct split
18 Correct 146 ms 15592 KB ok, correct split
19 Correct 122 ms 13048 KB ok, correct split
20 Correct 94 ms 10600 KB ok, correct split
21 Correct 67 ms 10480 KB ok, correct split
22 Correct 72 ms 10480 KB ok, correct split
23 Correct 66 ms 10864 KB ok, correct split
24 Correct 4 ms 384 KB ok, correct split
25 Correct 97 ms 10616 KB ok, correct split
26 Correct 33 ms 4472 KB ok, correct split
27 Correct 5 ms 256 KB ok, correct split
28 Correct 103 ms 13160 KB ok, correct split
29 Correct 102 ms 12904 KB ok, correct split
30 Correct 110 ms 12648 KB ok, correct split
31 Correct 103 ms 14056 KB ok, correct split
32 Correct 104 ms 12632 KB ok, correct split
33 Correct 28 ms 3840 KB ok, no valid answer
34 Correct 40 ms 5504 KB ok, no valid answer
35 Correct 74 ms 10828 KB ok, no valid answer
36 Correct 85 ms 10616 KB ok, no valid answer
37 Correct 76 ms 10976 KB ok, no valid answer
38 Correct 5 ms 384 KB ok, correct split
39 Correct 5 ms 384 KB ok, no valid answer
40 Correct 4 ms 384 KB ok, correct split
41 Correct 4 ms 256 KB ok, correct split
42 Correct 5 ms 256 KB ok, correct split
43 Correct 4 ms 256 KB ok, correct split
44 Correct 4 ms 256 KB ok, correct split
45 Correct 4 ms 256 KB ok, correct split
46 Correct 7 ms 640 KB ok, correct split
47 Correct 7 ms 640 KB ok, correct split
48 Correct 5 ms 384 KB ok, correct split
49 Correct 7 ms 640 KB ok, correct split
50 Correct 5 ms 384 KB ok, correct split
51 Correct 4 ms 384 KB ok, correct split
52 Correct 5 ms 384 KB ok, correct split
53 Correct 5 ms 384 KB ok, correct split
54 Correct 5 ms 384 KB ok, correct split
55 Correct 4 ms 256 KB ok, correct split
56 Correct 5 ms 384 KB ok, correct split
57 Correct 5 ms 384 KB ok, correct split
58 Correct 7 ms 640 KB ok, correct split
59 Correct 7 ms 640 KB ok, correct split
60 Correct 6 ms 640 KB ok, correct split
61 Correct 6 ms 640 KB ok, correct split
62 Correct 6 ms 640 KB ok, correct split
63 Correct 7 ms 640 KB ok, correct split
64 Correct 7 ms 640 KB ok, correct split
65 Correct 7 ms 640 KB ok, correct split
66 Correct 5 ms 384 KB ok, correct split
67 Correct 7 ms 640 KB ok, correct split
68 Correct 5 ms 384 KB ok, correct split
69 Correct 5 ms 384 KB ok, correct split
70 Correct 5 ms 384 KB ok, correct split
71 Correct 6 ms 640 KB ok, correct split
72 Correct 6 ms 640 KB ok, correct split
73 Correct 7 ms 640 KB ok, correct split
74 Correct 7 ms 640 KB ok, correct split
75 Correct 8 ms 736 KB ok, correct split
76 Correct 7 ms 640 KB ok, correct split
77 Correct 9 ms 640 KB ok, correct split
78 Correct 6 ms 512 KB ok, correct split
79 Correct 6 ms 512 KB ok, correct split
80 Correct 7 ms 640 KB ok, correct split
81 Correct 7 ms 640 KB ok, correct split
82 Correct 7 ms 640 KB ok, correct split
83 Correct 6 ms 640 KB ok, correct split
84 Correct 6 ms 640 KB ok, no valid answer
85 Correct 6 ms 616 KB ok, correct split
86 Correct 6 ms 640 KB ok, correct split
87 Correct 5 ms 256 KB ok, no valid answer
88 Correct 5 ms 384 KB ok, no valid answer
89 Correct 6 ms 512 KB ok, no valid answer
90 Correct 7 ms 640 KB ok, no valid answer
91 Correct 6 ms 640 KB ok, no valid answer
92 Correct 7 ms 640 KB ok, no valid answer
93 Correct 6 ms 640 KB ok, no valid answer
94 Correct 102 ms 11128 KB ok, correct split
95 Correct 127 ms 13304 KB ok, correct split
96 Correct 117 ms 12520 KB ok, correct split
97 Correct 35 ms 4480 KB ok, correct split
98 Correct 35 ms 4608 KB ok, correct split
99 Correct 47 ms 5240 KB ok, correct split
100 Correct 121 ms 13316 KB ok, correct split
101 Correct 112 ms 12524 KB ok, correct split
102 Correct 150 ms 12644 KB ok, correct split
103 Correct 108 ms 12404 KB ok, correct split
104 Correct 106 ms 13816 KB ok, correct split
105 Correct 62 ms 6688 KB ok, correct split
106 Correct 115 ms 13552 KB ok, correct split
107 Correct 109 ms 10616 KB ok, correct split
108 Correct 118 ms 10616 KB ok, correct split
109 Correct 122 ms 13048 KB ok, correct split
110 Correct 126 ms 13428 KB ok, correct split
111 Correct 128 ms 13428 KB ok, correct split
112 Correct 133 ms 13432 KB ok, correct split
113 Correct 130 ms 13428 KB ok, correct split
114 Correct 16 ms 1664 KB ok, correct split
115 Correct 15 ms 1664 KB ok, correct split
116 Correct 129 ms 13428 KB ok, correct split
117 Correct 123 ms 13300 KB ok, correct split
118 Correct 95 ms 10600 KB ok, correct split
119 Correct 94 ms 10600 KB ok, correct split
120 Correct 105 ms 13544 KB ok, correct split
121 Correct 85 ms 10616 KB ok, no valid answer
122 Correct 89 ms 10616 KB ok, no valid answer
123 Correct 120 ms 13416 KB ok, no valid answer
124 Correct 119 ms 13304 KB ok, no valid answer
125 Correct 80 ms 11632 KB ok, no valid answer
126 Correct 56 ms 9840 KB ok, no valid answer
127 Correct 101 ms 12148 KB ok, no valid answer