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 <vector>
#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
using ll = long long;
#ifdef KEV
#define DE(i, e) cerr << #i << ' ' << i << e
void debug(auto L, auto R) { while (L < R) cerr << *L << " \n"[L+1==R], ++L; }
#else
#define DE(...) 0
void debug(...) {}
#endif
const int maxn = 300010;
int g[maxn], sz[maxn], n, cnt[10];
int find(int i) { return g[i] == i ? g[i] : g[i] = find(g[i]); }
std::vector<std::vector<int>> p;
bool merge(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if ( sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
return g[b] = a, true;
}
void dsuinit() {
for (int i = 0;i < n;++i)
g[i] = i, sz[i] = 1;
}
bool build_ok(vector<vector<int>> &edge) {
dsuinit();
if (!cnt[2]) {
for (int i = 0;i < n;++i)
for (int j = i+1;j < n;++j)
if (p[i][j] && merge(i, j))
edge[i][j] = edge[j][i] = true;
for (int i = 0;i < n;++i)
for (int j = i+1;j < n;++j)
if (!p[i][j] && find(i) == find(j))
return false;
return true;
}
if (!cnt[1] && !cnt[3]) {
for (int i = 0;i < n;++i)
for (int j = i+1;j < n;++j)
if (p[i][j]) merge(i, j);
for (int i = 0;i < n;++i)
for (int j = i+1;j < n;++j)
if (!p[i][j] && find(i) == find(j))
return false;
vector<vector<int>> child(n);
for (int i = 0;i < n;++i)
child[ find(i) ].pb(i);
for (int i = 0;i < n;++i) if (child[i].size() == 2)
return false;
for (int i = 0;i < n;++i) if (child[i].size() > 2) {
int lst = child[i].back();
for (int u : child[i])
edge[lst][u] = edge[u][lst] = true;
}
return true;
}
assert(false);
}
int construct(std::vector<std::vector<int>> p) {
n = p.size();
::p = p;
std::vector<std::vector<int>> answer;
for (int i = 0; i < n; i++) {
std::vector<int> row(n);
answer.push_back(row);
for (int j = i+1;j < n;++j)
++cnt[ p[i][j] ];
}
if (!build_ok(answer))
return 0;
build(answer);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:76:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
76 | if (!build_ok(answer))
| ^~
supertrees.cpp:78:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
78 | build(answer);
| ^~~~~| # | 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... |