# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
405974 | tiberiu | Connecting Supertrees (IOI20_supertrees) | C++14 | 322 ms | 24132 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 <iostream>
#include <vector>
#include "supertrees.h"
using namespace std;
const int MAX_N = 1000;
int UF[MAX_N];
int root(int x) {
return UF[x] == -1 ? x : UF[x] = root(UF[x]);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x != y)
UF[x] = y;
}
vector<vector<int>> b;
bool solve(const vector<vector<int>>& p, vector<int> c) {
int N = p.size();
for (int i : c)
for (int j : c)
if (p[i][j] == 1)
unite(i, j);
for (int i : c)
for (int j : c)
if (p[i][j] != 1 + (root(i) != root(j)))
return 0;
vector<int> cycle;
for (int i : c)
if (UF[i] != -1)
b[i][UF[i]] = b[UF[i]][i] = 1;
else
cycle.push_back(i);
if (cycle.size() == 2)
return 0;
if (cycle.size() > 1) {
cycle.push_back(cycle[0]);
for (int i = 1; i < cycle.size(); i++)
b[cycle[i]][cycle[i-1]] = b[cycle[i-1]][cycle[i]] = 1;
}
return 1;
}
int construct(vector<vector<int>> p) {
int N = p.size();
for (int i = 0; i < N; i++)
UF[i] = -1;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (p[i][j] == 3)
return 0;
else if (p[i][j])
unite(i, j);
vector<vector<int>> comps;
for (int i = 0; i < N; i++)
if (UF[i] == -1) {
vector<int> c;
for (int j = 0; j < N; j++)
if (root(j) == i)
c.push_back(j);
comps.push_back(c);
}
b.resize(N);
for (int i = 0; i < N; i++) {
UF[i] = -1;
b[i].resize(N);
}
for (int i = 0; i < comps.size(); i++)
if (!solve(p, comps[i]))
return 0;
build(b);
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... |