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>
using namespace std;
//#define HOME
#ifndef HOME
#include "supertrees.h"
#endif // HOME
#ifdef HOME
static int n;
static vector<vector<int>> p;
static vector<vector<int>> b;
static bool called = false;
static void check(bool cond, 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 // HOME
/**
4
1 1 2 2
1 1 2 2
2 2 1 2
2 2 2 1
**/
const int max_n = 1000;
int father[1 + max_n];
int find_father (int x) {
if (father[x] == x)
return x;
return father[x] = find_father (father[x]);
}
vector <vector <int>> answer;
void connect (int x, int y, bool add) {
x = find_father (x);
y = find_father (y);
if (x != y) {
father[x] = y;
if (add)
answer[x][y] = answer[y][x] = 1;
}
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
answer.resize (n);
for (int i = 0; i < n; i++)
answer[i].resize (n);
for (int i = 0; i < n; i++)
father[i] = i;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (p[i][j] == 3)
return 0;
if (p[i][j] == 1)
connect (i, j, true);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (find_father (i) == find_father (j) && p[i][j] != 1)
return 0;
vector <bool> is_root (n);
for (int i = 0; i < n; i++)
if (find_father (i) == i)
is_root[i] = true;
for (int i = 0; i < n; i++)
father[i] = i;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (is_root[i] && is_root[j] && p[i][j] == 2)
connect (i, j, false);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j && find_father (i) == find_father (j) && p[i][j] != 2)
return 0;
for (int i = 0; i < n; i++) {
if (find_father (i) == i) {
vector <int> v;
for (int j = 0; j < n; j++)
if (find_father (j) == i)
v.push_back (j);
if (v.size () == 2) return 0;
if (v.size () >= 3) {
for (int j = 1; j < v.size (); j++)
answer[v[j - 1]][v[j]] = answer[v[j]][v[j - 1]] = 1;
answer[v[0]][v.back ()] = answer[v.back ()][v[0]] = 1;
}
}
}
build(answer);
return 1;
}
#ifdef HOME
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 // HOME
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:110:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int j = 1; j < v.size (); j++)
| ~~^~~~~~~~~~~
# | 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... |