Submission #835102

# Submission time Handle Problem Language Result Execution time Memory
835102 2023-08-23T08:20:28 Z NeroZein Connecting Supertrees (IOI20_supertrees) C++17
100 / 100
196 ms 24116 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std; 

int construct(vector<vector<int>> p) {
  int n = p.size();
  int ncc = n; 
  vector<int> pr(n);
  vector<int> sz(n, 1); 
  vector<vector<int>> comp(n);
  iota(pr.begin(), pr.end(), 0);
  for (int i = 0; i < n; ++i) {
    comp[i].push_back(i); 
  }
  function<int(int)> get = [&](int v) {
    return pr[v] = (pr[v] == v ? v : get(pr[v])); 
  };
  function<void(int, int)> unite = [&](int u, int v) {
    u = get(u), v = get(v);
    if (u == v) return;
    if (sz[u] > sz[v]) swap(u, v);
    ncc--;
    pr[u] = v;
    sz[v] += sz[u]; 
    for (int x : comp[u]) {
      comp[v].push_back(x); 
    }
    assert((int) comp[v].size() == sz[v]); 
  };
  for (int i = 0; i < n; ++i) {
    if (p[i][i] != 1) {
      return 0; 
    }
    for (int j = 0; j < n; ++j) {
      if (p[i][j] != p[j][i]) {
        return 0; 
      }
      if (p[i][j] != 0) {
        unite(i, j); 
      }
    }
  }
  int t = 0; 
  vector<int> chosen(n, -1); 
  vector<vector<int>> b(n, vector<int>(n)); 
  auto check = [&](vector<int>& v) {
    for (int i = 0; i < (int) v.size(); ++i) {
      for (int j = 0; j < (int) v.size(); ++j) {
        if (p[v[i]][v[j]] != 1) {
          return false;
        }
      }
      for (int j = 0; j < n; ++j) {
        if (get(j) == get(v[i]) && chosen[j] != chosen[v[i]] && p[j][v[i]] != 2) {
          return false; 
        }
      }
    }
    return true; 
  }; 
  for (int i = 0; i < n; ++i) {
    if (get(i) != i) continue; 
    t++; 
    vector<int> heads;
    vector<int> vec = comp[i];
    for (int j = 0; j < (int) vec.size(); ++j) {
      if (chosen[vec[j]] != -1) continue; 
      int head = vec[j];
      heads.push_back(head); 
      int la = head;
      vector<int> tmp = {head}; 
      chosen[head] = head;
      for (int k = j + 1; k < (int) vec.size(); ++k) {
        if (chosen[vec[k]] == -1 && p[head][vec[k]] == 1) {
          tmp.push_back(vec[k]); 
          chosen[vec[k]] = head;
          b[la][vec[k]] = b[vec[k]][la] = 1; 
          la = vec[k];
        }
      }
      if (!check(tmp)) return 0; 
    }
    if (heads.size() > 1) {
      if (heads.size() == 2) return 0; 
      for (int j = 0; j < (int) heads.size(); ++j) {
        b[heads[j]][heads[(j + 1) % heads.size()]] = 1;
        b[heads[(j + 1) % heads.size()]][heads[j]] = 1;
      }      
    }
    //#warning didn't make sure that an answer exists
  }
  assert(t == ncc); 
  build(b);
  return 1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 188 ms 22124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 188 ms 22124 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 157 ms 22036 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 78 ms 14068 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 42 ms 6224 KB Output is correct
21 Correct 170 ms 24052 KB Output is correct
22 Correct 164 ms 24052 KB Output is correct
23 Correct 189 ms 24012 KB Output is correct
24 Correct 156 ms 24012 KB Output is correct
25 Correct 73 ms 14076 KB Output is correct
26 Correct 65 ms 14064 KB Output is correct
27 Correct 196 ms 24116 KB Output is correct
28 Correct 160 ms 24072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 7 ms 1208 KB Output is correct
9 Correct 167 ms 24024 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 8 ms 1236 KB Output is correct
13 Correct 185 ms 24064 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 78 ms 14068 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 42 ms 6220 KB Output is correct
22 Correct 170 ms 24032 KB Output is correct
23 Correct 168 ms 23912 KB Output is correct
24 Correct 186 ms 23952 KB Output is correct
25 Correct 70 ms 14168 KB Output is correct
26 Correct 90 ms 14072 KB Output is correct
27 Correct 166 ms 23964 KB Output is correct
28 Correct 185 ms 24060 KB Output is correct
29 Correct 72 ms 14156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 44 ms 5708 KB Output is correct
5 Correct 173 ms 22032 KB Output is correct
6 Correct 169 ms 22092 KB Output is correct
7 Correct 185 ms 22020 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 45 ms 5716 KB Output is correct
10 Correct 172 ms 22020 KB Output is correct
11 Correct 171 ms 22124 KB Output is correct
12 Correct 186 ms 22092 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 42 ms 5716 KB Output is correct
17 Correct 180 ms 22092 KB Output is correct
18 Correct 169 ms 22092 KB Output is correct
19 Correct 168 ms 22164 KB Output is correct
20 Correct 166 ms 22216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 188 ms 22124 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 157 ms 22036 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 78 ms 14068 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 42 ms 6224 KB Output is correct
21 Correct 170 ms 24052 KB Output is correct
22 Correct 164 ms 24052 KB Output is correct
23 Correct 189 ms 24012 KB Output is correct
24 Correct 156 ms 24012 KB Output is correct
25 Correct 73 ms 14076 KB Output is correct
26 Correct 65 ms 14064 KB Output is correct
27 Correct 196 ms 24116 KB Output is correct
28 Correct 160 ms 24072 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1208 KB Output is correct
37 Correct 167 ms 24024 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 8 ms 1236 KB Output is correct
41 Correct 185 ms 24064 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 852 KB Output is correct
45 Correct 78 ms 14068 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 42 ms 6220 KB Output is correct
50 Correct 170 ms 24032 KB Output is correct
51 Correct 168 ms 23912 KB Output is correct
52 Correct 186 ms 23952 KB Output is correct
53 Correct 70 ms 14168 KB Output is correct
54 Correct 90 ms 14072 KB Output is correct
55 Correct 166 ms 23964 KB Output is correct
56 Correct 185 ms 24060 KB Output is correct
57 Correct 72 ms 14156 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 KB Output is correct
60 Correct 4 ms 852 KB Output is correct
61 Correct 79 ms 14076 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 42 ms 6264 KB Output is correct
66 Correct 65 ms 14156 KB Output is correct
67 Correct 74 ms 14156 KB Output is correct
68 Correct 63 ms 14156 KB Output is correct
69 Correct 64 ms 14156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 8 ms 1108 KB Output is correct
7 Correct 188 ms 22124 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 7 ms 1108 KB Output is correct
13 Correct 157 ms 22036 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 4 ms 852 KB Output is correct
17 Correct 78 ms 14068 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 42 ms 6224 KB Output is correct
21 Correct 170 ms 24052 KB Output is correct
22 Correct 164 ms 24052 KB Output is correct
23 Correct 189 ms 24012 KB Output is correct
24 Correct 156 ms 24012 KB Output is correct
25 Correct 73 ms 14076 KB Output is correct
26 Correct 65 ms 14064 KB Output is correct
27 Correct 196 ms 24116 KB Output is correct
28 Correct 160 ms 24072 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 0 ms 212 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 7 ms 1208 KB Output is correct
37 Correct 167 ms 24024 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 8 ms 1236 KB Output is correct
41 Correct 185 ms 24064 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 4 ms 852 KB Output is correct
45 Correct 78 ms 14068 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 42 ms 6220 KB Output is correct
50 Correct 170 ms 24032 KB Output is correct
51 Correct 168 ms 23912 KB Output is correct
52 Correct 186 ms 23952 KB Output is correct
53 Correct 70 ms 14168 KB Output is correct
54 Correct 90 ms 14072 KB Output is correct
55 Correct 166 ms 23964 KB Output is correct
56 Correct 185 ms 24060 KB Output is correct
57 Correct 72 ms 14156 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 44 ms 5708 KB Output is correct
62 Correct 173 ms 22032 KB Output is correct
63 Correct 169 ms 22092 KB Output is correct
64 Correct 185 ms 22020 KB Output is correct
65 Correct 1 ms 212 KB Output is correct
66 Correct 45 ms 5716 KB Output is correct
67 Correct 172 ms 22020 KB Output is correct
68 Correct 171 ms 22124 KB Output is correct
69 Correct 186 ms 22092 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 42 ms 5716 KB Output is correct
74 Correct 180 ms 22092 KB Output is correct
75 Correct 169 ms 22092 KB Output is correct
76 Correct 168 ms 22164 KB Output is correct
77 Correct 166 ms 22216 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 4 ms 852 KB Output is correct
81 Correct 79 ms 14076 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 42 ms 6264 KB Output is correct
86 Correct 65 ms 14156 KB Output is correct
87 Correct 74 ms 14156 KB Output is correct
88 Correct 63 ms 14156 KB Output is correct
89 Correct 64 ms 14156 KB Output is correct
90 Correct 1 ms 212 KB Output is correct
91 Correct 1 ms 340 KB Output is correct
92 Correct 4 ms 852 KB Output is correct
93 Correct 83 ms 14128 KB Output is correct
94 Correct 1 ms 212 KB Output is correct
95 Correct 1 ms 212 KB Output is correct
96 Correct 1 ms 212 KB Output is correct
97 Correct 18 ms 3716 KB Output is correct
98 Correct 62 ms 14072 KB Output is correct
99 Correct 77 ms 14156 KB Output is correct
100 Correct 67 ms 14156 KB Output is correct
101 Correct 64 ms 14072 KB Output is correct
102 Correct 80 ms 14084 KB Output is correct
103 Correct 77 ms 14132 KB Output is correct
104 Correct 82 ms 14180 KB Output is correct
105 Correct 80 ms 14156 KB Output is correct