# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
576149 | Vanilla | Comparing Plants (IOI20_plants) | C++17 | 0 ms | 0 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 <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
const int maxn = 1002;
int dsu[3][maxn];
set <int> comp [3][maxn];
int get_dad (int x, int d[]) {
return d[x] = (x == d[x] ? x: get_dad(d[x], d));
}
void merge (int a, int b, int d[]){
a = get_dad(a, d);
b = get_dad(b, d);
d[b] = a;
}
int construct(vector<vector<int>> p) {
int n = p.size();
vector <vector <int> > b (n, vector <int> (n));
for (int i = 0; i < n; i++){
dsu[0][i] = dsu[1][i] = dsu[2][i] = i;
comp[0][i].insert(i);
comp[1][i].insert(i);
}
for (int i = 0; i < n; i++){
for (int j = 0; j < i; j++){
merge(i,j, dsu[p[i][j]]);
}
}
for (int i = 0; i < n; i++){
for (int j = 0; j < i; j++){
if (p[i][j] == 0 && (get_dad(i, dsu[1]) == get_dad(j, dsu[1]) || get_dad(i, dsu[2]) == get_dad(j, dsu[2]))) {
// cout << i << " " << j << " " << get_dad(i, dsu[1]) << " " << get_dad(j, dsu[1]) << "\n";
return 0;
}
if (p[i][j] == 1) comp[1][get_dad(i, dsu[1])].insert(j);
if (p[i][j] == 2) comp[2][get_dad(i, dsu[2])].insert(j);
// if (get_dad(i, dsu[1]) == get_dad(j, dsu[1]) && get_dad(i, dsu[2]) == get_dad(j, dsu[2])) return 0;
}
}
for (int i = 0; i < n; i++){
if (comp[1][i].size() != 1) {
// cout << "1 " << i << ": ";
vector <int> v;
for (int j: comp[1][i]) {
// cout << j << " ";
v.push_back(j);
}
// cout << "\n";
for (int j = 0; j < v.size() - 1; j++){
b[v[j]][v[j + 1]] = b[v[j + 1]][v[j]] = 1;
}
}
if (comp[2][i].size() != 1){
// cout << "2 " << i << ": ";
vector <int> v;
for (int j: comp[2][i]) {
// cout << j << " ";
v.push_back(j);
}
// cout << "\n";
for (int j = 0; j < v.size(); j++){
b[v[j]][v[(j + 1) % v.size()]] = b[v[(j + 1) % v.size()]][v[j]] = 1;
}
}
}
// for (int i = 0; i < n; i++){
// for (int j = 0; j <i; j++){
// if (b[i][j]) {
// cout << j << " " << i << "\n";
// }
// }
// }
build(b);
return 1;
}