#include <bits/stdc++.h>
#include "supertrees.h"
#pragma GCC optimize("Ofast")
#define x first
#define y second
#define ld long double
#define ll long long
using namespace std;
/*void build(vector <vector <int>> b) {
for(int i = 0; i < b.size(); i++) {
for(int j = 0; j < b[i].size(); j++)
cout << b[i][j] << " ";
cout << "\n";
}
}*/
int construct(vector <vector <int>> p) {
int n = p.size();
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(p[i][j] == 3)
return 0;
}
}
// structures can be like: a chain and a cycle / a cycle / a chain
// nodes in cycle have all values in p = 2 or 0
vector <vector <int>> adj(n);
vector <int> comp(n), chain(n), from(n, -1), nodes;
vector <bool> viz(n);
int cnt = 0;
for(int i = 0; i < n; i++)
adj[i].resize(n);
for(int i = 0; i < n; i++) {
if(!comp[i])
comp[i] = ++cnt;
else
continue;
for(int j = 0; j < n; j++) {
if(p[i][j])
comp[j] = cnt;
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if((comp[i] == comp[j]) ^ (p[i][j] != 0))
return 0;
}
}
cnt = 0;
for(int i = 0; i < n; i++) {
if(!chain[i])
chain[i] = ++cnt, nodes.push_back(i);
else
continue;
for(int j = 0; j < n; j++) {
if(p[i][j] == 1) {
chain[j] = cnt;
from[j] = i;
}
}
}
for(int i = 0; i < n; i++) {
if(from[i] >= 0 && from[i] != i)
adj[i][from[i]] = adj[from[i]][i] = 1;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if((chain[i] == chain[j] ^ (p[i][j] == 1)))
return 0;
}
}
for(auto &i : nodes) {
if(viz[i])
continue;
vector <int> v;
for(auto &j : nodes) {
if(!viz[j] && comp[i] == comp[j])
v.push_back(j), viz[j] = 1;
}
if(v.size() == 2)
return 0;
if(v.size() == 1)
continue;
for(int j = 0; j < v.size(); j++) {
int k = (j + 1) % v.size();
if(v[k] != v[j])
adj[v[j]][v[k]] = adj[v[k]][v[j]] = 1;
}
}
build(adj);
return 1;
}
/*int main() {
int n;
vector <vector <int>> p;
cin >> n;
p.resize(n);
for(int i = 0; i < n; i++) {
p[i].resize(n);
for(int j = 0; j < n; j++)
cin >> p[i][j];
}
cout << construct(p);
return 0;
}*/
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:70:20: warning: suggest parentheses around comparison in operand of '^' [-Wparentheses]
70 | if((chain[i] == chain[j] ^ (p[i][j] == 1)))
supertrees.cpp:86:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int j = 0; j < v.size(); j++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
258 ms |
22136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
258 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
11 ms |
1152 KB |
Output is correct |
13 |
Correct |
240 ms |
22104 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
111 ms |
12156 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
60 ms |
5752 KB |
Output is correct |
21 |
Correct |
245 ms |
22136 KB |
Output is correct |
22 |
Correct |
236 ms |
22092 KB |
Output is correct |
23 |
Correct |
254 ms |
22136 KB |
Output is correct |
24 |
Correct |
237 ms |
22136 KB |
Output is correct |
25 |
Correct |
97 ms |
12152 KB |
Output is correct |
26 |
Correct |
115 ms |
12152 KB |
Output is correct |
27 |
Correct |
251 ms |
22136 KB |
Output is correct |
28 |
Correct |
241 ms |
22136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
10 ms |
1152 KB |
Output is correct |
9 |
Correct |
239 ms |
22136 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
12 ms |
1152 KB |
Output is correct |
13 |
Correct |
258 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
109 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
70 ms |
5880 KB |
Output is correct |
22 |
Correct |
238 ms |
22172 KB |
Output is correct |
23 |
Correct |
241 ms |
22136 KB |
Output is correct |
24 |
Correct |
257 ms |
22140 KB |
Output is correct |
25 |
Correct |
99 ms |
12152 KB |
Output is correct |
26 |
Correct |
96 ms |
12152 KB |
Output is correct |
27 |
Correct |
241 ms |
22140 KB |
Output is correct |
28 |
Correct |
255 ms |
22136 KB |
Output is correct |
29 |
Correct |
99 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
63 ms |
5752 KB |
Output is correct |
5 |
Correct |
237 ms |
22136 KB |
Output is correct |
6 |
Correct |
234 ms |
22136 KB |
Output is correct |
7 |
Correct |
252 ms |
22104 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
59 ms |
5752 KB |
Output is correct |
10 |
Correct |
240 ms |
22136 KB |
Output is correct |
11 |
Correct |
238 ms |
22272 KB |
Output is correct |
12 |
Correct |
253 ms |
22136 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
256 KB |
Output is correct |
16 |
Correct |
62 ms |
5752 KB |
Output is correct |
17 |
Correct |
240 ms |
22136 KB |
Output is correct |
18 |
Correct |
237 ms |
22136 KB |
Output is correct |
19 |
Correct |
242 ms |
22136 KB |
Output is correct |
20 |
Correct |
240 ms |
22100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
258 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
11 ms |
1152 KB |
Output is correct |
13 |
Correct |
240 ms |
22104 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
111 ms |
12156 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
60 ms |
5752 KB |
Output is correct |
21 |
Correct |
245 ms |
22136 KB |
Output is correct |
22 |
Correct |
236 ms |
22092 KB |
Output is correct |
23 |
Correct |
254 ms |
22136 KB |
Output is correct |
24 |
Correct |
237 ms |
22136 KB |
Output is correct |
25 |
Correct |
97 ms |
12152 KB |
Output is correct |
26 |
Correct |
115 ms |
12152 KB |
Output is correct |
27 |
Correct |
251 ms |
22136 KB |
Output is correct |
28 |
Correct |
241 ms |
22136 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
0 ms |
256 KB |
Output is correct |
32 |
Correct |
0 ms |
256 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
0 ms |
256 KB |
Output is correct |
36 |
Correct |
10 ms |
1152 KB |
Output is correct |
37 |
Correct |
239 ms |
22136 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
0 ms |
256 KB |
Output is correct |
40 |
Correct |
12 ms |
1152 KB |
Output is correct |
41 |
Correct |
258 ms |
22136 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
1 ms |
256 KB |
Output is correct |
44 |
Correct |
5 ms |
768 KB |
Output is correct |
45 |
Correct |
109 ms |
12152 KB |
Output is correct |
46 |
Correct |
0 ms |
256 KB |
Output is correct |
47 |
Correct |
1 ms |
256 KB |
Output is correct |
48 |
Correct |
1 ms |
256 KB |
Output is correct |
49 |
Correct |
70 ms |
5880 KB |
Output is correct |
50 |
Correct |
238 ms |
22172 KB |
Output is correct |
51 |
Correct |
241 ms |
22136 KB |
Output is correct |
52 |
Correct |
257 ms |
22140 KB |
Output is correct |
53 |
Correct |
99 ms |
12152 KB |
Output is correct |
54 |
Correct |
96 ms |
12152 KB |
Output is correct |
55 |
Correct |
241 ms |
22140 KB |
Output is correct |
56 |
Correct |
255 ms |
22136 KB |
Output is correct |
57 |
Correct |
99 ms |
12152 KB |
Output is correct |
58 |
Correct |
0 ms |
256 KB |
Output is correct |
59 |
Correct |
0 ms |
256 KB |
Output is correct |
60 |
Correct |
5 ms |
768 KB |
Output is correct |
61 |
Correct |
113 ms |
12120 KB |
Output is correct |
62 |
Correct |
0 ms |
256 KB |
Output is correct |
63 |
Correct |
0 ms |
256 KB |
Output is correct |
64 |
Correct |
0 ms |
256 KB |
Output is correct |
65 |
Correct |
62 ms |
5880 KB |
Output is correct |
66 |
Correct |
97 ms |
12116 KB |
Output is correct |
67 |
Correct |
101 ms |
12184 KB |
Output is correct |
68 |
Correct |
98 ms |
12188 KB |
Output is correct |
69 |
Correct |
98 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
11 ms |
1152 KB |
Output is correct |
7 |
Correct |
258 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
11 ms |
1152 KB |
Output is correct |
13 |
Correct |
240 ms |
22104 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
111 ms |
12156 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
60 ms |
5752 KB |
Output is correct |
21 |
Correct |
245 ms |
22136 KB |
Output is correct |
22 |
Correct |
236 ms |
22092 KB |
Output is correct |
23 |
Correct |
254 ms |
22136 KB |
Output is correct |
24 |
Correct |
237 ms |
22136 KB |
Output is correct |
25 |
Correct |
97 ms |
12152 KB |
Output is correct |
26 |
Correct |
115 ms |
12152 KB |
Output is correct |
27 |
Correct |
251 ms |
22136 KB |
Output is correct |
28 |
Correct |
241 ms |
22136 KB |
Output is correct |
29 |
Correct |
0 ms |
256 KB |
Output is correct |
30 |
Correct |
0 ms |
256 KB |
Output is correct |
31 |
Correct |
0 ms |
256 KB |
Output is correct |
32 |
Correct |
0 ms |
256 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
0 ms |
256 KB |
Output is correct |
36 |
Correct |
10 ms |
1152 KB |
Output is correct |
37 |
Correct |
239 ms |
22136 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
0 ms |
256 KB |
Output is correct |
40 |
Correct |
12 ms |
1152 KB |
Output is correct |
41 |
Correct |
258 ms |
22136 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
1 ms |
256 KB |
Output is correct |
44 |
Correct |
5 ms |
768 KB |
Output is correct |
45 |
Correct |
109 ms |
12152 KB |
Output is correct |
46 |
Correct |
0 ms |
256 KB |
Output is correct |
47 |
Correct |
1 ms |
256 KB |
Output is correct |
48 |
Correct |
1 ms |
256 KB |
Output is correct |
49 |
Correct |
70 ms |
5880 KB |
Output is correct |
50 |
Correct |
238 ms |
22172 KB |
Output is correct |
51 |
Correct |
241 ms |
22136 KB |
Output is correct |
52 |
Correct |
257 ms |
22140 KB |
Output is correct |
53 |
Correct |
99 ms |
12152 KB |
Output is correct |
54 |
Correct |
96 ms |
12152 KB |
Output is correct |
55 |
Correct |
241 ms |
22140 KB |
Output is correct |
56 |
Correct |
255 ms |
22136 KB |
Output is correct |
57 |
Correct |
99 ms |
12152 KB |
Output is correct |
58 |
Correct |
1 ms |
256 KB |
Output is correct |
59 |
Correct |
1 ms |
256 KB |
Output is correct |
60 |
Correct |
1 ms |
256 KB |
Output is correct |
61 |
Correct |
63 ms |
5752 KB |
Output is correct |
62 |
Correct |
237 ms |
22136 KB |
Output is correct |
63 |
Correct |
234 ms |
22136 KB |
Output is correct |
64 |
Correct |
252 ms |
22104 KB |
Output is correct |
65 |
Correct |
0 ms |
256 KB |
Output is correct |
66 |
Correct |
59 ms |
5752 KB |
Output is correct |
67 |
Correct |
240 ms |
22136 KB |
Output is correct |
68 |
Correct |
238 ms |
22272 KB |
Output is correct |
69 |
Correct |
253 ms |
22136 KB |
Output is correct |
70 |
Correct |
0 ms |
256 KB |
Output is correct |
71 |
Correct |
0 ms |
256 KB |
Output is correct |
72 |
Correct |
1 ms |
256 KB |
Output is correct |
73 |
Correct |
62 ms |
5752 KB |
Output is correct |
74 |
Correct |
240 ms |
22136 KB |
Output is correct |
75 |
Correct |
237 ms |
22136 KB |
Output is correct |
76 |
Correct |
242 ms |
22136 KB |
Output is correct |
77 |
Correct |
240 ms |
22100 KB |
Output is correct |
78 |
Correct |
0 ms |
256 KB |
Output is correct |
79 |
Correct |
0 ms |
256 KB |
Output is correct |
80 |
Correct |
5 ms |
768 KB |
Output is correct |
81 |
Correct |
113 ms |
12120 KB |
Output is correct |
82 |
Correct |
0 ms |
256 KB |
Output is correct |
83 |
Correct |
0 ms |
256 KB |
Output is correct |
84 |
Correct |
0 ms |
256 KB |
Output is correct |
85 |
Correct |
62 ms |
5880 KB |
Output is correct |
86 |
Correct |
97 ms |
12116 KB |
Output is correct |
87 |
Correct |
101 ms |
12184 KB |
Output is correct |
88 |
Correct |
98 ms |
12188 KB |
Output is correct |
89 |
Correct |
98 ms |
12124 KB |
Output is correct |
90 |
Correct |
0 ms |
256 KB |
Output is correct |
91 |
Correct |
1 ms |
256 KB |
Output is correct |
92 |
Correct |
5 ms |
640 KB |
Output is correct |
93 |
Correct |
112 ms |
8184 KB |
Output is correct |
94 |
Correct |
1 ms |
256 KB |
Output is correct |
95 |
Correct |
0 ms |
256 KB |
Output is correct |
96 |
Correct |
0 ms |
256 KB |
Output is correct |
97 |
Correct |
23 ms |
2296 KB |
Output is correct |
98 |
Correct |
93 ms |
8184 KB |
Output is correct |
99 |
Correct |
106 ms |
8148 KB |
Output is correct |
100 |
Correct |
98 ms |
8184 KB |
Output is correct |
101 |
Correct |
94 ms |
8184 KB |
Output is correct |
102 |
Correct |
112 ms |
8280 KB |
Output is correct |
103 |
Correct |
112 ms |
8184 KB |
Output is correct |
104 |
Correct |
119 ms |
8160 KB |
Output is correct |
105 |
Correct |
109 ms |
8152 KB |
Output is correct |