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 <iostream>
#include <vector>
using std::vector;
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
vector<int> group(n, -1);
vector<int> component(n, -1);
int max_comp = 0;
int cur_comp = -1;
int cur_group = -1;
int max_group = 0;
for (int i = 0; i < n; ++i) {
if (group[i] == -1) {
group[i] = max_group;
max_group += 1;
}
if (component[i] == -1) {
component[i] = max_comp;
max_comp += 1;
}
cur_comp = component[i];
cur_group = group[i];
for (int j = 0; j < n; ++j) {
if (p[i][j] != p[j][i] || (i == j && p[i][j] != 1)) {
return 0;
}
if (p[i][j] == 1) {
if (group[j] == -1) {
group[j] = cur_group;
}
else if (group[j] != cur_group) {
return 0;
}
}
else {
if (group[j] == -1) {
group[j] = max_group;
++max_group;
}
else if (group[j] == cur_group) {
return 0;
}
}
if (p[i][j] != 0) {
if (component[j] == -1) {
component[j] = cur_comp;
}
else if (component[j] != cur_comp) {
return 0;
}
}
else {
if (component[j] == -1) {
component[j] = max_comp;
++max_comp;
}
else if (component[j] == cur_comp) {
return 0;
}
}
}
}
vector<int> first_of_group(max_group, -1);
vector<int> group_component(max_group, -1);
for (int i = 0; i < n; ++i) {
if (first_of_group[group[i]] == -1) {
first_of_group[group[i]] = i;
//std::cerr << "first_of_group[" << group[i] << "] = " << i << std::endl;
}
}
for (int i = 0; i < max_group; ++i) {
group_component[i] = component[first_of_group[i]];
//std::cerr << "group_component[" << i << "] = " << group_component[i] << std::endl;
}
std::vector<std::vector<int>> answer;
for (int i = 0; i < n; i++) {
std::vector<int> row(n, 0);
answer.push_back(row);
}
for (int i = 0; i < n; ++i) {
for (int j = i+1; j < n; ++j) {
if (group[j] == group[i]) {
answer[i][j] = 1;
break;
}
}
for (int j = i-1; j >= 0; --j) {
if (group[j] == group[i]) {
answer[i][j] = 1;
break;
}
}
if (first_of_group[group[i]] == i) {
int mygroup = group[i];
int mycomp = component[i];
for (int j = 1; j < max_group; ++j) {
int next_group = (mygroup + j) % max_group;
if (group_component[next_group] == mycomp) {
answer[i][first_of_group[next_group]] = 1;
answer[first_of_group[next_group]][i] = 1;
break;
}
}
for (int j = 1; j < max_group; ++j) {
int prev_group = (mygroup - j + max_group) % max_group;
if (group_component[prev_group] == mycomp) {
answer[i][first_of_group[prev_group]] = 1;
answer[first_of_group[prev_group]][i] = 1;
break;
}
}
}
}
build(answer);
return 1;
}
# | 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... |