#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define re resize
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define loop(i, n) for(int i = 0; i < n; i++)
#define loop1(i, n) for(int i = 1; i <= n; i++)
#define print(x) cout << #x << ": " << x << endl << flush
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
typedef array<int, 3> ti;
typedef vector<ii> vii;
typedef vector<ti> vti;
typedef priority_queue<int> pq;
template<class T> bool ckmin(T&a, T b) { bool B = a > b; a = min(a, b); return B; }
template<class T> bool ckmax(T&a, T b) { bool B = a < b; a = max(a, b); return B; }
const int inf = 1e17;
//const int maxn = ;
struct ufds {
vi par;
ufds(int n): par(n+1, -1) {}
int find(int a) {
while(par[a] >= 0)
a = par[a];
return a;
}
int size(int a) {
return -par[find(a)];
}
bool disjoint(int a, int b) {
a = find(a);
b = find(b);
return !(a == b);
}
bool merge(int a, int b) {
a = find(a);
b = find(b);
if(a == b) return false;
if(-par[a] < -par[b])
swap(a, b);
par[a] += par[b];
par[b] = a;
return true;
}
bool query(int a, int b) {
a = find(a);
b = find(b);
return a == b;
}
};
int construct(vector<vi> p) {
int n = p.size();
bool possible = true;
ufds ufds1(n);
loop(i, n) loop(j, n) {
if(p[i][j] == 1)
ufds1.merge(i, j);
}
vi par(n);
map<int, vi> groups;
loop(i, n) {
int x = ufds1.find(i);
par[i] = x;
if(x != i) groups[x].pb(i);
}
vi keys;
loop(i, n) if(par[i] == i) {
keys.pb(i);
}
loop(i, n) loop(j, n) {
if(p[i][j] != p[par[i]][par[j]])
possible = false;
if(p[i][j] == 3) possible = false;
}
ufds ufds2(n);
for(auto i : keys) for(auto j : keys) {
if(p[i][j] == 2)
ufds2.merge(i, j);
}
vi par2(n);
map<int, vi> groups2;
for(auto i : keys) {
int x = ufds2.find(i);
par2[i] = x;
if(x != i) groups2[x].pb(i);
}
for(auto i : keys) for(auto j : keys) if(i != j) {
if(p[i][j] != 2) if(!ufds2.disjoint(i, j))
possible = false;
}
vector<vi> ans(n, vi(n));
for(auto [node, grp] : groups) {
for(auto x : grp) {
ans[x][node] = 1;
ans[node][x] = 1;
}
}
for(auto [node, grp] : groups2) {
if(grp.size() == 1) possible = false;
int prev = node;
for(auto x : grp) {
ans[prev][x] = 1;
ans[x][prev] = 1;
prev = x;
}
ans[prev][node] = 1;
ans[node][prev] = 1;
}
if(!possible) return 0;
build(ans);
return 1;
}
Compilation message
supertrees.cpp:27:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+17' to '2147483647' [-Woverflow]
27 | const int inf = 1e17;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1236 KB |
Output is correct |
7 |
Correct |
177 ms |
22276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1236 KB |
Output is correct |
7 |
Correct |
177 ms |
22276 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
8 ms |
1248 KB |
Output is correct |
13 |
Correct |
166 ms |
22332 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
82 ms |
12364 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
49 ms |
6008 KB |
Output is correct |
21 |
Correct |
167 ms |
22276 KB |
Output is correct |
22 |
Correct |
164 ms |
22336 KB |
Output is correct |
23 |
Correct |
174 ms |
22348 KB |
Output is correct |
24 |
Correct |
168 ms |
22256 KB |
Output is correct |
25 |
Correct |
73 ms |
12292 KB |
Output is correct |
26 |
Correct |
71 ms |
12364 KB |
Output is correct |
27 |
Correct |
176 ms |
22300 KB |
Output is correct |
28 |
Correct |
172 ms |
22216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
8 ms |
1236 KB |
Output is correct |
9 |
Correct |
171 ms |
23932 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
7 ms |
1272 KB |
Output is correct |
13 |
Correct |
166 ms |
23972 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
296 KB |
Output is correct |
16 |
Correct |
5 ms |
796 KB |
Output is correct |
17 |
Correct |
92 ms |
13996 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
296 KB |
Output is correct |
21 |
Correct |
43 ms |
6244 KB |
Output is correct |
22 |
Correct |
177 ms |
24012 KB |
Output is correct |
23 |
Correct |
169 ms |
23940 KB |
Output is correct |
24 |
Correct |
183 ms |
23928 KB |
Output is correct |
25 |
Correct |
70 ms |
14064 KB |
Output is correct |
26 |
Correct |
74 ms |
14068 KB |
Output is correct |
27 |
Correct |
172 ms |
24016 KB |
Output is correct |
28 |
Correct |
181 ms |
24016 KB |
Output is correct |
29 |
Correct |
71 ms |
14028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
42 ms |
6064 KB |
Output is correct |
5 |
Correct |
168 ms |
22376 KB |
Output is correct |
6 |
Correct |
165 ms |
22444 KB |
Output is correct |
7 |
Correct |
175 ms |
22344 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
42 ms |
6008 KB |
Output is correct |
10 |
Correct |
182 ms |
22536 KB |
Output is correct |
11 |
Correct |
175 ms |
22476 KB |
Output is correct |
12 |
Correct |
183 ms |
22472 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
44 ms |
6056 KB |
Output is correct |
17 |
Correct |
169 ms |
22412 KB |
Output is correct |
18 |
Correct |
175 ms |
22472 KB |
Output is correct |
19 |
Correct |
176 ms |
22364 KB |
Output is correct |
20 |
Correct |
179 ms |
22348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1236 KB |
Output is correct |
7 |
Correct |
177 ms |
22276 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
8 ms |
1248 KB |
Output is correct |
13 |
Correct |
166 ms |
22332 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
82 ms |
12364 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
49 ms |
6008 KB |
Output is correct |
21 |
Correct |
167 ms |
22276 KB |
Output is correct |
22 |
Correct |
164 ms |
22336 KB |
Output is correct |
23 |
Correct |
174 ms |
22348 KB |
Output is correct |
24 |
Correct |
168 ms |
22256 KB |
Output is correct |
25 |
Correct |
73 ms |
12292 KB |
Output is correct |
26 |
Correct |
71 ms |
12364 KB |
Output is correct |
27 |
Correct |
176 ms |
22300 KB |
Output is correct |
28 |
Correct |
172 ms |
22216 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 |
296 KB |
Output is correct |
32 |
Correct |
1 ms |
300 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
171 ms |
23932 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
304 KB |
Output is correct |
40 |
Correct |
7 ms |
1272 KB |
Output is correct |
41 |
Correct |
166 ms |
23972 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
296 KB |
Output is correct |
44 |
Correct |
5 ms |
796 KB |
Output is correct |
45 |
Correct |
92 ms |
13996 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
296 KB |
Output is correct |
49 |
Correct |
43 ms |
6244 KB |
Output is correct |
50 |
Correct |
177 ms |
24012 KB |
Output is correct |
51 |
Correct |
169 ms |
23940 KB |
Output is correct |
52 |
Correct |
183 ms |
23928 KB |
Output is correct |
53 |
Correct |
70 ms |
14064 KB |
Output is correct |
54 |
Correct |
74 ms |
14068 KB |
Output is correct |
55 |
Correct |
172 ms |
24016 KB |
Output is correct |
56 |
Correct |
181 ms |
24016 KB |
Output is correct |
57 |
Correct |
71 ms |
14028 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
4 ms |
756 KB |
Output is correct |
61 |
Correct |
82 ms |
14100 KB |
Output is correct |
62 |
Correct |
1 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
44 ms |
6260 KB |
Output is correct |
66 |
Correct |
75 ms |
14004 KB |
Output is correct |
67 |
Correct |
74 ms |
14068 KB |
Output is correct |
68 |
Correct |
71 ms |
14028 KB |
Output is correct |
69 |
Correct |
72 ms |
14084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
8 ms |
1236 KB |
Output is correct |
7 |
Correct |
177 ms |
22276 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
8 ms |
1248 KB |
Output is correct |
13 |
Correct |
166 ms |
22332 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
82 ms |
12364 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
49 ms |
6008 KB |
Output is correct |
21 |
Correct |
167 ms |
22276 KB |
Output is correct |
22 |
Correct |
164 ms |
22336 KB |
Output is correct |
23 |
Correct |
174 ms |
22348 KB |
Output is correct |
24 |
Correct |
168 ms |
22256 KB |
Output is correct |
25 |
Correct |
73 ms |
12292 KB |
Output is correct |
26 |
Correct |
71 ms |
12364 KB |
Output is correct |
27 |
Correct |
176 ms |
22300 KB |
Output is correct |
28 |
Correct |
172 ms |
22216 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 |
296 KB |
Output is correct |
32 |
Correct |
1 ms |
300 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
171 ms |
23932 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
304 KB |
Output is correct |
40 |
Correct |
7 ms |
1272 KB |
Output is correct |
41 |
Correct |
166 ms |
23972 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
296 KB |
Output is correct |
44 |
Correct |
5 ms |
796 KB |
Output is correct |
45 |
Correct |
92 ms |
13996 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
296 KB |
Output is correct |
49 |
Correct |
43 ms |
6244 KB |
Output is correct |
50 |
Correct |
177 ms |
24012 KB |
Output is correct |
51 |
Correct |
169 ms |
23940 KB |
Output is correct |
52 |
Correct |
183 ms |
23928 KB |
Output is correct |
53 |
Correct |
70 ms |
14064 KB |
Output is correct |
54 |
Correct |
74 ms |
14068 KB |
Output is correct |
55 |
Correct |
172 ms |
24016 KB |
Output is correct |
56 |
Correct |
181 ms |
24016 KB |
Output is correct |
57 |
Correct |
71 ms |
14028 KB |
Output is correct |
58 |
Correct |
1 ms |
296 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
300 KB |
Output is correct |
61 |
Correct |
42 ms |
6064 KB |
Output is correct |
62 |
Correct |
168 ms |
22376 KB |
Output is correct |
63 |
Correct |
165 ms |
22444 KB |
Output is correct |
64 |
Correct |
175 ms |
22344 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
42 ms |
6008 KB |
Output is correct |
67 |
Correct |
182 ms |
22536 KB |
Output is correct |
68 |
Correct |
175 ms |
22476 KB |
Output is correct |
69 |
Correct |
183 ms |
22472 KB |
Output is correct |
70 |
Correct |
1 ms |
300 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
44 ms |
6056 KB |
Output is correct |
74 |
Correct |
169 ms |
22412 KB |
Output is correct |
75 |
Correct |
175 ms |
22472 KB |
Output is correct |
76 |
Correct |
176 ms |
22364 KB |
Output is correct |
77 |
Correct |
179 ms |
22348 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
4 ms |
756 KB |
Output is correct |
81 |
Correct |
82 ms |
14100 KB |
Output is correct |
82 |
Correct |
1 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
212 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
44 ms |
6260 KB |
Output is correct |
86 |
Correct |
75 ms |
14004 KB |
Output is correct |
87 |
Correct |
74 ms |
14068 KB |
Output is correct |
88 |
Correct |
71 ms |
14028 KB |
Output is correct |
89 |
Correct |
72 ms |
14084 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |
91 |
Correct |
1 ms |
212 KB |
Output is correct |
92 |
Correct |
4 ms |
852 KB |
Output is correct |
93 |
Correct |
76 ms |
14112 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
0 ms |
212 KB |
Output is correct |
97 |
Correct |
19 ms |
3784 KB |
Output is correct |
98 |
Correct |
78 ms |
14072 KB |
Output is correct |
99 |
Correct |
71 ms |
14076 KB |
Output is correct |
100 |
Correct |
71 ms |
14072 KB |
Output is correct |
101 |
Correct |
73 ms |
14072 KB |
Output is correct |
102 |
Correct |
69 ms |
14072 KB |
Output is correct |
103 |
Correct |
80 ms |
14028 KB |
Output is correct |
104 |
Correct |
74 ms |
14072 KB |
Output is correct |
105 |
Correct |
87 ms |
14072 KB |
Output is correct |