This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef LOCAL
#include <vector>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <string>
static int n;
static std::vector<std::vector<int>> p;
static std::vector<std::vector<int>> b;
static bool called = false;
static void check(bool cond, std::string message) {
if (!cond) {
printf("%s\n", message.c_str());
fclose(stdout);
exit(0);
}
}
void build(std::vector<std::vector<int>> _b) {
check(!called, "build is called more than once");
called = true;
check((int)_b.size() == n, "Invalid number of rows in b");
for (int i = 0; i < n; i++) {
check((int)_b[i].size() == n, "Invalid number of columns in b");
}
b = _b;
}
#endif // LOCAL
#ifndef LOCAl
#include "supertrees.h"
#endif // LOCAl
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
int construct(std::vector<std::vector<int>> p) {
int N;
vector<vector<int>> ans;
vector<int> lab;
N = p.size();
ans = vector<vector<int>>(N, vector<int>(N));
lab = vector<int>(N, -1);
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (p[i][j] == 3) {
return 0;
}
}
}
auto addedge = [&](int u, int v) {
ans[u][v] = ans[v][u] = 1;
};
auto init = [&](void) {
lab.assign(N, -1);
};
function<int(int)>findset = [&](int u) {
if (lab[u] < 0) return u;
return lab[u] = findset(lab[u]);
};
function<bool(int, int)>same = [&](int u, int v) {
return findset(u) == findset(v);
};
function<void(int, int)>mergeset = [&](int u, int v) {
u = findset(u); v = findset(v);
if (u == v) return;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return;
};
init();
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (p[i][j] == 0 && same(i, j)) {
return 0;
}
if (p[i][j])
mergeset(i, j);
}
}
init();
vector<bool> used(N, false), ok(N, true);
/// used is have cycle go through v
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
if (p[i][j] == 1) ok[i] = false;
}
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < i; ++j) {
if (p[i][j] == 1 && !same(i, j)) {
addedge(i, j);
mergeset(i, j);
}
}
if (!ok[i]) continue;
if (used[i]) continue;
queue<int> q; q.push(i);
used[i] = true;
vector<int> cycle;
while (q.size()) {
int u = q.front(); q.pop();
cycle.eb(u);
for (int v = 0; v < N; ++v) {
if (p[u][v] == 2 && !used[v] && ok[v]) {
used[v] = true;
q.push(v);
}
}
}
if (cycle.size() < 3)
return 0;
int n = cycle.size();
for (int i = 0; i < n; ++i) {
addedge(cycle[i], cycle[(i + 1) % n]);
mergeset(cycle[i], cycle[(i + 1) % n]);
}
}
build(ans);
return 1;
}
#ifdef LOCAL
int main() {
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
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);
check(possible == 0 || possible == 1, "Invalid return value of construct");
if (possible == 1) {
check(called, "construct returned 1 without calling build");
} else {
check(!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");
}
}
fclose(stdout);
}
#endif // LOCAL
# | 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... |