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>
using namespace std;
struct DSU{
vector<int> v;
vector<int> v2;
void init(int n){
v.assign(n, -1);
}
int get(int x){return v[x] < 0 ? x : v[x] = get(v[x]);}
int get2(int x){return v2[x] < 0 ? x : v2[x] = get(v2[x]);}
void unite(int x, int y){
x = get(x); y = get(y);
if(x == y) return;
if(x > y) swap(x, y);
v[x] += v[y]; v[y] = x;
return;
}
void unite2(int x, int y){
x = get2(x); y = get2(y);
if(x == y) return;
if(x > y) swap(x, y);
v2[x] += v2[y]; v2[y] = x;
return;
}
bool check(vector<vector<int>>& p, vector<vector<int>>& ans){
for(int k = 0; k < v.size(); k++){
if(v[k] < 0){
vector<int> c;
c.push_back(k);
for(int j = 0; j < v.size(); j++){
if(v[j] == k) c.push_back(j);
}
bool global_pos = 0;
for(int i : c){
if(global_pos) break;
bool pos = 1;
vector<int> three;
vector<int> two;
vector<int> one;
for(int j : c){
if(j == i) continue;
if(p[i][j] == 3) three.push_back(j);
else if(p[i][j] == 2) two.push_back(j);
else if(p[i][j] == 1) one.push_back(j);
else pos = 0;
}
if((three.size() > 0 && three.size() < 3) || (two.size() > 0 && two.size() < 2) || (two.size() > 0 && three.size() > 0)){
pos = 0;
}
for(int q : three){
for(int j : three){
if(q == j) continue;
if(p[q][j] != 3){
pos = 0;
}
}
for(int j : one){
if(p[q][j] != 3){
pos = 0;
}
}
}
v2.assign(two.size(), -1);
vector<vector<int>> norm;
for(int q : two){
for(int j : two){
if(q == j) continue;
if(p[q][j] == 1){
unite2(q, j);
}else if(p[q][j] == 0) pos = 0;
}
for(int j : one){
if(p[q][j] != 2){
pos = 0;
}
}
}
for(int j = 0; j < v2.size(); j++){
if(v2[j] < 0){
//Reviso que todo el cc se puede y añado j a norm.
//No puedo juntarlo todavía tengo que guardarme los vectores.
vector<int> curr = {two[j]};
for(int q = 0; q < v2.size(); q++){
if(v2[q] == j){
curr.push_back(two[q]);
}
}
for(int x : curr){
for(int y : curr){
if(p[x][y] != 1) pos = 0;
}
}
norm.push_back(curr);
}
}
if(norm.size() == 1) pos = 0;
for(int q : one){
for(int j : one){
if(q == j) continue;
if(p[q][j] != 1){
pos = 0;
}
}
}
if(pos){
global_pos = 1;
if(three.size() > 0){
ans[i][three[three.size() - 1]] = ans[i][three[three.size() - 2]] = 1;
ans[three[three.size() - 1]][i] = ans[three[three.size() - 2]][i] = 1;
ans[three[0]][i] = ans[i][three[0]] = 1;
for(int j = 0; j < three.size() - 1; j++){
ans[three[j]][three[j+1]] = ans[three[j+1]][three[j]] = 1;
}
}
if(two.size() > 0){
ans[i][norm[0][0]] = ans[norm[0][0]][i] = 1;
ans[i][norm[norm.size() - 1][0]] = ans[norm[norm.size() - 1][0]][i] = 1;
for(int j = 0; j < norm.size() - 1; j++){
ans[norm[j][0]][norm[j+1][0]] = ans[norm[j+1][0]][norm[j][0]] = 1;
}
for(auto vec : norm){
for(int j = 0; j < vec.size() - 1; j++){
ans[vec[j]][vec[j+1]] = ans[vec[j+1]][vec[j]] = 1;
}
}
}
if(one.size() > 0){
ans[one[0]][i] = ans[i][one[0]] = 1;
for(int j = 0; j < one.size() - 1; j++){
ans[one[j]][one[j+1]] = ans[one[j+1]][one[j]] = 1;
}
}
}
}
if(!global_pos) return 0;
}
}
return 1;
}
};
DSU UF2;
int construct(vector<vector<int>> p) { //
int n = p.size();
vector<bool> vis(n, 0);
UF2.init(n);
vector<vector<int>> ans(n, vector<int>(n, 0));
for(int i = 0; i < n; i++){
if(!vis[i]){
vis[i] = 1;
for(int j = 0; j < n; j++){
if(i == j || p[i][j] == 0) continue;
if(vis[j] == 1) return 0;
vis[j] = 1;
UF2.unite(i, j);
}
}else{
for(int j = 0; j < n; j++){
if((p[i][j] != 0 && vis[j] == false) || (p[i][j] != 0 && vis[j] == 1 && UF2.get(i) != UF2.get(j))){
return 0;
}
}
}
}
if(UF2.check(p, ans)){
build(ans);
return 1;
}else return 0;
}
Compilation message (stderr)
supertrees.cpp: In member function 'bool DSU::check(std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:33:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int k = 0; k < v.size(); k++){
| ~~^~~~~~~~~~
supertrees.cpp:37:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int j = 0; j < v.size(); j++){
| ~~^~~~~~~~~~
supertrees.cpp:85:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int j = 0; j < v2.size(); j++){
| ~~^~~~~~~~~~~
supertrees.cpp:90:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for(int q = 0; q < v2.size(); q++){
| ~~^~~~~~~~~~~
supertrees.cpp:118:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for(int j = 0; j < three.size() - 1; j++){
| ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:125:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for(int j = 0; j < norm.size() - 1; j++){
| ~~^~~~~~~~~~~~~~~~~
supertrees.cpp:129:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | for(int j = 0; j < vec.size() - 1; j++){
| ~~^~~~~~~~~~~~~~~~
supertrees.cpp:136:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | for(int j = 0; j < one.size() - 1; j++){
| ~~^~~~~~~~~~~~~~~~
# | 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... |