# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
568969 | Stickfish | 슈퍼트리 잇기 (IOI20_supertrees) | C++17 | 195 ms | 24028 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 <vector>
#include <iostream>
using namespace std;
struct dsu {
void resize(int n) {
par.resize(n);
sz.assign(n, 1);
for (int i = 0; i < n; ++i)
par[i] = i;
}
int gst(int i) {
if (par[i] == i)
return i;
return par[i] = gst(par[i]);
}
void unite(int i, int j) {
i = gst(i);
j = gst(j);
if (i == j)
return;
if (sz[i] < sz[j])
swap(i, j);
par[j] = i;
sz[i] += sz[j];
}
private:
vector<int> par;
vector<int> sz;
};
int construct(vector<vector<int>> p) {
int n = p.size();
dsu suall;
suall.resize(n);
dsu su1;
su1.resize(n);
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (p[i][j] > 0)
suall.unite(i, j);
if (p[i][j] == 1)
su1.unite(i, j);
if (p[i][j] == 3)
return 0;
}
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (p[i][j] == 0 && suall.gst(i) == suall.gst(j))
return 0;
if (p[i][j] == 2 && su1.gst(i) == su1.gst(j))
return 0;
}
}
//for (int i = 0; i < n; ++i)
//cout << suall.gst(i) << ' ';
//cout << endl;
//for (int i = 0; i < n; ++i)
//cout << su1.gst(i) << ' ';
//cout << endl;
vector<vector<int>> ans(n, vector<int>(n, 0));
for (int i = 0; i < n; ++i) {
if (suall.gst(i) == i) {
vector<int> comp;
for (int j = 0; j < n; ++j) {
if (suall.gst(j) == i)
comp.push_back(j);
}
vector<int> cycle;
for (int j : comp) {
if (su1.gst(j) == j)
cycle.push_back(j);
else
ans[j][su1.gst(j)] = ans[su1.gst(j)][j] = 1;
}
if (cycle.size() == 2) {
return 0;
}
if (cycle.size() > 1) {
for (int t = 0; t < cycle.size(); ++t) {
int v = cycle[t];
int u = cycle[(t + 1) % cycle.size()];
ans[u][v] = ans[v][u] = 1;
}
}
}
}
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... |