#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector <ll> P, H;
vector <vector <int>> sol;
ll dsu_find(ll x){
if (P[x] == x)return x;
return (P[x] = dsu_find(P[x]));
}
void dsu_union(ll x, ll y){
x = dsu_find(x), y = dsu_find(y);
if (x == y)return;
if (H[x] > H[y])P[y] = x;
if (H[x] < H[y])P[x] = y;
if (H[x] == H[y]){
P[y] = x;
H[x]++;
}
}
bool cmp(ll a, ll b){
return (P[a] < P[b]);
}
bool solve(vector <ll> &v, vector <vector <int>> &p){
ll n = v.size();
for (auto x : v){
P[x] = x;
H[x] = 0;
}
for (int i = 0; i < n; i++){
for (int j = i+1; j < n; j++)if (p[v[i]][v[j]] == 1)dsu_union(v[i], v[j]);
}
for (int i = 0; i < n; i++)P[v[i]] = dsu_find(v[i]);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)if (p[v[i]][v[j]] != 1 && P[v[i]] == P[v[j]])return 0;
}
sort(v.begin(), v.end(), cmp);
ll current = 0, coun = 0;
for (int i = 1; i < n; i++){
if (P[v[i]] == P[v[i-1]]){
sol[v[i]][v[i-1]] = 1;
sol[v[i-1]][v[i]] = 1;
}
else {
sol[v[i]][v[current]] = 1;
sol[v[current]][v[i]] = 1;
current = i;
coun++;
}
}
if (coun == 1)return 0;
sol[v[current]][v[0]] = 1;
sol[v[0]][v[current]] = 1;
return 1;
}
/*void build(vector <vector <int>> b){
/*for (int i = 0; i < b.size(); i++){
cerr << endl;
for (int j = 0; j < b.size(); j++){
cerr << b[i][j] << " ";
}
}
return;
}*/
int construct(vector <vector <int>> p){
ll n = p.size();
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)if (p[i][j] == 3)return 0;
}
P.resize(n);
H.resize(n, 0);
for (int i = 0; i < n; i++)P[i] = i;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)if (p[i][j] > 0)dsu_union(i, j);
}
for (int i = 0; i < n; i++)P[i] = dsu_find(i);
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)if (p[i][j] == 0 && P[i] == P[j])return 0;
}
vector <pair<ll, ll>> t(n);
for (int i = 0; i < n; i++)t[i] = {P[i], i};
sort(t.begin(), t.end());
sol.resize(n, vector <int> (n, 0));
for (int i = 0; i < n; i++){
P[i] = i;
H[i] = 0;
}
vector <ll> v;
for (int i = 0; i < n; i++){
if (i > 0 && t[i].first != t[i-1].first){
if (!solve(v, p))return 0;
v.clear();
}
v.push_back(t[i].second);
}
if (!solve(v, p))return 0;
for (int i = 0; i < n; i++)sol[i][i] = 0;
build(sol);
return 1;
}
/*int main(){
int n; cin >> n;
vector <vector <int>> p(n, vector <int> (n));
for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)cin >> p[i][j];
cout << construct(p);
}*/
/*
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
*/
Compilation message
supertrees.cpp:65:5: warning: "/*" within comment [-Wcomment]
65 | /*for (int i = 0; i < b.size(); i++){
|
# |
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 |
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 |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
173 ms |
22072 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 |
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 |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
173 ms |
22072 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1192 KB |
Output is correct |
13 |
Correct |
151 ms |
22032 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 |
596 KB |
Output is correct |
17 |
Correct |
71 ms |
8200 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
38 ms |
5724 KB |
Output is correct |
21 |
Correct |
156 ms |
22032 KB |
Output is correct |
22 |
Correct |
152 ms |
22028 KB |
Output is correct |
23 |
Correct |
160 ms |
22032 KB |
Output is correct |
24 |
Correct |
153 ms |
21956 KB |
Output is correct |
25 |
Correct |
60 ms |
8196 KB |
Output is correct |
26 |
Correct |
60 ms |
8140 KB |
Output is correct |
27 |
Correct |
161 ms |
22020 KB |
Output is correct |
28 |
Correct |
152 ms |
22028 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 |
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 |
1228 KB |
Output is correct |
9 |
Correct |
151 ms |
23924 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1236 KB |
Output is correct |
13 |
Correct |
156 ms |
23944 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
71 ms |
10232 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
304 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
43 ms |
6224 KB |
Output is correct |
22 |
Correct |
153 ms |
23920 KB |
Output is correct |
23 |
Correct |
153 ms |
23936 KB |
Output is correct |
24 |
Correct |
165 ms |
24008 KB |
Output is correct |
25 |
Correct |
63 ms |
14064 KB |
Output is correct |
26 |
Correct |
61 ms |
10060 KB |
Output is correct |
27 |
Correct |
151 ms |
23936 KB |
Output is correct |
28 |
Correct |
160 ms |
23920 KB |
Output is correct |
29 |
Correct |
64 ms |
14068 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 |
212 KB |
Output is correct |
4 |
Correct |
42 ms |
5872 KB |
Output is correct |
5 |
Correct |
154 ms |
22044 KB |
Output is correct |
6 |
Correct |
151 ms |
22000 KB |
Output is correct |
7 |
Correct |
165 ms |
22220 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
40 ms |
5760 KB |
Output is correct |
10 |
Correct |
151 ms |
22004 KB |
Output is correct |
11 |
Correct |
155 ms |
21968 KB |
Output is correct |
12 |
Correct |
159 ms |
22020 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
38 ms |
5752 KB |
Output is correct |
17 |
Correct |
153 ms |
22020 KB |
Output is correct |
18 |
Correct |
152 ms |
22020 KB |
Output is correct |
19 |
Correct |
157 ms |
22148 KB |
Output is correct |
20 |
Correct |
150 ms |
22092 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 |
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 |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
173 ms |
22072 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1192 KB |
Output is correct |
13 |
Correct |
151 ms |
22032 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 |
596 KB |
Output is correct |
17 |
Correct |
71 ms |
8200 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
38 ms |
5724 KB |
Output is correct |
21 |
Correct |
156 ms |
22032 KB |
Output is correct |
22 |
Correct |
152 ms |
22028 KB |
Output is correct |
23 |
Correct |
160 ms |
22032 KB |
Output is correct |
24 |
Correct |
153 ms |
21956 KB |
Output is correct |
25 |
Correct |
60 ms |
8196 KB |
Output is correct |
26 |
Correct |
60 ms |
8140 KB |
Output is correct |
27 |
Correct |
161 ms |
22020 KB |
Output is correct |
28 |
Correct |
152 ms |
22028 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 |
1228 KB |
Output is correct |
37 |
Correct |
151 ms |
23924 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1236 KB |
Output is correct |
41 |
Correct |
156 ms |
23944 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
71 ms |
10232 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
304 KB |
Output is correct |
48 |
Correct |
1 ms |
300 KB |
Output is correct |
49 |
Correct |
43 ms |
6224 KB |
Output is correct |
50 |
Correct |
153 ms |
23920 KB |
Output is correct |
51 |
Correct |
153 ms |
23936 KB |
Output is correct |
52 |
Correct |
165 ms |
24008 KB |
Output is correct |
53 |
Correct |
63 ms |
14064 KB |
Output is correct |
54 |
Correct |
61 ms |
10060 KB |
Output is correct |
55 |
Correct |
151 ms |
23936 KB |
Output is correct |
56 |
Correct |
160 ms |
23920 KB |
Output is correct |
57 |
Correct |
64 ms |
14068 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
3 ms |
596 KB |
Output is correct |
61 |
Correct |
69 ms |
10108 KB |
Output is correct |
62 |
Correct |
1 ms |
296 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
41 ms |
6280 KB |
Output is correct |
66 |
Correct |
61 ms |
10100 KB |
Output is correct |
67 |
Correct |
62 ms |
10096 KB |
Output is correct |
68 |
Correct |
62 ms |
10112 KB |
Output is correct |
69 |
Correct |
63 ms |
14068 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 |
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 |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
173 ms |
22072 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1192 KB |
Output is correct |
13 |
Correct |
151 ms |
22032 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 |
596 KB |
Output is correct |
17 |
Correct |
71 ms |
8200 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
38 ms |
5724 KB |
Output is correct |
21 |
Correct |
156 ms |
22032 KB |
Output is correct |
22 |
Correct |
152 ms |
22028 KB |
Output is correct |
23 |
Correct |
160 ms |
22032 KB |
Output is correct |
24 |
Correct |
153 ms |
21956 KB |
Output is correct |
25 |
Correct |
60 ms |
8196 KB |
Output is correct |
26 |
Correct |
60 ms |
8140 KB |
Output is correct |
27 |
Correct |
161 ms |
22020 KB |
Output is correct |
28 |
Correct |
152 ms |
22028 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 |
1228 KB |
Output is correct |
37 |
Correct |
151 ms |
23924 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1236 KB |
Output is correct |
41 |
Correct |
156 ms |
23944 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
71 ms |
10232 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
304 KB |
Output is correct |
48 |
Correct |
1 ms |
300 KB |
Output is correct |
49 |
Correct |
43 ms |
6224 KB |
Output is correct |
50 |
Correct |
153 ms |
23920 KB |
Output is correct |
51 |
Correct |
153 ms |
23936 KB |
Output is correct |
52 |
Correct |
165 ms |
24008 KB |
Output is correct |
53 |
Correct |
63 ms |
14064 KB |
Output is correct |
54 |
Correct |
61 ms |
10060 KB |
Output is correct |
55 |
Correct |
151 ms |
23936 KB |
Output is correct |
56 |
Correct |
160 ms |
23920 KB |
Output is correct |
57 |
Correct |
64 ms |
14068 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 |
42 ms |
5872 KB |
Output is correct |
62 |
Correct |
154 ms |
22044 KB |
Output is correct |
63 |
Correct |
151 ms |
22000 KB |
Output is correct |
64 |
Correct |
165 ms |
22220 KB |
Output is correct |
65 |
Correct |
0 ms |
212 KB |
Output is correct |
66 |
Correct |
40 ms |
5760 KB |
Output is correct |
67 |
Correct |
151 ms |
22004 KB |
Output is correct |
68 |
Correct |
155 ms |
21968 KB |
Output is correct |
69 |
Correct |
159 ms |
22020 KB |
Output is correct |
70 |
Correct |
1 ms |
212 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
38 ms |
5752 KB |
Output is correct |
74 |
Correct |
153 ms |
22020 KB |
Output is correct |
75 |
Correct |
152 ms |
22020 KB |
Output is correct |
76 |
Correct |
157 ms |
22148 KB |
Output is correct |
77 |
Correct |
150 ms |
22092 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
3 ms |
596 KB |
Output is correct |
81 |
Correct |
69 ms |
10108 KB |
Output is correct |
82 |
Correct |
1 ms |
296 KB |
Output is correct |
83 |
Correct |
0 ms |
212 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
41 ms |
6280 KB |
Output is correct |
86 |
Correct |
61 ms |
10100 KB |
Output is correct |
87 |
Correct |
62 ms |
10096 KB |
Output is correct |
88 |
Correct |
62 ms |
10112 KB |
Output is correct |
89 |
Correct |
63 ms |
14068 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 |
596 KB |
Output is correct |
93 |
Correct |
62 ms |
10100 KB |
Output is correct |
94 |
Correct |
0 ms |
300 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
0 ms |
304 KB |
Output is correct |
97 |
Correct |
15 ms |
2680 KB |
Output is correct |
98 |
Correct |
59 ms |
10100 KB |
Output is correct |
99 |
Correct |
63 ms |
10060 KB |
Output is correct |
100 |
Correct |
66 ms |
10112 KB |
Output is correct |
101 |
Correct |
59 ms |
10108 KB |
Output is correct |
102 |
Correct |
59 ms |
10100 KB |
Output is correct |
103 |
Correct |
59 ms |
10112 KB |
Output is correct |
104 |
Correct |
59 ms |
10096 KB |
Output is correct |
105 |
Correct |
64 ms |
10060 KB |
Output is correct |