This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>
#include "supertrees.h"
#define pb push_back
#define f first
#define s second
using namespace std;
//
//static int n;
//static std::vector<std::vector<int>> p;
//static std::vector<std::vector<int>> b;
//static bool called = false;
//
//int construct(std::vector<std::vector<int>>);
//
//static void _assert(bool cond, const char* error) {
//    if (!cond) {
//        printf("%s\n", error);
//        exit(0);
//    }
//}
//
//void build(std::vector<std::vector<int>> _b) {
//    _assert(!called, "build is called more than once");
//    called = true;
//    _assert((int) _b.size() == n, "Invalid number of rows in b");
//    for (int i = 0;  i < n;  i++)
//        _assert((int) _b[i].size() == n, "Invalid number of columns in b");
//    b = _b;
//}
//
//int main() {
//    assert(scanf("%d", &n) == 1);
//
//    p.resize(n);
//    for (int i = 0;  i < n;  i++)
//        p[i].resize(n);
//
//    for (int i = 0;  i < n;  i++)
//        for (int j = 0;  j < n;  j++)
//            assert(scanf("%d", &p[i][j]) == 1);
//    fclose(stdin);
//
//    int possible = construct(p);
//
//    _assert(possible == 0 || possible == 1, "Invalid return value of construct");
//    if (possible == 1)
//        _assert(called, "construct returned 1 without calling build");
//    else
//        _assert(!called, "construct called build but returned 0");
//
//    printf("%d\n", possible);
//    if (possible == 1)
//        for (int i = 0; i < n; i++) {
//            for (int j = 0; j < n; j++) {
//                if (j)
//                    printf(" ");
//                printf("%d", b[i][j]);
//            }
//            printf("\n");
//        }
//}
//
//
//
//
//
//
//
//
//
//
//
const int N = 1501;
vector <vector <int> > ans;
int mrk[N];
int kol[N][N];
int n1;
void Rec(int st, int x)
{
    kol[st][x]++;
    mrk[x] = 1;
    for (int i = 0; i < n1; i++)
        if (!mrk[i] && ans[x][i]) Rec(st, i);
    mrk[x] = 0;
}
int hsh(vector <int> vc)
{
    int sm = 0;
    for (int i = 0, p = 1; i < vc.size(); i++, p *= 3) sm += vc[i] * p;
    return sm;
}
int construct(std::vector<std::vector<int> > p) {
	int n = p.size();
    n1 = n;
	ans.resize(n);
    for (int i = 0; i < n; i++)	ans[i].resize(n);
    pair<int, int> vc[n];
    for (int i = 0; i < n; i++) vc[i] = {hsh(p[i]), i};
    sort(vc, vc + n);
    vector <pair<int, int> > vc1;
    vector <int> vc3(n);
    for (int i = 1; i < n; i++)
        if (vc[i].f != vc[i - 1].f)
        {
            for (int j = 0; j < n; j++) vc3[j] = (p[vc[i - 1].s][j] > 0);
            vc1.pb({hsh(vc3), vc[i - 1].s});
        }
        else
        {
            ans[vc[i - 1].s][vc[i].s] = 1;
            ans[vc[i].s][vc[i - 1].s] = 1;
        }
    for (int j = 0; j < n; j++) vc3[j] = (p[vc[n - 1].s][j] > 0);
    vc1.pb({hsh(vc3), vc[n - 1].s});
    sort(vc1.begin(), vc1.end());
    int ls = 0;
    for (int i = 1; i < vc1.size(); i++)
        if (vc1[i].f != vc1[i - 1].f)
    {
        if (ls != i - 1)
        {
            ans[vc1[i - 1].s][vc1[ls].s] = 1;
            ans[vc1[ls].s][vc1[i - 1].s] = 1;
            if (p[vc1[ls].s][vc1[i - 1].s] == 3)
            {
                if (i - ls >= 4)
                {
                    ans[vc1[i - 1].s][vc1[ls + 1].s] = 1;
                    ans[vc1[ls + 1].s][vc1[i - 1].s] = 1;
                }
                else return 0;
            }
        }
        ls = i;
    }
    else
    {
        ans[vc1[i].s][vc1[i - 1].s] = 1;
        ans[vc1[i - 1].s][vc1[i].s] = 1;
    }
    int m = vc1.size();
    if (ls != m - 1)
    {
        ans[vc1[m - 1].s][vc1[ls].s] = 1;
        ans[vc1[ls].s][vc1[m - 1].s] = 1;
        if (p[vc1[ls].s][vc1[m - 1].s] == 3)
        {
            if (m - ls >= 4)
            {
                ans[vc1[m - 1].s][vc1[ls + 1].s] = 1;
                ans[vc1[ls + 1].s][vc1[m - 1].s] = 1;
            }
            else return 0;
        }
    }
    for (int i = 0; i < n; i++) Rec(i, i);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (kol[i][j] != p[i][j]) return 0;
    build(ans);
	return 1;
}
/*
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
*/
Compilation message (stderr)
supertrees.cpp: In function 'int hsh(std::vector<int>)':
supertrees.cpp:112:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  112 |     for (int i = 0, p = 1; i < vc.size(); i++, p *= 3) sm += vc[i] * p;
      |                            ~~^~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:146:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |     for (int i = 1; i < vc1.size(); i++)
      |                     ~~^~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |