#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
/*
Sub 1 + 2: Construct numerous connected components
Sub 3: Add cycle to the numerous components (break if component size == 2)
Sub 4: Build many trees and loop around their roots
Sub 5: Verify that Law Of Tree works
Sub 6: We can't allow 3 since we would need 4
*/
typedef long long int64;
#define FOR(i, x, y) for (int i = x; i < y; i++)
#define pb push_back
#define f first
#define s second
#define vi vector<int>
const int MAXN = 1010;
bool vis[MAXN];
int par[MAXN];
int getParent(int a)
{
return a == par[a] ? a : par[a] = getParent(par[a]);
}
void combine(int a, int b)
{
par[getParent(a)] = getParent(b);
}
int construct(vector<vector<int>> p)
{
int n = p.size();
vector<vi> answer;
for (int i = 0; i < n; i++) {
vector<int> row;
row.resize(n);
answer.push_back(row);
}
// check for validity
FOR(i, 0, n) FOR(j, 0, n) if (p[i][j] == 3) return 0;
// init parents
FOR(i, 0, n) par[i] = i;
FOR(id, 0, n)
{
if (!vis[id])
{
// build a new component
bool inSet[n] = {};
vi trees;
FOR(j, 0, n) if (p[id][j]) inSet[j] = 1, trees.pb(j);
// build trees
for (int i : trees)
for (int j : trees)
if (p[i][j] == 1) combine(i, j);
// cout << "Trees built\n";
// validate tree
for (int i : trees)
{
// check that it is unique to this component
if (vis[i]) return 0;
FOR(j, 0, n) if (p[i][j] && !inSet[j]) return 0;
// cout << i << " : component check\n";
// ensure the law of trees with all other nodes that we are know are in the set
for (int j : trees)
{
// cout << j << " " << (getParent(i) != getParent(j) && p[i][j] == 2) << "\n";
// cout << j << " " << (getParent(i) == getParent(j) && p[i][j] == 1) << "\n";
if (!((getParent(i) != getParent(j) && p[i][j] == 2) or (getParent(i) == getParent(j) && p[i][j] == 1))) return 0;
}
// cout << i << " : num ways check\n";
}
for (int i : trees) vis[i] = 1;
// cout << "Tree integrity check\n";
// connect the trees
bool added[n] = {};
vi origin;
for (int j : trees)
{
int leader = getParent(j);
if (!added[leader]) origin.pb(leader), added[leader] = 1;
// build edge between nodes in the same tree
if (j != leader) answer[j][leader] = answer[leader][j] = 1;
}
for (int i = 0; i < origin.size(); i++)
{
// let us do a loopty loop
int x = origin[i], y = origin[(i+1) % origin.size()];
// it won't create a cycle if there is only a single tree
if (x != y) answer[x][y] = answer[y][x] = 1;
}
// we can't create cycles if we only have 2 nodes
if (origin.size() == 2) return 0;
}
}
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:105:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (int i = 0; i < origin.size(); i++)
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
11 ms |
1148 KB |
Output is correct |
7 |
Correct |
179 ms |
22016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
11 ms |
1148 KB |
Output is correct |
7 |
Correct |
179 ms |
22016 KB |
Output is correct |
8 |
Correct |
0 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 |
165 ms |
22000 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
804 KB |
Output is correct |
17 |
Correct |
74 ms |
14028 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
45 ms |
6264 KB |
Output is correct |
21 |
Correct |
165 ms |
23928 KB |
Output is correct |
22 |
Correct |
162 ms |
23988 KB |
Output is correct |
23 |
Correct |
176 ms |
23940 KB |
Output is correct |
24 |
Correct |
165 ms |
23956 KB |
Output is correct |
25 |
Correct |
72 ms |
13996 KB |
Output is correct |
26 |
Correct |
64 ms |
14080 KB |
Output is correct |
27 |
Correct |
180 ms |
24012 KB |
Output is correct |
28 |
Correct |
164 ms |
23932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
7 ms |
1128 KB |
Output is correct |
9 |
Correct |
176 ms |
22020 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1108 KB |
Output is correct |
13 |
Correct |
165 ms |
21968 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
70 ms |
12048 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
42 ms |
5676 KB |
Output is correct |
22 |
Correct |
175 ms |
24116 KB |
Output is correct |
23 |
Correct |
157 ms |
23932 KB |
Output is correct |
24 |
Correct |
175 ms |
24012 KB |
Output is correct |
25 |
Correct |
65 ms |
14028 KB |
Output is correct |
26 |
Correct |
68 ms |
14004 KB |
Output is correct |
27 |
Correct |
157 ms |
23928 KB |
Output is correct |
28 |
Correct |
183 ms |
24012 KB |
Output is correct |
29 |
Correct |
65 ms |
14076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
49 ms |
6292 KB |
Output is correct |
5 |
Correct |
167 ms |
23928 KB |
Output is correct |
6 |
Correct |
164 ms |
23988 KB |
Output is correct |
7 |
Correct |
176 ms |
24012 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
41 ms |
6208 KB |
Output is correct |
10 |
Correct |
166 ms |
24140 KB |
Output is correct |
11 |
Correct |
159 ms |
23944 KB |
Output is correct |
12 |
Correct |
168 ms |
23928 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
304 KB |
Output is correct |
16 |
Correct |
41 ms |
6256 KB |
Output is correct |
17 |
Correct |
162 ms |
23940 KB |
Output is correct |
18 |
Correct |
168 ms |
23920 KB |
Output is correct |
19 |
Correct |
158 ms |
23936 KB |
Output is correct |
20 |
Correct |
163 ms |
23924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
11 ms |
1148 KB |
Output is correct |
7 |
Correct |
179 ms |
22016 KB |
Output is correct |
8 |
Correct |
0 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 |
165 ms |
22000 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
804 KB |
Output is correct |
17 |
Correct |
74 ms |
14028 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
45 ms |
6264 KB |
Output is correct |
21 |
Correct |
165 ms |
23928 KB |
Output is correct |
22 |
Correct |
162 ms |
23988 KB |
Output is correct |
23 |
Correct |
176 ms |
23940 KB |
Output is correct |
24 |
Correct |
165 ms |
23956 KB |
Output is correct |
25 |
Correct |
72 ms |
13996 KB |
Output is correct |
26 |
Correct |
64 ms |
14080 KB |
Output is correct |
27 |
Correct |
180 ms |
24012 KB |
Output is correct |
28 |
Correct |
164 ms |
23932 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1128 KB |
Output is correct |
37 |
Correct |
176 ms |
22020 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
8 ms |
1108 KB |
Output is correct |
41 |
Correct |
165 ms |
21968 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
724 KB |
Output is correct |
45 |
Correct |
70 ms |
12048 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
42 ms |
5676 KB |
Output is correct |
50 |
Correct |
175 ms |
24116 KB |
Output is correct |
51 |
Correct |
157 ms |
23932 KB |
Output is correct |
52 |
Correct |
175 ms |
24012 KB |
Output is correct |
53 |
Correct |
65 ms |
14028 KB |
Output is correct |
54 |
Correct |
68 ms |
14004 KB |
Output is correct |
55 |
Correct |
157 ms |
23928 KB |
Output is correct |
56 |
Correct |
183 ms |
24012 KB |
Output is correct |
57 |
Correct |
65 ms |
14076 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
3 ms |
852 KB |
Output is correct |
61 |
Correct |
73 ms |
14084 KB |
Output is correct |
62 |
Correct |
0 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 |
6220 KB |
Output is correct |
66 |
Correct |
78 ms |
14032 KB |
Output is correct |
67 |
Correct |
65 ms |
14028 KB |
Output is correct |
68 |
Correct |
64 ms |
14084 KB |
Output is correct |
69 |
Correct |
65 ms |
14088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
11 ms |
1148 KB |
Output is correct |
7 |
Correct |
179 ms |
22016 KB |
Output is correct |
8 |
Correct |
0 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 |
165 ms |
22000 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
804 KB |
Output is correct |
17 |
Correct |
74 ms |
14028 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
45 ms |
6264 KB |
Output is correct |
21 |
Correct |
165 ms |
23928 KB |
Output is correct |
22 |
Correct |
162 ms |
23988 KB |
Output is correct |
23 |
Correct |
176 ms |
23940 KB |
Output is correct |
24 |
Correct |
165 ms |
23956 KB |
Output is correct |
25 |
Correct |
72 ms |
13996 KB |
Output is correct |
26 |
Correct |
64 ms |
14080 KB |
Output is correct |
27 |
Correct |
180 ms |
24012 KB |
Output is correct |
28 |
Correct |
164 ms |
23932 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1128 KB |
Output is correct |
37 |
Correct |
176 ms |
22020 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
8 ms |
1108 KB |
Output is correct |
41 |
Correct |
165 ms |
21968 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
724 KB |
Output is correct |
45 |
Correct |
70 ms |
12048 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
42 ms |
5676 KB |
Output is correct |
50 |
Correct |
175 ms |
24116 KB |
Output is correct |
51 |
Correct |
157 ms |
23932 KB |
Output is correct |
52 |
Correct |
175 ms |
24012 KB |
Output is correct |
53 |
Correct |
65 ms |
14028 KB |
Output is correct |
54 |
Correct |
68 ms |
14004 KB |
Output is correct |
55 |
Correct |
157 ms |
23928 KB |
Output is correct |
56 |
Correct |
183 ms |
24012 KB |
Output is correct |
57 |
Correct |
65 ms |
14076 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
49 ms |
6292 KB |
Output is correct |
62 |
Correct |
167 ms |
23928 KB |
Output is correct |
63 |
Correct |
164 ms |
23988 KB |
Output is correct |
64 |
Correct |
176 ms |
24012 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
41 ms |
6208 KB |
Output is correct |
67 |
Correct |
166 ms |
24140 KB |
Output is correct |
68 |
Correct |
159 ms |
23944 KB |
Output is correct |
69 |
Correct |
168 ms |
23928 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
304 KB |
Output is correct |
73 |
Correct |
41 ms |
6256 KB |
Output is correct |
74 |
Correct |
162 ms |
23940 KB |
Output is correct |
75 |
Correct |
168 ms |
23920 KB |
Output is correct |
76 |
Correct |
158 ms |
23936 KB |
Output is correct |
77 |
Correct |
163 ms |
23924 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
0 ms |
212 KB |
Output is correct |
80 |
Correct |
3 ms |
852 KB |
Output is correct |
81 |
Correct |
73 ms |
14084 KB |
Output is correct |
82 |
Correct |
0 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 |
6220 KB |
Output is correct |
86 |
Correct |
78 ms |
14032 KB |
Output is correct |
87 |
Correct |
65 ms |
14028 KB |
Output is correct |
88 |
Correct |
64 ms |
14084 KB |
Output is correct |
89 |
Correct |
65 ms |
14088 KB |
Output is correct |
90 |
Correct |
0 ms |
212 KB |
Output is correct |
91 |
Correct |
0 ms |
212 KB |
Output is correct |
92 |
Correct |
3 ms |
852 KB |
Output is correct |
93 |
Correct |
71 ms |
14032 KB |
Output is correct |
94 |
Correct |
0 ms |
212 KB |
Output is correct |
95 |
Correct |
0 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
304 KB |
Output is correct |
97 |
Correct |
16 ms |
3716 KB |
Output is correct |
98 |
Correct |
69 ms |
14084 KB |
Output is correct |
99 |
Correct |
67 ms |
14084 KB |
Output is correct |
100 |
Correct |
66 ms |
14072 KB |
Output is correct |
101 |
Correct |
65 ms |
14072 KB |
Output is correct |
102 |
Correct |
64 ms |
14028 KB |
Output is correct |
103 |
Correct |
67 ms |
14004 KB |
Output is correct |
104 |
Correct |
65 ms |
14072 KB |
Output is correct |
105 |
Correct |
70 ms |
14028 KB |
Output is correct |