답안 #373499

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
373499 2021-03-04T21:33:02 Z Matteo_Verz 슈퍼트리 잇기 (IOI20_supertrees) C++17
100 / 100
273 ms 24300 KB
#include "supertrees.h"
#include <bits/stdc++.h>

using namespace std;

vector <vector <int>> answer;
vector <int> viz;
vector <int> countroot;
vector <int> roots;
bitset <1001> isroot;
bool ok = true;

void Solve_Component(int node, vector <vector <int>> &p, int comp) {
  int n = p.size();
  vector <int> realnodes;

  for(int i = 0; i < n; i++)
    if(p[node][i] > 0 || i == node) {
      viz[i] = comp; // mark each node as part of the component
      realnodes.push_back(i); // we keep a vector with only the nodes in the component
    }

  for(int i : realnodes) {
    for(int j = 0; j < n; j++)
      if(p[i][j] == 0 && p[node][j] != 0) { // if there is path from i to j, from j to k, but not from i to k
        ok = false;
        return;
      } else if(p[i][j] != 0 && p[node][j] == 0) { // same shit
        ok = false;
        return;
      }
  }

  for(int i : realnodes)
    isroot[i] = true; // we consider each node as the "root"

  roots.clear();
  for(int i = 0; i < realnodes.size(); i++) { // we go through each node
    if(isroot[realnodes[i]] == true) {
      roots.push_back(realnodes[i]);
      countroot[realnodes[i]]++;
      if(countroot[realnodes[i]] > 1) { // if the node is a root of more than 1 tree
        ok = false;
        return;
      }

      for(int j = i + 1; j < realnodes.size(); j++) {
        if(p[realnodes[i]][realnodes[j]] == 1) { // if there is only 1 path from i to j => i is parent of j
          answer[realnodes[i]][realnodes[j]] = answer[realnodes[j]][realnodes[i]] = 1;
          isroot[realnodes[j]] = false;
        } else if(p[realnodes[i]][realnodes[j]] == 0) { // they are in the same component, but no path between them
          ok = false;
          return;
        }
      }
    }
  }

  if(roots.size() == 1) return; // it's fine, we only have a tree
  if(roots.size() == 2) { // we have a cycle of length 2 => impossible
    ok = false;
    return;
  }

  for(int i = 0; i < roots.size() - 1; i++)
    answer[roots[i]][roots[i + 1]] = answer[roots[i + 1]][roots[i]] = 1; // we connect the roots
  answer[roots[0]][roots.back()] = answer[roots.back()][roots[0]] = 1;
}

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

  answer.resize(n);
  for(int i = 0; i < n; i++) answer[i].resize(n);

  // last subtask, p[i][j] == 3
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++)
      if(p[i][j] > 2) return 0;
  }

  viz.resize(n);
  countroot.resize(n);
  int cc(0);
  for(int i = 0; i < n; i++)
     if(viz[i] == 0)
       Solve_Component(i, p, ++cc); // solve for each component

  if(ok == false)
    return 0;

  build(answer);
  return 1;
}

Compilation message

supertrees.cpp: In function 'void Solve_Component(int, std::vector<std::vector<int> >&, int)':
supertrees.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |   for(int i = 0; i < realnodes.size(); i++) { // we go through each node
      |                  ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:47:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |       for(int j = i + 1; j < realnodes.size(); j++) {
      |                          ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |   for(int i = 0; i < roots.size() - 1; i++)
      |                  ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 10 ms 1260 KB Output is correct
7 Correct 246 ms 22172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 10 ms 1260 KB Output is correct
7 Correct 246 ms 22172 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 250 ms 22124 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 876 KB Output is correct
17 Correct 104 ms 13164 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 64 ms 6124 KB Output is correct
21 Correct 273 ms 22444 KB Output is correct
22 Correct 255 ms 23308 KB Output is correct
23 Correct 256 ms 24044 KB Output is correct
24 Correct 251 ms 24300 KB Output is correct
25 Correct 108 ms 14188 KB Output is correct
26 Correct 113 ms 13036 KB Output is correct
27 Correct 253 ms 23148 KB Output is correct
28 Correct 251 ms 24044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 11 ms 1260 KB Output is correct
9 Correct 257 ms 22252 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 10 ms 1260 KB Output is correct
13 Correct 238 ms 22112 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 748 KB Output is correct
17 Correct 104 ms 12140 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 0 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 63 ms 5868 KB Output is correct
22 Correct 248 ms 22124 KB Output is correct
23 Correct 250 ms 22124 KB Output is correct
24 Correct 261 ms 22108 KB Output is correct
25 Correct 110 ms 12140 KB Output is correct
26 Correct 115 ms 12140 KB Output is correct
27 Correct 247 ms 24044 KB Output is correct
28 Correct 256 ms 24172 KB Output is correct
29 Correct 108 ms 14160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 63 ms 5868 KB Output is correct
5 Correct 249 ms 22272 KB Output is correct
6 Correct 245 ms 22124 KB Output is correct
7 Correct 250 ms 22124 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 65 ms 5868 KB Output is correct
10 Correct 254 ms 22252 KB Output is correct
11 Correct 245 ms 22124 KB Output is correct
12 Correct 249 ms 22252 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 64 ms 5996 KB Output is correct
17 Correct 248 ms 22124 KB Output is correct
18 Correct 250 ms 22248 KB Output is correct
19 Correct 248 ms 22124 KB Output is correct
20 Correct 251 ms 22124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 10 ms 1260 KB Output is correct
7 Correct 246 ms 22172 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 250 ms 22124 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 876 KB Output is correct
17 Correct 104 ms 13164 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 64 ms 6124 KB Output is correct
21 Correct 273 ms 22444 KB Output is correct
22 Correct 255 ms 23308 KB Output is correct
23 Correct 256 ms 24044 KB Output is correct
24 Correct 251 ms 24300 KB Output is correct
25 Correct 108 ms 14188 KB Output is correct
26 Correct 113 ms 13036 KB Output is correct
27 Correct 253 ms 23148 KB Output is correct
28 Correct 251 ms 24044 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 0 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 0 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 11 ms 1260 KB Output is correct
37 Correct 257 ms 22252 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 10 ms 1260 KB Output is correct
41 Correct 238 ms 22112 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 748 KB Output is correct
45 Correct 104 ms 12140 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 63 ms 5868 KB Output is correct
50 Correct 248 ms 22124 KB Output is correct
51 Correct 250 ms 22124 KB Output is correct
52 Correct 261 ms 22108 KB Output is correct
53 Correct 110 ms 12140 KB Output is correct
54 Correct 115 ms 12140 KB Output is correct
55 Correct 247 ms 24044 KB Output is correct
56 Correct 256 ms 24172 KB Output is correct
57 Correct 108 ms 14160 KB Output is correct
58 Correct 1 ms 364 KB Output is correct
59 Correct 1 ms 492 KB Output is correct
60 Correct 5 ms 876 KB Output is correct
61 Correct 103 ms 12524 KB Output is correct
62 Correct 1 ms 364 KB Output is correct
63 Correct 1 ms 364 KB Output is correct
64 Correct 1 ms 364 KB Output is correct
65 Correct 64 ms 6252 KB Output is correct
66 Correct 105 ms 13932 KB Output is correct
67 Correct 110 ms 13628 KB Output is correct
68 Correct 110 ms 12780 KB Output is correct
69 Correct 109 ms 14060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 0 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 10 ms 1260 KB Output is correct
7 Correct 246 ms 22172 KB Output is correct
8 Correct 0 ms 364 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 11 ms 1260 KB Output is correct
13 Correct 250 ms 22124 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 5 ms 876 KB Output is correct
17 Correct 104 ms 13164 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 64 ms 6124 KB Output is correct
21 Correct 273 ms 22444 KB Output is correct
22 Correct 255 ms 23308 KB Output is correct
23 Correct 256 ms 24044 KB Output is correct
24 Correct 251 ms 24300 KB Output is correct
25 Correct 108 ms 14188 KB Output is correct
26 Correct 113 ms 13036 KB Output is correct
27 Correct 253 ms 23148 KB Output is correct
28 Correct 251 ms 24044 KB Output is correct
29 Correct 1 ms 364 KB Output is correct
30 Correct 0 ms 364 KB Output is correct
31 Correct 1 ms 364 KB Output is correct
32 Correct 1 ms 364 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 0 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 11 ms 1260 KB Output is correct
37 Correct 257 ms 22252 KB Output is correct
38 Correct 1 ms 364 KB Output is correct
39 Correct 1 ms 364 KB Output is correct
40 Correct 10 ms 1260 KB Output is correct
41 Correct 238 ms 22112 KB Output is correct
42 Correct 1 ms 364 KB Output is correct
43 Correct 1 ms 364 KB Output is correct
44 Correct 5 ms 748 KB Output is correct
45 Correct 104 ms 12140 KB Output is correct
46 Correct 1 ms 364 KB Output is correct
47 Correct 0 ms 364 KB Output is correct
48 Correct 1 ms 364 KB Output is correct
49 Correct 63 ms 5868 KB Output is correct
50 Correct 248 ms 22124 KB Output is correct
51 Correct 250 ms 22124 KB Output is correct
52 Correct 261 ms 22108 KB Output is correct
53 Correct 110 ms 12140 KB Output is correct
54 Correct 115 ms 12140 KB Output is correct
55 Correct 247 ms 24044 KB Output is correct
56 Correct 256 ms 24172 KB Output is correct
57 Correct 108 ms 14160 KB Output is correct
58 Correct 0 ms 364 KB Output is correct
59 Correct 0 ms 364 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 63 ms 5868 KB Output is correct
62 Correct 249 ms 22272 KB Output is correct
63 Correct 245 ms 22124 KB Output is correct
64 Correct 250 ms 22124 KB Output is correct
65 Correct 1 ms 364 KB Output is correct
66 Correct 65 ms 5868 KB Output is correct
67 Correct 254 ms 22252 KB Output is correct
68 Correct 245 ms 22124 KB Output is correct
69 Correct 249 ms 22252 KB Output is correct
70 Correct 1 ms 364 KB Output is correct
71 Correct 1 ms 364 KB Output is correct
72 Correct 1 ms 364 KB Output is correct
73 Correct 64 ms 5996 KB Output is correct
74 Correct 248 ms 22124 KB Output is correct
75 Correct 250 ms 22248 KB Output is correct
76 Correct 248 ms 22124 KB Output is correct
77 Correct 251 ms 22124 KB Output is correct
78 Correct 1 ms 364 KB Output is correct
79 Correct 1 ms 492 KB Output is correct
80 Correct 5 ms 876 KB Output is correct
81 Correct 103 ms 12524 KB Output is correct
82 Correct 1 ms 364 KB Output is correct
83 Correct 1 ms 364 KB Output is correct
84 Correct 1 ms 364 KB Output is correct
85 Correct 64 ms 6252 KB Output is correct
86 Correct 105 ms 13932 KB Output is correct
87 Correct 110 ms 13628 KB Output is correct
88 Correct 110 ms 12780 KB Output is correct
89 Correct 109 ms 14060 KB Output is correct
90 Correct 1 ms 364 KB Output is correct
91 Correct 1 ms 364 KB Output is correct
92 Correct 5 ms 876 KB Output is correct
93 Correct 103 ms 12524 KB Output is correct
94 Correct 1 ms 364 KB Output is correct
95 Correct 1 ms 364 KB Output is correct
96 Correct 1 ms 364 KB Output is correct
97 Correct 27 ms 3696 KB Output is correct
98 Correct 101 ms 12652 KB Output is correct
99 Correct 101 ms 12524 KB Output is correct
100 Correct 101 ms 13036 KB Output is correct
101 Correct 100 ms 12524 KB Output is correct
102 Correct 92 ms 12524 KB Output is correct
103 Correct 93 ms 13292 KB Output is correct
104 Correct 101 ms 12780 KB Output is correct
105 Correct 104 ms 13676 KB Output is correct