Submission #956577

# Submission time Handle Problem Language Result Execution time Memory
956577 2024-04-02T07:38:59 Z Numberz Connecting Supertrees (IOI20_supertrees) C++14
100 / 100
587 ms 24416 KB
#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;

int construct(vector<std::vector<int>> p) {
  //subtask 1
  int n = p.size();
  vector<vector<int>> res(n, vector<int>(n, 0));

  //check if even possible
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (p[i][j] == 1) {
        for (int k = j; k < n; k++) {
          if (p[i][j] != 0 && p[i][k] != 0 && p[j][k] == 0) return 0;
        }
      } 
      else if (p[i][j] == 2) {
        for (int k = j; k < n; k++) {
          if (p[i][j] != 0 && p[i][k] != 0 && p[j][k] == 0) return 0;
        }
      }
      else if (p[i][j] == 3) {
        return false;
      }
    }
  }

  //build the components
  //this builds all the needed trees
  vector<vector<int>> trees;
  vector<bool> used(n, false);
  vector<int> roots;

  for (int i = 0; i < n; i++) {
    if (used[i]) continue;
    used[i] = true;
    trees.push_back({i});
    roots.push_back(i);
    for (int j = i+1; j < n; j++) {
      if (p[i][j] == 1) {
        used[j] = true;
        trees[trees.size()-1].push_back(j);
        res[i][j] = 1;
        res[j][i] = 1;
      }
    }
  }

  //we now build cycles out of the roots
  vector<vector<int>> cycles;
  vector<bool> used_cyc(roots.size(), false);
  for (int r = 0; r < roots.size(); r++) {
    if (used_cyc[r]) continue;
    used_cyc[r] = true;
    cycles.push_back({roots[r]});
    for (int j = r+1; j < roots.size(); j++) {
      if (p[roots[r]][roots[j]] == 2) {
        used_cyc[j] = true;
        cycles[cycles.size()-1].push_back(roots[j]);
      }
    }
  }

  for (auto cyc : cycles) {
    if (cyc.size() == 2) {
      return 0;
    }
    if (cyc.size() > 2) {
      int k = cyc.size();
      for (int i = 0; i < k-1; i++) {
        res[cyc[i]][cyc[i+1]] = 1;
        res[cyc[i+1]][cyc[i]] = 1;
      }
      res[cyc[k-1]][cyc[0]] = 1;
      res[cyc[0]][cyc[k-1]] = 1;
    }
  }
  
  build(res);
  return 1;
}

Compilation message

supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:53:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |   for (int r = 0; r < roots.size(); r++) {
      |                   ~~^~~~~~~~~~~~~~
supertrees.cpp:57:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (int j = r+1; j < roots.size(); j++) {
      |                       ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 436 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 10 ms 1184 KB Output is correct
7 Correct 530 ms 23604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 436 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 10 ms 1184 KB Output is correct
7 Correct 530 ms 23604 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 160 ms 23996 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 61 ms 14084 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 41 ms 6268 KB Output is correct
21 Correct 210 ms 24068 KB Output is correct
22 Correct 164 ms 24008 KB Output is correct
23 Correct 485 ms 24216 KB Output is correct
24 Correct 171 ms 24416 KB Output is correct
25 Correct 105 ms 14088 KB Output is correct
26 Correct 57 ms 14160 KB Output is correct
27 Correct 501 ms 24144 KB Output is correct
28 Correct 156 ms 24204 KB Output is correct
# Verdict Execution time Memory 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 436 KB Output is correct
8 Correct 7 ms 1368 KB Output is correct
9 Correct 156 ms 24072 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 9 ms 1372 KB Output is correct
13 Correct 414 ms 24040 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 3 ms 936 KB Output is correct
17 Correct 67 ms 13924 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 40 ms 6228 KB Output is correct
22 Correct 221 ms 24040 KB Output is correct
23 Correct 163 ms 23956 KB Output is correct
24 Correct 540 ms 23888 KB Output is correct
25 Correct 69 ms 14168 KB Output is correct
26 Correct 64 ms 13908 KB Output is correct
27 Correct 192 ms 23892 KB Output is correct
28 Correct 587 ms 23780 KB Output is correct
29 Correct 60 ms 14160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 40 ms 6236 KB Output is correct
5 Correct 212 ms 23272 KB Output is correct
6 Correct 154 ms 23300 KB Output is correct
7 Correct 523 ms 23380 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 42 ms 6236 KB Output is correct
10 Correct 208 ms 23380 KB Output is correct
11 Correct 153 ms 23380 KB Output is correct
12 Correct 544 ms 23316 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 436 KB Output is correct
16 Correct 38 ms 6236 KB Output is correct
17 Correct 209 ms 24144 KB Output is correct
18 Correct 212 ms 24128 KB Output is correct
19 Correct 193 ms 24060 KB Output is correct
20 Correct 155 ms 24056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 436 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 10 ms 1184 KB Output is correct
7 Correct 530 ms 23604 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 160 ms 23996 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 61 ms 14084 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 41 ms 6268 KB Output is correct
21 Correct 210 ms 24068 KB Output is correct
22 Correct 164 ms 24008 KB Output is correct
23 Correct 485 ms 24216 KB Output is correct
24 Correct 171 ms 24416 KB Output is correct
25 Correct 105 ms 14088 KB Output is correct
26 Correct 57 ms 14160 KB Output is correct
27 Correct 501 ms 24144 KB Output is correct
28 Correct 156 ms 24204 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 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 436 KB Output is correct
36 Correct 7 ms 1368 KB Output is correct
37 Correct 156 ms 24072 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 344 KB Output is correct
40 Correct 9 ms 1372 KB Output is correct
41 Correct 414 ms 24040 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 936 KB Output is correct
45 Correct 67 ms 13924 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 40 ms 6228 KB Output is correct
50 Correct 221 ms 24040 KB Output is correct
51 Correct 163 ms 23956 KB Output is correct
52 Correct 540 ms 23888 KB Output is correct
53 Correct 69 ms 14168 KB Output is correct
54 Correct 64 ms 13908 KB Output is correct
55 Correct 192 ms 23892 KB Output is correct
56 Correct 587 ms 23780 KB Output is correct
57 Correct 60 ms 14160 KB Output is correct
58 Correct 0 ms 344 KB Output is correct
59 Correct 0 ms 344 KB Output is correct
60 Correct 3 ms 860 KB Output is correct
61 Correct 59 ms 14068 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 348 KB Output is correct
65 Correct 43 ms 6228 KB Output is correct
66 Correct 70 ms 14160 KB Output is correct
67 Correct 61 ms 14200 KB Output is correct
68 Correct 63 ms 14164 KB Output is correct
69 Correct 60 ms 14216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 436 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 10 ms 1184 KB Output is correct
7 Correct 530 ms 23604 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 7 ms 1372 KB Output is correct
13 Correct 160 ms 23996 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 3 ms 860 KB Output is correct
17 Correct 61 ms 14084 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 41 ms 6268 KB Output is correct
21 Correct 210 ms 24068 KB Output is correct
22 Correct 164 ms 24008 KB Output is correct
23 Correct 485 ms 24216 KB Output is correct
24 Correct 171 ms 24416 KB Output is correct
25 Correct 105 ms 14088 KB Output is correct
26 Correct 57 ms 14160 KB Output is correct
27 Correct 501 ms 24144 KB Output is correct
28 Correct 156 ms 24204 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 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 436 KB Output is correct
36 Correct 7 ms 1368 KB Output is correct
37 Correct 156 ms 24072 KB Output is correct
38 Correct 0 ms 344 KB Output is correct
39 Correct 0 ms 344 KB Output is correct
40 Correct 9 ms 1372 KB Output is correct
41 Correct 414 ms 24040 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 3 ms 936 KB Output is correct
45 Correct 67 ms 13924 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 40 ms 6228 KB Output is correct
50 Correct 221 ms 24040 KB Output is correct
51 Correct 163 ms 23956 KB Output is correct
52 Correct 540 ms 23888 KB Output is correct
53 Correct 69 ms 14168 KB Output is correct
54 Correct 64 ms 13908 KB Output is correct
55 Correct 192 ms 23892 KB Output is correct
56 Correct 587 ms 23780 KB Output is correct
57 Correct 60 ms 14160 KB Output is correct
58 Correct 0 ms 344 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 40 ms 6236 KB Output is correct
62 Correct 212 ms 23272 KB Output is correct
63 Correct 154 ms 23300 KB Output is correct
64 Correct 523 ms 23380 KB Output is correct
65 Correct 1 ms 348 KB Output is correct
66 Correct 42 ms 6236 KB Output is correct
67 Correct 208 ms 23380 KB Output is correct
68 Correct 153 ms 23380 KB Output is correct
69 Correct 544 ms 23316 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 436 KB Output is correct
73 Correct 38 ms 6236 KB Output is correct
74 Correct 209 ms 24144 KB Output is correct
75 Correct 212 ms 24128 KB Output is correct
76 Correct 193 ms 24060 KB Output is correct
77 Correct 155 ms 24056 KB Output is correct
78 Correct 0 ms 344 KB Output is correct
79 Correct 0 ms 344 KB Output is correct
80 Correct 3 ms 860 KB Output is correct
81 Correct 59 ms 14068 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 348 KB Output is correct
85 Correct 43 ms 6228 KB Output is correct
86 Correct 70 ms 14160 KB Output is correct
87 Correct 61 ms 14200 KB Output is correct
88 Correct 63 ms 14164 KB Output is correct
89 Correct 60 ms 14216 KB Output is correct
90 Correct 0 ms 348 KB Output is correct
91 Correct 1 ms 432 KB Output is correct
92 Correct 3 ms 1116 KB Output is correct
93 Correct 59 ms 14164 KB Output is correct
94 Correct 1 ms 344 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 0 ms 344 KB Output is correct
97 Correct 15 ms 3848 KB Output is correct
98 Correct 62 ms 14164 KB Output is correct
99 Correct 67 ms 14160 KB Output is correct
100 Correct 57 ms 14204 KB Output is correct
101 Correct 59 ms 14164 KB Output is correct
102 Correct 57 ms 14068 KB Output is correct
103 Correct 57 ms 14092 KB Output is correct
104 Correct 58 ms 14160 KB Output is correct
105 Correct 66 ms 14164 KB Output is correct