답안 #981004

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
981004 2024-05-12T17:23:10 Z kilkuwu 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
171 ms 24500 KB
#include "supertrees.h"
#include <bits/stdc++.h>

struct DSU {
  std::vector<int> e;

  DSU(int n) : e(n, -1) {}

  int find(int u) {
    return e[u] < 0 ? u : e[u] = find(e[u]);
  }
  
  bool merge(int u, int v) {
    u = find(u), v = find(v);
    if (u == v) return false;
    if (e[u] > e[v]) std::swap(u, v);
    e[u] += e[v];
    e[v] = u;
    return true;
  } 

  bool same(int u, int v) {
    return find(u) == find(v);
  }
  
  int size(int u) {
    return -e[find(u)];
  }
};

int construct(std::vector<std::vector<int>> p) {
  int n = p.size();

  DSU dsu(n);

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (p[i][j] == 3) return false;
      if (p[i][j] != 0) {
        dsu.merge(i, j);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (!p[i][j] && dsu.same(i, j)) return false;
    }
  }

  // now we have all the tplt we need 

  // now what can we do ? 
  // each tplt has only one fucking cycle

  std::vector<std::vector<int>> comps(n);
  for (int i = 0; i < n; i++) {
    comps[dsu.find(i)].push_back(i);
  }

  // they must have connected to each other 

  std::vector<std::vector<int>> b(n, std::vector<int>(n));

  DSU lmao(n);

  std::vector<std::pair<int, int>> edges;

  auto add_edge = [&](int u, int v) -> void {
    b[u][v] = b[v][u] = 1;
  };

  for (int c = 0; c < n; c++) {
    if (comps[c].empty()) continue;

    std::map<std::vector<int>, std::vector<int>> mp;

    for (int u : comps[c]) {
      std::vector<int> key;
      for (int j = 0; j < n; j++) {
        if (p[u][j] == 2) {
          key.push_back(j);
        }
      }
      mp[key].push_back(u);
    }

    std::vector<int> rep;

    for (auto& [key, val] : mp) {
      int total = key.size() + val.size(); 
      if (total != (int) comps[c].size()) {
        return false; 
      }
      for (int i = 0; i + 1 < (int) val.size(); i++) {
        add_edge(val[i], val[i + 1]);
      }
      rep.push_back(val[0]);
    }

    if (rep.size() == 2) {
      // we cannot do a cycle with two 
      return false;
    }
    if (rep.size() > 2) {
      for (int i = 0; i < (int)rep.size(); i++) {
        add_edge(rep[i], rep[(i + 1) % rep.size()]);
      }
    }
  }

  build(b);
	return 1;
}


#ifdef LOCAL
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>

static int n;
static std::vector<std::vector<int>> p;
static std::vector<std::vector<int>> b;
static bool called = false;

static void check(bool cond, std::string message) {
    if (!cond) {
        printf("%s\n", message.c_str());
        fclose(stdout);
        exit(0);
    }
}

void build(std::vector<std::vector<int>> _b) {
    check(!called, "build is called more than once");
    called = true;
    check((int)_b.size() == n, "Invalid number of rows in b");
    for (int i = 0; i < n; i++) {
        check((int)_b[i].size() == n, "Invalid number of columns in b");
    }
    b = _b;
}

int main() {
    assert(scanf("%d", &n) == 1);
    
    p.resize(n);
    for (int i = 0; i < n; i++) {
        p[i].resize(n);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            assert(scanf("%d", &p[i][j]) == 1);
        }
    }
    fclose(stdin);

    int possible = construct(p);

    check(possible == 0 || possible == 1, "Invalid return value of construct");
    if (possible == 1) {
        check(called, "construct returned 1 without calling build");
    } else {
        check(!called, "construct called build but returned 0");
    }

    printf("%d\n", possible);
    if (possible == 1) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (j) {
                    printf(" ");
                }
                printf("%d", b[i][j]);
            }
            printf("\n");
        }
    }
    fclose(stdout);
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 6 ms 1372 KB Output is correct
7 Correct 149 ms 24148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 6 ms 1372 KB Output is correct
7 Correct 149 ms 24148 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 159 ms 24140 KB Output is correct
14 Correct 0 ms 436 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 672 KB Output is correct
17 Correct 65 ms 10156 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 43 ms 6484 KB Output is correct
21 Correct 171 ms 23932 KB Output is correct
22 Correct 169 ms 23940 KB Output is correct
23 Correct 157 ms 23936 KB Output is correct
24 Correct 150 ms 24188 KB Output is correct
25 Correct 63 ms 10408 KB Output is correct
26 Correct 61 ms 10124 KB Output is correct
27 Correct 155 ms 24148 KB Output is correct
28 Correct 161 ms 24440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 428 KB Output is correct
8 Correct 7 ms 1184 KB Output is correct
9 Correct 158 ms 24236 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 154 ms 24200 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 604 KB Output is correct
17 Correct 66 ms 10068 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 39 ms 6388 KB Output is correct
22 Correct 154 ms 24060 KB Output is correct
23 Correct 153 ms 24148 KB Output is correct
24 Correct 156 ms 24084 KB Output is correct
25 Correct 65 ms 14160 KB Output is correct
26 Correct 62 ms 10500 KB Output is correct
27 Correct 150 ms 23928 KB Output is correct
28 Correct 160 ms 24148 KB Output is correct
29 Correct 63 ms 14164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 38 ms 6404 KB Output is correct
5 Correct 157 ms 23948 KB Output is correct
6 Correct 152 ms 24060 KB Output is correct
7 Correct 154 ms 24148 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 39 ms 6396 KB Output is correct
10 Correct 157 ms 24196 KB Output is correct
11 Correct 157 ms 24500 KB Output is correct
12 Correct 156 ms 24020 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 43 ms 6408 KB Output is correct
17 Correct 153 ms 24028 KB Output is correct
18 Correct 152 ms 24200 KB Output is correct
19 Correct 153 ms 24148 KB Output is correct
20 Correct 149 ms 24148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 6 ms 1372 KB Output is correct
7 Correct 149 ms 24148 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 159 ms 24140 KB Output is correct
14 Correct 0 ms 436 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 672 KB Output is correct
17 Correct 65 ms 10156 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 43 ms 6484 KB Output is correct
21 Correct 171 ms 23932 KB Output is correct
22 Correct 169 ms 23940 KB Output is correct
23 Correct 157 ms 23936 KB Output is correct
24 Correct 150 ms 24188 KB Output is correct
25 Correct 63 ms 10408 KB Output is correct
26 Correct 61 ms 10124 KB Output is correct
27 Correct 155 ms 24148 KB Output is correct
28 Correct 161 ms 24440 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 428 KB Output is correct
36 Correct 7 ms 1184 KB Output is correct
37 Correct 158 ms 24236 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 7 ms 1372 KB Output is correct
41 Correct 154 ms 24200 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 66 ms 10068 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 39 ms 6388 KB Output is correct
50 Correct 154 ms 24060 KB Output is correct
51 Correct 153 ms 24148 KB Output is correct
52 Correct 156 ms 24084 KB Output is correct
53 Correct 65 ms 14160 KB Output is correct
54 Correct 62 ms 10500 KB Output is correct
55 Correct 150 ms 23928 KB Output is correct
56 Correct 160 ms 24148 KB Output is correct
57 Correct 63 ms 14164 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 1 ms 588 KB Output is correct
60 Correct 3 ms 604 KB Output is correct
61 Correct 64 ms 10112 KB Output is correct
62 Correct 0 ms 348 KB Output is correct
63 Correct 0 ms 348 KB Output is correct
64 Correct 1 ms 360 KB Output is correct
65 Correct 39 ms 6360 KB Output is correct
66 Correct 63 ms 10120 KB Output is correct
67 Correct 66 ms 10064 KB Output is correct
68 Correct 61 ms 10112 KB Output is correct
69 Correct 63 ms 14164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 6 ms 1372 KB Output is correct
7 Correct 149 ms 24148 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 159 ms 24140 KB Output is correct
14 Correct 0 ms 436 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 4 ms 672 KB Output is correct
17 Correct 65 ms 10156 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 43 ms 6484 KB Output is correct
21 Correct 171 ms 23932 KB Output is correct
22 Correct 169 ms 23940 KB Output is correct
23 Correct 157 ms 23936 KB Output is correct
24 Correct 150 ms 24188 KB Output is correct
25 Correct 63 ms 10408 KB Output is correct
26 Correct 61 ms 10124 KB Output is correct
27 Correct 155 ms 24148 KB Output is correct
28 Correct 161 ms 24440 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 428 KB Output is correct
36 Correct 7 ms 1184 KB Output is correct
37 Correct 158 ms 24236 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 7 ms 1372 KB Output is correct
41 Correct 154 ms 24200 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 604 KB Output is correct
45 Correct 66 ms 10068 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 39 ms 6388 KB Output is correct
50 Correct 154 ms 24060 KB Output is correct
51 Correct 153 ms 24148 KB Output is correct
52 Correct 156 ms 24084 KB Output is correct
53 Correct 65 ms 14160 KB Output is correct
54 Correct 62 ms 10500 KB Output is correct
55 Correct 150 ms 23928 KB Output is correct
56 Correct 160 ms 24148 KB Output is correct
57 Correct 63 ms 14164 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 38 ms 6404 KB Output is correct
62 Correct 157 ms 23948 KB Output is correct
63 Correct 152 ms 24060 KB Output is correct
64 Correct 154 ms 24148 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 39 ms 6396 KB Output is correct
67 Correct 157 ms 24196 KB Output is correct
68 Correct 157 ms 24500 KB Output is correct
69 Correct 156 ms 24020 KB Output is correct
70 Correct 1 ms 344 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 43 ms 6408 KB Output is correct
74 Correct 153 ms 24028 KB Output is correct
75 Correct 152 ms 24200 KB Output is correct
76 Correct 153 ms 24148 KB Output is correct
77 Correct 149 ms 24148 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 1 ms 588 KB Output is correct
80 Correct 3 ms 604 KB Output is correct
81 Correct 64 ms 10112 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 1 ms 360 KB Output is correct
85 Correct 39 ms 6360 KB Output is correct
86 Correct 63 ms 10120 KB Output is correct
87 Correct 66 ms 10064 KB Output is correct
88 Correct 61 ms 10112 KB Output is correct
89 Correct 63 ms 14164 KB Output is correct
90 Correct 1 ms 344 KB Output is correct
91 Correct 1 ms 348 KB Output is correct
92 Correct 3 ms 604 KB Output is correct
93 Correct 71 ms 10112 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 0 ms 344 KB Output is correct
97 Correct 16 ms 2908 KB Output is correct
98 Correct 60 ms 10120 KB Output is correct
99 Correct 61 ms 10112 KB Output is correct
100 Correct 65 ms 10320 KB Output is correct
101 Correct 60 ms 10064 KB Output is correct
102 Correct 57 ms 10068 KB Output is correct
103 Correct 73 ms 10052 KB Output is correct
104 Correct 62 ms 10320 KB Output is correct
105 Correct 65 ms 10068 KB Output is correct