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 maxn = 1e3 + 20;
int cnt[maxn][maxn];
vector<vector<int>> res;
bool flag[maxn];
int n;
void add_edge(int u, int v) {
res[u][v] = true;
res[v][u] = true;
}
bool solve(vector<int> comp) {
for (auto u: comp) {
for (auto v: comp) {
if (cnt[u][v] == 0) {
return false;
}
}
}
for (int i = 0; i < (int)comp.size() - 1; i++) {
add_edge(comp[i], comp[i + 1]);
}
for (auto i: comp) {
flag[i] = false;
}
vector<vector<int>> chains;
for (auto i: comp) {
if (!flag[i]) {
flag[i] = true;
vector<int> chain = {i};
int pt = 0;
int sz = 1;
while (pt < sz) {
int u = chain[pt++];
for (int v = 0; v < n; v++) {
if (!flag[v] && cnt[u][v] == 1) {
flag[v] = true;
chain.push_back(v);
sz++;
}
}
}
for (auto u: chain) {
for (auto v: chain) {
if (cnt[u][v] != 1) {
return false;
}
}
}
chains.push_back(chain);
}
}
int sz = chains.size();
if (sz == 2) {
return false;
}
if (sz >= 3) {
for (int i = 0; i < sz; i++) {
add_edge(chains[i].back(), chains[(i + 1) % sz].back());
}
}
return true;
}
int construct(vector<vector<int>> _cnt) {
n = _cnt.size();
res.resize(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cnt[i][j] = _cnt[i][j];
if (cnt[i][j] == 3) {
return 0;
}
}
}
for (int i = 0; i < n; i++) {
if (!flag[i]) {
flag[i] = true;
vector<int> comp = {i};
int pt = 0;
int sz = 1;
while (pt < sz) {
int u = comp[pt++];
for (int v = 0; v < n; v++) {
if (!flag[v] && cnt[u][v] > 0) {
flag[v] = true;
comp.push_back(v);
sz++;
}
}
}
if (!solve(comp)) {
return 0;
}
}
}
build(res);
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... |