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 "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1003;
vector<int> adj[2][N];
struct DisjointSetUnion {
vector<int> con[N];
int par[N];
DisjointSetUnion() {
for (int i = 0; i < N; i++)
par[i] = i, con[i] = {i};
}
inline int find(int x) {
if (x != par[x])
par[x] = find(par[x]);
return par[x];
}
void join(int x, int y) {
x = find(x); y = find(y);
if (x == y) return;
if (con[x].size() < con[y].size()) swap(x, y);
par[y] = x;
for (int i: con[y])
con[x].emplace_back(i);
con[y].clear();
con[y].shrink_to_fit();
}
} one, two, positive;
int construct(vector<vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] != p[j][i] || p[i][j] == 3) return 0;
if (!p[i][j]) continue;
adj[p[i][j] - 1][i].emplace_back(j);
positive.join(i, j);
}
}
for (int i = 0; i < n; i++)
for (int j: adj[0][i])
one.join(i, j);
for (int i = 0; i < n; i++)
for (int j: adj[1][i])
two.join(one.find(i), one.find(j));
vector<vector<int>> ans(n, vector<int>(n, 0));
auto add_edge = [&] (int x, int y) -> void {
ans[x][y] = ans[y][x] = 1;
};
for (int i = 0; i < n; i++) {
if (positive.find(i) == i) {
for (int x: positive.con[i])
for (int y: positive.con[i])
if (p[x][y] == 0)
return 0;
}
if (one.find(i) == i) {
for (int x: one.con[i])
for (int y: one.con[i])
if (p[x][y] != 1)
return 0;
for (int j = 0, o = one.con[i].size(); j < o - 1; j++)
add_edge(one.con[i][j], one.con[i][j + 1]);
}
if (two.find(i) == i && (int)two.con[i].size() > 1) {
if ((int)two.con[i].size() == 2) return 0;
for (int j = 0, o = two.con[i].size(); j < o - 1; j++)
add_edge(two.con[i][j], two.con[i][j + 1]);
add_edge(two.con[i][0], two.con[i].back());
}
}
build(ans);
return 1;
}
#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;
}
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);
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
# | 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... |