답안 #390484

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390484 2021-04-16T07:07:04 Z AlexPop28 슈퍼트리 잇기 (IOI20_supertrees) C++14
100 / 100
304 ms 22244 KB
#include "supertrees.h"
#include <vector>
#include <iostream>
#include <cassert>

using namespace std;

struct DSU {
  int n;
  vector<int> dad;
  vector<vector<int>> t;
  DSU(int n_) : n(n_), dad(n, -1), t(n) {
    for (int i = 0; i < n; ++i) {
      t[i] = {i};
    }
  }

  int Find(int x) {
    if (dad[x] == -1) return x;
    return dad[x] = Find(dad[x]);
  }

  bool Union(int x, int y) {
    x = Find(x), y = Find(y);
    if (x == y) return false;
    if (t[x].size() < t[y].size()) swap(x, y);
    t[x].insert(t[x].end(), t[y].begin(), t[y].end());
    dad[y] = x;
    return true;
  }

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

int construct(vector<vector<int>> p) {
  int n = p.size();
  vector<vector<int>> ans(n, vector<int>(n));

  auto AddEdge = [&](int a, int b) {
    if (a != b) ans[a][b] = ans[b][a] = 1;
  };

  DSU dsu(n);
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (p[i][j] == 1) {
        if (dsu.Union(i, j)) {
          AddEdge(i, j);
        }
      } else if (p[i][j] == 3) return 0;
    }
  }

  vector<int> roots;
  for (int i = 0; i < n; ++i) {
    if (dsu.Find(i) == i) roots.emplace_back(i);
  }

  for (int node : roots) {
    for (int i = 0; i < (int)dsu.t[node].size(); ++i) {
      int x = dsu.t[node][i];
      for (int j = i + 1; j < (int)dsu.t[node].size(); ++j) {
        int y = dsu.t[node][j];
        if (p[x][y] != 1) return 0;
      }
    }
  }

  DSU dsu_roots(n);
  for (int i = 0; i < (int)roots.size(); ++i) {
    int x = roots[i];
    for (int j = i + 1; j < (int)roots.size(); ++j) {
      int y = roots[j];
      if (p[x][y] == 2) {
        dsu_roots.Union(x, y);
      }
    }
  }
  
  for (int root : roots) {
    if (dsu_roots.Find(root) != root) continue;

    auto &v = dsu_roots.t[root];
    if ((int)v.size() == 1) continue;
    if ((int)v.size() == 2) return 0;

    for (int i = 0; i < (int)v.size(); ++i) {
      int j = i + 1;
      if (j == (int)v.size()) j = 0;
      AddEdge(v[i], v[j]);
    }
  }

  bool ret = true;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (dsu.Connected(i, j)) {
        ret &= p[i][j] == 1;
      } else {
        ret &= p[i][j] != 1;
      }
      if (dsu.Find(i) != dsu.Find(j) &&
          dsu_roots.Connected(dsu.Find(i), dsu.Find(j))) {
        ret &= p[i][j] == 2;
      } else {
        ret &= p[i][j] != 2;
      }
    }
  }
  
  if (!ret) return 0;

  build(ans);
  return 1;
}
# 결과 실행 시간 메모리 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 11 ms 1096 KB Output is correct
7 Correct 251 ms 22084 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 11 ms 1096 KB Output is correct
7 Correct 251 ms 22084 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 13 ms 1100 KB Output is correct
13 Correct 254 ms 22152 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 716 KB Output is correct
17 Correct 126 ms 12160 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 63 ms 5760 KB Output is correct
21 Correct 249 ms 22152 KB Output is correct
22 Correct 257 ms 22140 KB Output is correct
23 Correct 264 ms 22160 KB Output is correct
24 Correct 249 ms 22084 KB Output is correct
25 Correct 102 ms 12100 KB Output is correct
26 Correct 104 ms 12156 KB Output is correct
27 Correct 258 ms 22152 KB Output is correct
28 Correct 253 ms 22128 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 11 ms 1156 KB Output is correct
9 Correct 251 ms 22144 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 11 ms 1156 KB Output is correct
13 Correct 257 ms 22156 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 708 KB Output is correct
17 Correct 120 ms 12172 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 70 ms 5772 KB Output is correct
22 Correct 253 ms 22128 KB Output is correct
23 Correct 265 ms 22128 KB Output is correct
24 Correct 259 ms 22084 KB Output is correct
25 Correct 103 ms 12228 KB Output is correct
26 Correct 115 ms 12168 KB Output is correct
27 Correct 304 ms 22148 KB Output is correct
28 Correct 277 ms 22164 KB Output is correct
29 Correct 103 ms 12232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 69 ms 5748 KB Output is correct
5 Correct 251 ms 22036 KB Output is correct
6 Correct 250 ms 22100 KB Output is correct
7 Correct 259 ms 22220 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 65 ms 5764 KB Output is correct
10 Correct 264 ms 22100 KB Output is correct
11 Correct 255 ms 22160 KB Output is correct
12 Correct 270 ms 22244 KB Output is correct
13 Correct 1 ms 208 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 64 ms 5700 KB Output is correct
17 Correct 261 ms 22216 KB Output is correct
18 Correct 280 ms 22084 KB Output is correct
19 Correct 254 ms 22148 KB Output is correct
20 Correct 252 ms 22152 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 11 ms 1096 KB Output is correct
7 Correct 251 ms 22084 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 13 ms 1100 KB Output is correct
13 Correct 254 ms 22152 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 716 KB Output is correct
17 Correct 126 ms 12160 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 63 ms 5760 KB Output is correct
21 Correct 249 ms 22152 KB Output is correct
22 Correct 257 ms 22140 KB Output is correct
23 Correct 264 ms 22160 KB Output is correct
24 Correct 249 ms 22084 KB Output is correct
25 Correct 102 ms 12100 KB Output is correct
26 Correct 104 ms 12156 KB Output is correct
27 Correct 258 ms 22152 KB Output is correct
28 Correct 253 ms 22128 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 11 ms 1156 KB Output is correct
37 Correct 251 ms 22144 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 11 ms 1156 KB Output is correct
41 Correct 257 ms 22156 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 708 KB Output is correct
45 Correct 120 ms 12172 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 70 ms 5772 KB Output is correct
50 Correct 253 ms 22128 KB Output is correct
51 Correct 265 ms 22128 KB Output is correct
52 Correct 259 ms 22084 KB Output is correct
53 Correct 103 ms 12228 KB Output is correct
54 Correct 115 ms 12168 KB Output is correct
55 Correct 304 ms 22148 KB Output is correct
56 Correct 277 ms 22164 KB Output is correct
57 Correct 103 ms 12232 KB Output is correct
58 Correct 1 ms 204 KB Output is correct
59 Correct 0 ms 204 KB Output is correct
60 Correct 5 ms 716 KB Output is correct
61 Correct 110 ms 12156 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 204 KB Output is correct
64 Correct 1 ms 204 KB Output is correct
65 Correct 66 ms 5888 KB Output is correct
66 Correct 115 ms 12168 KB Output is correct
67 Correct 103 ms 12164 KB Output is correct
68 Correct 113 ms 12168 KB Output is correct
69 Correct 106 ms 12168 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 11 ms 1096 KB Output is correct
7 Correct 251 ms 22084 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 13 ms 1100 KB Output is correct
13 Correct 254 ms 22152 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 5 ms 716 KB Output is correct
17 Correct 126 ms 12160 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 63 ms 5760 KB Output is correct
21 Correct 249 ms 22152 KB Output is correct
22 Correct 257 ms 22140 KB Output is correct
23 Correct 264 ms 22160 KB Output is correct
24 Correct 249 ms 22084 KB Output is correct
25 Correct 102 ms 12100 KB Output is correct
26 Correct 104 ms 12156 KB Output is correct
27 Correct 258 ms 22152 KB Output is correct
28 Correct 253 ms 22128 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 11 ms 1156 KB Output is correct
37 Correct 251 ms 22144 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 1 ms 204 KB Output is correct
40 Correct 11 ms 1156 KB Output is correct
41 Correct 257 ms 22156 KB Output is correct
42 Correct 1 ms 204 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 5 ms 708 KB Output is correct
45 Correct 120 ms 12172 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 70 ms 5772 KB Output is correct
50 Correct 253 ms 22128 KB Output is correct
51 Correct 265 ms 22128 KB Output is correct
52 Correct 259 ms 22084 KB Output is correct
53 Correct 103 ms 12228 KB Output is correct
54 Correct 115 ms 12168 KB Output is correct
55 Correct 304 ms 22148 KB Output is correct
56 Correct 277 ms 22164 KB Output is correct
57 Correct 103 ms 12232 KB Output is correct
58 Correct 0 ms 204 KB Output is correct
59 Correct 1 ms 204 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 69 ms 5748 KB Output is correct
62 Correct 251 ms 22036 KB Output is correct
63 Correct 250 ms 22100 KB Output is correct
64 Correct 259 ms 22220 KB Output is correct
65 Correct 1 ms 204 KB Output is correct
66 Correct 65 ms 5764 KB Output is correct
67 Correct 264 ms 22100 KB Output is correct
68 Correct 255 ms 22160 KB Output is correct
69 Correct 270 ms 22244 KB Output is correct
70 Correct 1 ms 208 KB Output is correct
71 Correct 0 ms 204 KB Output is correct
72 Correct 1 ms 204 KB Output is correct
73 Correct 64 ms 5700 KB Output is correct
74 Correct 261 ms 22216 KB Output is correct
75 Correct 280 ms 22084 KB Output is correct
76 Correct 254 ms 22148 KB Output is correct
77 Correct 252 ms 22152 KB Output is correct
78 Correct 1 ms 204 KB Output is correct
79 Correct 0 ms 204 KB Output is correct
80 Correct 5 ms 716 KB Output is correct
81 Correct 110 ms 12156 KB Output is correct
82 Correct 1 ms 204 KB Output is correct
83 Correct 1 ms 204 KB Output is correct
84 Correct 1 ms 204 KB Output is correct
85 Correct 66 ms 5888 KB Output is correct
86 Correct 115 ms 12168 KB Output is correct
87 Correct 103 ms 12164 KB Output is correct
88 Correct 113 ms 12168 KB Output is correct
89 Correct 106 ms 12168 KB Output is correct
90 Correct 0 ms 204 KB Output is correct
91 Correct 1 ms 208 KB Output is correct
92 Correct 5 ms 716 KB Output is correct
93 Correct 108 ms 12160 KB Output is correct
94 Correct 1 ms 204 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 296 KB Output is correct
97 Correct 26 ms 3688 KB Output is correct
98 Correct 125 ms 14192 KB Output is correct
99 Correct 105 ms 14060 KB Output is correct
100 Correct 105 ms 14076 KB Output is correct
101 Correct 120 ms 14072 KB Output is correct
102 Correct 102 ms 14020 KB Output is correct
103 Correct 108 ms 14072 KB Output is correct
104 Correct 104 ms 14064 KB Output is correct
105 Correct 111 ms 14020 KB Output is correct