# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
303131 | qpwoeirut | Connecting Supertrees (IOI20_supertrees) | C++17 | 1 ms | 384 KiB |
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;
typedef vector<vector<int>> vvi;
typedef pair<int,int> pii;
const int MN = 1001;
int N;
int root[MN];
int par(int u) {
if (u == root[u]) return u;
root[u] = par(root[u]);
return root[u];
}
void join(int u, int v) {
int ru = par(u), rv = par(v);
if (ru == rv) return;
root[ru] = rv;
}
bool dsu(const vvi& p) {
for (int i=0; i<N; ++i) {
root[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]) join(i, j);
}
}
for (int i=0; i<N; ++i) {
for (int j=0; j<N; ++j) {
if (p[i][j] == 0 && par(i) == par(j)) return 0;
}
}
return 1;
}
int ones[MN];
vvi ans;
bool make_part(const vvi& p, const vector<int>& nodes) {
set<pii, greater<pii>> q;
for (int i=0; i<nodes.size(); ++i) {
q.insert(pii(ones[nodes[i]], nodes[i]));
}
int root = -1;
int last = -1;
while (q.size() > 0) {
int cur = q.begin()->second;
q.erase(q.begin());
if (root == -1) {
root = cur;
}
if (last != -1) {
ans[cur][last] = 1;
ans[last][cur] = 1;
}
last = cur;
for (int i=0; i<N; ++i) {
if (p[cur][i] == 1) {
if (ones[i] != 1) {
return false;
}
ans[cur][i] = 1;
ans[i][cur] = 1;
q.erase(pii(ones[i], i));
}
}
}
return 1;
}
int construct(std::vector<std::vector<int>> p) {
N = p.size();
if (!dsu(p)) return 0;
map<int, vector<int>> parts;
for (int i=0; i<N; ++i) {
parts[par(i)].push_back(i);
for (int j=0; j<N; ++j) {
if (p[i][j] == 1) ++ones[i];
}
}
ans = vvi(N, vector<int>(N));
for (auto it=parts.begin(); it!=parts.end(); ++it) {
if (!make_part(p, it->second)) return 0;
}
build(ans);
return 1;
}
Compilation message (stderr)
# | 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... |