# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
304871 | daveeed | Connecting Supertrees (IOI20_supertrees) | C++17 | 264 ms | 22264 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>
const int MAXN = 1000;
int parent[MAXN], sz[MAXN], comp[MAXN], c;
int find(int p){
while (p != parent[p])
p = parent[p];
return p;
}
void merge(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
if (sz[rootP] < sz[rootQ]) {
parent[rootP] = rootQ;
sz[rootQ] += sz[rootP];
}
else {
parent[rootQ] = rootP;
sz[rootP] += sz[rootQ];
}
}
int construct(std::vector<std::vector<int> > p) {
int n = p.size();
std::vector<std::vector<int> > answer;
for (int i = 0; i < n; i++) {
std::vector<int> row;
row.resize(n);
answer.push_back(row);
}
for(int i = 0; i < n; i++){
parent[i] = i;
}
bool isOne = false;
bool isTwo = false;
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(p[i][j])
merge(i, j);
if(p[i][j] == 1)
isOne = true;
if(p[i][j] == 2)
isTwo = true;
}
}
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(find(i) == find(j) && !p[i][j])
return 0;
}
}
if(isOne && isTwo){
for(int i = 0; i < n; i++){
int r = find(i);
if(r == i){
std::vector<int> inComp;
std::vector<int> inCircle;
for(int j = 0; j < n; j++){
if(find(j) == r){
inComp.push_back(j);
}
}
int start = -1;
int prev = -1;
for(int j = 0; j < inComp.size(); j++) if(comp[inComp[j]] == 0){
comp[inComp[j]] = ++c;
for(int k = j + 1; k < inComp.size(); k++) if(comp[inComp[k]] == 0){
if(p[inComp[j]][inComp[k]] == 1){
comp[inComp[k]] = comp[inComp[j]];
answer[inComp[j]][inComp[k]] = 1;
answer[inComp[k]][inComp[j]] = 1;
}
}
if(start == -1){
start = inComp[j];
prev = inComp[j];
}
else{
answer[prev][inComp[j]] = 1;
answer[inComp[j]][prev] = 1;
prev = inComp[j];
}
}
answer[prev][start] = 1;
answer[start][prev] = 1;
if(c == 2)
return 0;
}
}
}
else if(isOne){
for(int i = 0; i < n; i++){
int r = find(i);
if(r != i){
answer[r][i] = 1;
answer[i][r] = 1;
}
}
}
else if(isTwo){
for(int i = 0; i < n; i++){
int r = find(i);
if(r == i){
int prev = -1;
int count = 0;
int start = -1;
for(int j = 0; j < n; j++){
if(find(j) == r){
count++;
if(prev == -1){
prev = j;
start = j;
}
else{
answer[prev][j] = 1;
answer[j][prev] = 1;
prev = j;
}
}
}
if(count == 1){
continue;
}
if(count == 2){
return 0;
}
answer[prev][start] = 1;
answer[start][prev] = 1;
}
}
}
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... |