# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
823616 | GusterGoose27 | Connecting Supertrees (IOI20_supertrees) | C++17 | 198 ms | 24068 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>
const int MAXN = 1e3+5;
using namespace std;
int n;
int uf[MAXN];
int find(int i) {
return uf[i] == i ? i : uf[i] = find(uf[i]);
}
void merge(int a, int b) {
a = find(a);
b = find(b);
assert(a != b);
uf[a] = b;
}
bool vis[MAXN];
int construct(vector<vector<int>> p) {
n = p.size();
iota(uf, uf+n, 0);
vector<vector<int>> answer(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] == 3) return 0;
if (find(i) != find(j) && p[i][j] == 1) {
answer[i][j] = answer[j][i] = 1;
merge(i, j);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] == 1 && find(i) != find(j)) return 0;
if (p[i][j] != 1 && find(i) == find(j)) return 0;
}
}
// all 1 connections are dealt with
for (int i = 0; i < n; i++) {
vector<int> vals;
int cur = find(i);
if (vis[cur]) continue;
do {
if (vals.size()) merge(vals.back(), cur);
vals.push_back(cur);
vis[cur] = 1;
bool f = 0;
for (int j = 0; j < n; j++) {
if (uf[j] != j || vis[j] || j == find(i)) continue;
if (p[cur][j] == 2) {
f = 1;
cur = j;
break;
}
}
if (!f) cur = find(i);
} while (cur != find(i));
if (vals.size() == 2) return 0;
if (vals.size() > 1) {
for (int j = 0; j < vals.size(); j++) {
answer[vals[j]][vals[(j+1)%vals.size()]] =
answer[vals[(j+1)%vals.size()]][vals[j]] = 1;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] >= 1 && find(i) != find(j)) return 0;
if (p[i][j] == 0 && find(i) == find(j)) return 0;
}
}
build(answer);
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... |