답안 #496168

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
496168 2021-12-20T20:57:00 Z Alex_tz307 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
211 ms 28100 KB
#include <bits/stdc++.h>
#include "supertrees.h"

using namespace std;

struct DSU {
  vector<int> p, sz;

  DSU(int n) {
    p.resize(n);
    iota(p.begin(), p.end(), 0);
    sz.assign(n, 1);
  }

  int root(int x) {
    if (x == p[x]) {
      return x;
    }
    return p[x] = root(p[x]);
  }

  bool connected(int u, int v) {
    return root(u) == root(v);
  }

  bool unite(int u, int v) {
    int x = root(u), y = root(v);
    if (x == y) {
      return false;
    }
    if (sz[y] < sz[x]) {
      swap(x, y);
    }
    p[x] = y;
    sz[y] += sz[x];
    return true;
  }
};

const int kN = 1e3;
vector<vector<int>> g;
bitset<kN> vis;
vector<int> comp;

void dfs(int u) {
  vis[u] = true;
  comp.emplace_back(u);
  for (int v : g[u]) {
    if (!vis[v]) {
      dfs(v);
    }
  }
}

int construct(vector<vector<int>> p) {
	int n = p.size();
	DSU dsu(n);
	vector<vector<int>> sol(n, vector<int>(n));
	g.resize(n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (p[i][j] == 3) {
        return 0;
      }
      if (p[i][j] == 1 && dsu.unite(i, j)) {
        sol[i][j] = sol[j][i] = 1;
      }
      if (p[i][j] == 2) {
        g[i].emplace_back(j);
      }
    }
  }
  DSU cycles(n);
  for (int i = 0; i < n; ++i) {
    if (!vis[dsu.root(i)]) {
      comp.clear();
      dfs(i);
      for (int &v : comp) {
        v = dsu.root(v);
        vis[v] = true;
      }
      sort(comp.begin(), comp.end());
      comp.erase(unique(comp.begin(), comp.end()), comp.end());
      int m = comp.size();
      if (m == 2) {
        return 0;
      }
      if (m == 1) {
        continue;
      }
      for (int j = 0; j < m; ++j) {
        int k = comp[(j + 1) % m];
        sol[comp[j]][k] = sol[k][comp[j]] = 1;
        cycles.unite(comp[j], k);
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (p[i][j] == 0 && (dsu.connected(i, j) || cycles.connected(i, j))) {
        return 0;
      }
    }
  }
  build(sol);
	return 1;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 7 ms 1204 KB Output is correct
7 Correct 170 ms 24048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 7 ms 1204 KB Output is correct
7 Correct 170 ms 24048 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 7 ms 1192 KB Output is correct
13 Correct 211 ms 24036 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 0 ms 208 KB Output is correct
16 Correct 4 ms 848 KB Output is correct
17 Correct 83 ms 14076 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 0 ms 208 KB Output is correct
20 Correct 45 ms 6216 KB Output is correct
21 Correct 186 ms 24080 KB Output is correct
22 Correct 181 ms 23924 KB Output is correct
23 Correct 202 ms 24008 KB Output is correct
24 Correct 169 ms 24008 KB Output is correct
25 Correct 72 ms 14032 KB Output is correct
26 Correct 74 ms 14092 KB Output is correct
27 Correct 208 ms 23932 KB Output is correct
28 Correct 204 ms 23972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 292 KB Output is correct
8 Correct 8 ms 1172 KB Output is correct
9 Correct 171 ms 23956 KB Output is correct
10 Correct 0 ms 292 KB Output is correct
11 Correct 1 ms 280 KB Output is correct
12 Correct 8 ms 1456 KB Output is correct
13 Correct 174 ms 28100 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 0 ms 208 KB Output is correct
16 Correct 5 ms 900 KB Output is correct
17 Correct 84 ms 16496 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 1 ms 208 KB Output is correct
20 Correct 0 ms 296 KB Output is correct
21 Correct 44 ms 6256 KB Output is correct
22 Correct 169 ms 24560 KB Output is correct
23 Correct 195 ms 24044 KB Output is correct
24 Correct 181 ms 27240 KB Output is correct
25 Correct 73 ms 14064 KB Output is correct
26 Correct 70 ms 14604 KB Output is correct
27 Correct 194 ms 24052 KB Output is correct
28 Correct 177 ms 26056 KB Output is correct
29 Correct 67 ms 14132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 47 ms 6152 KB Output is correct
5 Correct 175 ms 23976 KB Output is correct
6 Correct 166 ms 23912 KB Output is correct
7 Correct 176 ms 23980 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 44 ms 6252 KB Output is correct
10 Correct 174 ms 24536 KB Output is correct
11 Correct 171 ms 24104 KB Output is correct
12 Correct 188 ms 27020 KB Output is correct
13 Correct 1 ms 208 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 0 ms 208 KB Output is correct
16 Correct 44 ms 6256 KB Output is correct
17 Correct 173 ms 24560 KB Output is correct
18 Correct 184 ms 24484 KB Output is correct
19 Correct 182 ms 24220 KB Output is correct
20 Correct 169 ms 24048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 7 ms 1204 KB Output is correct
7 Correct 170 ms 24048 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 7 ms 1192 KB Output is correct
13 Correct 211 ms 24036 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 0 ms 208 KB Output is correct
16 Correct 4 ms 848 KB Output is correct
17 Correct 83 ms 14076 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 0 ms 208 KB Output is correct
20 Correct 45 ms 6216 KB Output is correct
21 Correct 186 ms 24080 KB Output is correct
22 Correct 181 ms 23924 KB Output is correct
23 Correct 202 ms 24008 KB Output is correct
24 Correct 169 ms 24008 KB Output is correct
25 Correct 72 ms 14032 KB Output is correct
26 Correct 74 ms 14092 KB Output is correct
27 Correct 208 ms 23932 KB Output is correct
28 Correct 204 ms 23972 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 208 KB Output is correct
31 Correct 1 ms 208 KB Output is correct
32 Correct 0 ms 208 KB Output is correct
33 Correct 1 ms 292 KB Output is correct
34 Correct 1 ms 208 KB Output is correct
35 Correct 1 ms 292 KB Output is correct
36 Correct 8 ms 1172 KB Output is correct
37 Correct 171 ms 23956 KB Output is correct
38 Correct 0 ms 292 KB Output is correct
39 Correct 1 ms 280 KB Output is correct
40 Correct 8 ms 1456 KB Output is correct
41 Correct 174 ms 28100 KB Output is correct
42 Correct 1 ms 208 KB Output is correct
43 Correct 0 ms 208 KB Output is correct
44 Correct 5 ms 900 KB Output is correct
45 Correct 84 ms 16496 KB Output is correct
46 Correct 0 ms 208 KB Output is correct
47 Correct 1 ms 208 KB Output is correct
48 Correct 0 ms 296 KB Output is correct
49 Correct 44 ms 6256 KB Output is correct
50 Correct 169 ms 24560 KB Output is correct
51 Correct 195 ms 24044 KB Output is correct
52 Correct 181 ms 27240 KB Output is correct
53 Correct 73 ms 14064 KB Output is correct
54 Correct 70 ms 14604 KB Output is correct
55 Correct 194 ms 24052 KB Output is correct
56 Correct 177 ms 26056 KB Output is correct
57 Correct 67 ms 14132 KB Output is correct
58 Correct 0 ms 208 KB Output is correct
59 Correct 1 ms 224 KB Output is correct
60 Correct 4 ms 952 KB Output is correct
61 Correct 84 ms 16112 KB Output is correct
62 Correct 1 ms 208 KB Output is correct
63 Correct 1 ms 208 KB Output is correct
64 Correct 1 ms 208 KB Output is correct
65 Correct 43 ms 6200 KB Output is correct
66 Correct 80 ms 14848 KB Output is correct
67 Correct 70 ms 14572 KB Output is correct
68 Correct 74 ms 14156 KB Output is correct
69 Correct 68 ms 14060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 292 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 7 ms 1204 KB Output is correct
7 Correct 170 ms 24048 KB Output is correct
8 Correct 1 ms 292 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 7 ms 1192 KB Output is correct
13 Correct 211 ms 24036 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 0 ms 208 KB Output is correct
16 Correct 4 ms 848 KB Output is correct
17 Correct 83 ms 14076 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 0 ms 208 KB Output is correct
20 Correct 45 ms 6216 KB Output is correct
21 Correct 186 ms 24080 KB Output is correct
22 Correct 181 ms 23924 KB Output is correct
23 Correct 202 ms 24008 KB Output is correct
24 Correct 169 ms 24008 KB Output is correct
25 Correct 72 ms 14032 KB Output is correct
26 Correct 74 ms 14092 KB Output is correct
27 Correct 208 ms 23932 KB Output is correct
28 Correct 204 ms 23972 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 0 ms 208 KB Output is correct
31 Correct 1 ms 208 KB Output is correct
32 Correct 0 ms 208 KB Output is correct
33 Correct 1 ms 292 KB Output is correct
34 Correct 1 ms 208 KB Output is correct
35 Correct 1 ms 292 KB Output is correct
36 Correct 8 ms 1172 KB Output is correct
37 Correct 171 ms 23956 KB Output is correct
38 Correct 0 ms 292 KB Output is correct
39 Correct 1 ms 280 KB Output is correct
40 Correct 8 ms 1456 KB Output is correct
41 Correct 174 ms 28100 KB Output is correct
42 Correct 1 ms 208 KB Output is correct
43 Correct 0 ms 208 KB Output is correct
44 Correct 5 ms 900 KB Output is correct
45 Correct 84 ms 16496 KB Output is correct
46 Correct 0 ms 208 KB Output is correct
47 Correct 1 ms 208 KB Output is correct
48 Correct 0 ms 296 KB Output is correct
49 Correct 44 ms 6256 KB Output is correct
50 Correct 169 ms 24560 KB Output is correct
51 Correct 195 ms 24044 KB Output is correct
52 Correct 181 ms 27240 KB Output is correct
53 Correct 73 ms 14064 KB Output is correct
54 Correct 70 ms 14604 KB Output is correct
55 Correct 194 ms 24052 KB Output is correct
56 Correct 177 ms 26056 KB Output is correct
57 Correct 67 ms 14132 KB Output is correct
58 Correct 0 ms 204 KB Output is correct
59 Correct 0 ms 208 KB Output is correct
60 Correct 1 ms 208 KB Output is correct
61 Correct 47 ms 6152 KB Output is correct
62 Correct 175 ms 23976 KB Output is correct
63 Correct 166 ms 23912 KB Output is correct
64 Correct 176 ms 23980 KB Output is correct
65 Correct 0 ms 208 KB Output is correct
66 Correct 44 ms 6252 KB Output is correct
67 Correct 174 ms 24536 KB Output is correct
68 Correct 171 ms 24104 KB Output is correct
69 Correct 188 ms 27020 KB Output is correct
70 Correct 1 ms 208 KB Output is correct
71 Correct 0 ms 208 KB Output is correct
72 Correct 0 ms 208 KB Output is correct
73 Correct 44 ms 6256 KB Output is correct
74 Correct 173 ms 24560 KB Output is correct
75 Correct 184 ms 24484 KB Output is correct
76 Correct 182 ms 24220 KB Output is correct
77 Correct 169 ms 24048 KB Output is correct
78 Correct 0 ms 208 KB Output is correct
79 Correct 1 ms 224 KB Output is correct
80 Correct 4 ms 952 KB Output is correct
81 Correct 84 ms 16112 KB Output is correct
82 Correct 1 ms 208 KB Output is correct
83 Correct 1 ms 208 KB Output is correct
84 Correct 1 ms 208 KB Output is correct
85 Correct 43 ms 6200 KB Output is correct
86 Correct 80 ms 14848 KB Output is correct
87 Correct 70 ms 14572 KB Output is correct
88 Correct 74 ms 14156 KB Output is correct
89 Correct 68 ms 14060 KB Output is correct
90 Correct 0 ms 208 KB Output is correct
91 Correct 0 ms 208 KB Output is correct
92 Correct 3 ms 812 KB Output is correct
93 Correct 82 ms 14128 KB Output is correct
94 Correct 0 ms 208 KB Output is correct
95 Correct 0 ms 208 KB Output is correct
96 Correct 1 ms 208 KB Output is correct
97 Correct 17 ms 3656 KB Output is correct
98 Correct 67 ms 14076 KB Output is correct
99 Correct 65 ms 14072 KB Output is correct
100 Correct 66 ms 14104 KB Output is correct
101 Correct 64 ms 14024 KB Output is correct
102 Correct 66 ms 14120 KB Output is correct
103 Correct 66 ms 14124 KB Output is correct
104 Correct 64 ms 14064 KB Output is correct
105 Correct 72 ms 14024 KB Output is correct