#include "supertrees.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <iomanip>
#include <functional>
#include <bitset>
typedef long long ll;
typedef long double ld;
using namespace std;
//void build(vector<vector<int>> b);
vector<int>f;
vector<vector<int> > b;
void dfs(int vr, vector<int>&v, vector<vector<int>> &p, int edge = -1)
{
v.push_back(vr);
f[vr] = 1;
for (int i = 0; i < p.size(); i++)
{
if (i != vr && ((edge == -1 && p[vr][i]) || p[vr][i] == edge) && f[i] == 0)
{
dfs(i, v, p, edge);
}
}
}
bool create(vector<int>& v, bool cycle = false)
{
// vytvori strom/cyklus
if (cycle && v.size() <= 2)
{
return false;
}
for (int i = 0; i < v.size() - 1; i++)
{
b[v[i]][v[i + 1]] = b[v[i + 1]][v[i]] = 1;
}
if (cycle)
{
b[v[0]][v.back()] = b[v.back()][v[0]] = 1;
}
return true;
}
int construct(vector<vector<int>> p)
{
int n = p.size();
b.resize(n, vector<int>(n, 0));
f.resize(n);
for (int i = 0; i < n; i++)
{
if (count(p[i].begin(), p[i].end(), 3)) return 0;
if (!f[i])
{
vector<int>v;
dfs(i, v, p);
if (v.size() == 1)
{
continue;
}
int cc = 0;
for (int j : v)
{
for (int k : v)
{
if (!p[j][k])
{
return 0;
}
if (p[j][k] == 2)
{
cc++;
}
}
}
if (!cc)
{
create(v);
continue;
}
for (int j : v)
{
f[j] = 0;
}
// lubovolny prvok z kazdeho stromu moze byt v cykle!
// cize spojime tie stromy a spojime lubovolne ich korene
vector<int> roots;
for (int j : v)
{
if (!f[j])
{
roots.push_back(j);
vector<int>v2;
dfs(j, v2, p, 1);
for (int k : v2)
{
for (int l : v2)
{
if (p[k][l] == 2)
{
return 0;
}
}
}
create(v2);
}
}
if (!create(roots, true))
{
return 0;
}
}
}
build(b);
return 1;
}
Compilation message
supertrees.cpp: In function 'void dfs(int, std::vector<int>&, std::vector<std::vector<int> >&, int)':
supertrees.cpp:25:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i = 0; i < p.size(); i++)
| ~~^~~~~~~~~~
supertrees.cpp: In function 'bool create(std::vector<int>&, bool)':
supertrees.cpp:40:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 0; i < v.size() - 1; i++)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1100 KB |
Output is correct |
7 |
Correct |
190 ms |
22024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1100 KB |
Output is correct |
7 |
Correct |
190 ms |
22024 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
8 ms |
1088 KB |
Output is correct |
13 |
Correct |
176 ms |
21956 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
90 ms |
12164 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
43 ms |
5688 KB |
Output is correct |
21 |
Correct |
181 ms |
22024 KB |
Output is correct |
22 |
Correct |
171 ms |
22008 KB |
Output is correct |
23 |
Correct |
206 ms |
22072 KB |
Output is correct |
24 |
Correct |
172 ms |
22028 KB |
Output is correct |
25 |
Correct |
76 ms |
12032 KB |
Output is correct |
26 |
Correct |
81 ms |
12040 KB |
Output is correct |
27 |
Correct |
190 ms |
22048 KB |
Output is correct |
28 |
Correct |
172 ms |
21920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
9 ms |
1100 KB |
Output is correct |
9 |
Correct |
174 ms |
21940 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
8 ms |
1088 KB |
Output is correct |
13 |
Correct |
181 ms |
22100 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
784 KB |
Output is correct |
17 |
Correct |
92 ms |
14072 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
292 KB |
Output is correct |
20 |
Correct |
1 ms |
292 KB |
Output is correct |
21 |
Correct |
45 ms |
6240 KB |
Output is correct |
22 |
Correct |
176 ms |
23916 KB |
Output is correct |
23 |
Correct |
185 ms |
24056 KB |
Output is correct |
24 |
Correct |
195 ms |
24004 KB |
Output is correct |
25 |
Correct |
76 ms |
14144 KB |
Output is correct |
26 |
Correct |
83 ms |
14072 KB |
Output is correct |
27 |
Correct |
173 ms |
23876 KB |
Output is correct |
28 |
Correct |
193 ms |
23984 KB |
Output is correct |
29 |
Correct |
77 ms |
14020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
51 ms |
5652 KB |
Output is correct |
5 |
Correct |
181 ms |
22084 KB |
Output is correct |
6 |
Correct |
171 ms |
22012 KB |
Output is correct |
7 |
Correct |
189 ms |
22008 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
52 ms |
5708 KB |
Output is correct |
10 |
Correct |
184 ms |
21944 KB |
Output is correct |
11 |
Correct |
177 ms |
21956 KB |
Output is correct |
12 |
Correct |
185 ms |
22024 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
45 ms |
5756 KB |
Output is correct |
17 |
Correct |
174 ms |
22016 KB |
Output is correct |
18 |
Correct |
186 ms |
21968 KB |
Output is correct |
19 |
Correct |
171 ms |
22024 KB |
Output is correct |
20 |
Correct |
176 ms |
22044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1100 KB |
Output is correct |
7 |
Correct |
190 ms |
22024 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
8 ms |
1088 KB |
Output is correct |
13 |
Correct |
176 ms |
21956 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
90 ms |
12164 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
43 ms |
5688 KB |
Output is correct |
21 |
Correct |
181 ms |
22024 KB |
Output is correct |
22 |
Correct |
171 ms |
22008 KB |
Output is correct |
23 |
Correct |
206 ms |
22072 KB |
Output is correct |
24 |
Correct |
172 ms |
22028 KB |
Output is correct |
25 |
Correct |
76 ms |
12032 KB |
Output is correct |
26 |
Correct |
81 ms |
12040 KB |
Output is correct |
27 |
Correct |
190 ms |
22048 KB |
Output is correct |
28 |
Correct |
172 ms |
21920 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
9 ms |
1100 KB |
Output is correct |
37 |
Correct |
174 ms |
21940 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
8 ms |
1088 KB |
Output is correct |
41 |
Correct |
181 ms |
22100 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
4 ms |
784 KB |
Output is correct |
45 |
Correct |
92 ms |
14072 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
292 KB |
Output is correct |
48 |
Correct |
1 ms |
292 KB |
Output is correct |
49 |
Correct |
45 ms |
6240 KB |
Output is correct |
50 |
Correct |
176 ms |
23916 KB |
Output is correct |
51 |
Correct |
185 ms |
24056 KB |
Output is correct |
52 |
Correct |
195 ms |
24004 KB |
Output is correct |
53 |
Correct |
76 ms |
14144 KB |
Output is correct |
54 |
Correct |
83 ms |
14072 KB |
Output is correct |
55 |
Correct |
173 ms |
23876 KB |
Output is correct |
56 |
Correct |
193 ms |
23984 KB |
Output is correct |
57 |
Correct |
77 ms |
14020 KB |
Output is correct |
58 |
Correct |
1 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
4 ms |
844 KB |
Output is correct |
61 |
Correct |
87 ms |
14056 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
0 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
49 ms |
6212 KB |
Output is correct |
66 |
Correct |
78 ms |
14068 KB |
Output is correct |
67 |
Correct |
77 ms |
14068 KB |
Output is correct |
68 |
Correct |
78 ms |
13980 KB |
Output is correct |
69 |
Correct |
73 ms |
14068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
8 ms |
1100 KB |
Output is correct |
7 |
Correct |
190 ms |
22024 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
8 ms |
1088 KB |
Output is correct |
13 |
Correct |
176 ms |
21956 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
4 ms |
716 KB |
Output is correct |
17 |
Correct |
90 ms |
12164 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
43 ms |
5688 KB |
Output is correct |
21 |
Correct |
181 ms |
22024 KB |
Output is correct |
22 |
Correct |
171 ms |
22008 KB |
Output is correct |
23 |
Correct |
206 ms |
22072 KB |
Output is correct |
24 |
Correct |
172 ms |
22028 KB |
Output is correct |
25 |
Correct |
76 ms |
12032 KB |
Output is correct |
26 |
Correct |
81 ms |
12040 KB |
Output is correct |
27 |
Correct |
190 ms |
22048 KB |
Output is correct |
28 |
Correct |
172 ms |
21920 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
9 ms |
1100 KB |
Output is correct |
37 |
Correct |
174 ms |
21940 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
8 ms |
1088 KB |
Output is correct |
41 |
Correct |
181 ms |
22100 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
4 ms |
784 KB |
Output is correct |
45 |
Correct |
92 ms |
14072 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
292 KB |
Output is correct |
48 |
Correct |
1 ms |
292 KB |
Output is correct |
49 |
Correct |
45 ms |
6240 KB |
Output is correct |
50 |
Correct |
176 ms |
23916 KB |
Output is correct |
51 |
Correct |
185 ms |
24056 KB |
Output is correct |
52 |
Correct |
195 ms |
24004 KB |
Output is correct |
53 |
Correct |
76 ms |
14144 KB |
Output is correct |
54 |
Correct |
83 ms |
14072 KB |
Output is correct |
55 |
Correct |
173 ms |
23876 KB |
Output is correct |
56 |
Correct |
193 ms |
23984 KB |
Output is correct |
57 |
Correct |
77 ms |
14020 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
0 ms |
204 KB |
Output is correct |
60 |
Correct |
0 ms |
204 KB |
Output is correct |
61 |
Correct |
51 ms |
5652 KB |
Output is correct |
62 |
Correct |
181 ms |
22084 KB |
Output is correct |
63 |
Correct |
171 ms |
22012 KB |
Output is correct |
64 |
Correct |
189 ms |
22008 KB |
Output is correct |
65 |
Correct |
0 ms |
204 KB |
Output is correct |
66 |
Correct |
52 ms |
5708 KB |
Output is correct |
67 |
Correct |
184 ms |
21944 KB |
Output is correct |
68 |
Correct |
177 ms |
21956 KB |
Output is correct |
69 |
Correct |
185 ms |
22024 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
204 KB |
Output is correct |
72 |
Correct |
0 ms |
204 KB |
Output is correct |
73 |
Correct |
45 ms |
5756 KB |
Output is correct |
74 |
Correct |
174 ms |
22016 KB |
Output is correct |
75 |
Correct |
186 ms |
21968 KB |
Output is correct |
76 |
Correct |
171 ms |
22024 KB |
Output is correct |
77 |
Correct |
176 ms |
22044 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
0 ms |
204 KB |
Output is correct |
80 |
Correct |
4 ms |
844 KB |
Output is correct |
81 |
Correct |
87 ms |
14056 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
0 ms |
204 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
49 ms |
6212 KB |
Output is correct |
86 |
Correct |
78 ms |
14068 KB |
Output is correct |
87 |
Correct |
77 ms |
14068 KB |
Output is correct |
88 |
Correct |
78 ms |
13980 KB |
Output is correct |
89 |
Correct |
73 ms |
14068 KB |
Output is correct |
90 |
Correct |
0 ms |
204 KB |
Output is correct |
91 |
Correct |
1 ms |
204 KB |
Output is correct |
92 |
Correct |
4 ms |
844 KB |
Output is correct |
93 |
Correct |
80 ms |
14148 KB |
Output is correct |
94 |
Correct |
1 ms |
204 KB |
Output is correct |
95 |
Correct |
0 ms |
204 KB |
Output is correct |
96 |
Correct |
0 ms |
288 KB |
Output is correct |
97 |
Correct |
42 ms |
3692 KB |
Output is correct |
98 |
Correct |
77 ms |
14072 KB |
Output is correct |
99 |
Correct |
86 ms |
14148 KB |
Output is correct |
100 |
Correct |
76 ms |
14064 KB |
Output is correct |
101 |
Correct |
72 ms |
14056 KB |
Output is correct |
102 |
Correct |
71 ms |
14020 KB |
Output is correct |
103 |
Correct |
77 ms |
14060 KB |
Output is correct |
104 |
Correct |
76 ms |
14020 KB |
Output is correct |
105 |
Correct |
84 ms |
14072 KB |
Output is correct |