#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
int construct(vector<vector<int>> p) {
int n = p.size();
vector<int> grp(n);
for (int i = 0; i < n; i++) {
grp[i] = i;
}
auto fnd = [&] (auto &&self, int a) {
if (grp[a] == a) {
return a;
} else {
return grp[a] = self(self, grp[a]);
}
};
vector<vector<int>> edge(n, vector(n, 0));
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i][j] == 1) {
int x = fnd(fnd, i), y = fnd(fnd, j);
if (x != y) {
edge[i][j] = edge[j][i] = 1;
grp[x] = y;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] == 0 && fnd(fnd, i) == fnd(fnd, j)) {
return 0;
}
}
}
build(edge);
return 1;
}