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>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
vector<vector<int>> v;
vector <vector<int>> dsu1;
vector <int> dsu2;
void unite(int a, int b){
if(dsu2[a] == dsu2[b]) return;
int x = dsu2[a], y = dsu2[b];
if(dsu1[x].size() < dsu1.size()){
while(!dsu1[x].empty()){
int ss = dsu1[x].back();
dsu1[x].pop_back();
dsu2[ss] = y;
dsu1[y].pb(ss);
}
}
else{
while(!dsu1[y].empty()){
int ss = dsu1[y].back();
dsu1[y].pop_back();
dsu2[ss] = x;
dsu1[x].pb(ss);
}
}
}
int construct(vector<vector<int>> p){
v.clear();
dsu1.clear();
dsu2.clear();
int n = p.size();
v = vector<vector<int>>(n, vector<int>(n, 0));
dsu1 = vector<vector<int>>(n);
dsu2 = vector<int>(n);
for(int i = 0; i < n ; i++) dsu1[i].pb(i);
for(int i = 0; i < n; i++) dsu2[i] = i;
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) if(p[i][j] != 0) unite(i, j);
for(auto sth: dsu1){
if(sth.size() < 2) continue;
vector <int> two, one;
for(int x: sth){
int o = 0,tw = 0;
for(int k = 0; k < n; k++){
if(k == x) continue;
if(p[x][k] == 1) o++;
}
if(o == 0) two.pb(x);
else one.pb(x);
}
for(int i = 0; i < one.size() - 1; i++){
int x = one[i], y = one[i+1];
v[x][y] = 1;
v[y][x] = 1;
}
if(two.size() == 0) continue;
for(int i = 0; i < two.size() - 1; i++){
int x = two[i], y = two[i+1];
v[x][y] = 1;
v[y][x] = 1;
}
if(one.size() == 0){
int x = two[0], y = two.back();
v[x][y] = 1;
v[y][x] = 1;
}
else{
int x = two[0], y = two.back(), z = one[0];
v[x][z] = 1;
v[z][x] = 1;
v[y][z] = 1;
v[z][y] = 1;
}
}
build(v);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:47:23: warning: unused variable 'tw' [-Wunused-variable]
47 | int o = 0,tw = 0;
| ^~
supertrees.cpp:55:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i = 0; i < one.size() - 1; i++){
| ~~^~~~~~~~~~~~~~~~
supertrees.cpp:61:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i = 0; i < two.size() - 1; i++){
| ~~^~~~~~~~~~~~~~~~
# | 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... |