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>
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int parent_component[N];
vector<int> components[N];
vector<int> g[N];
vector<bool> vis(N);
int Find(int x) {
if(x == parent_component[x]) return x;
return parent_component[x] = Find(parent_component[x]);
}
void Unite(int a, int b) {
a = Find(a), b = Find(b);
if(a > b) swap(a, b);
parent_component[b] = a;
}
void dfs(int node, int total_par) {
parent_component[node] = total_par;
components[total_par].push_back(node);
vis[node] = true;
for(int i = 0; i < g[node].size(); i++) {
if(vis[g[node][i]]) continue;
dfs(g[node][i], total_par);
}
}
int construct(vector<vector<int> > input) {
int n = input.size();
vector<int> representative(n, -1);
vector<vector<int> > answer(n, vector<int> (n, 0));
for(int i = 0; i < n; i++) {
parent_component[i] = i;
representative[i] = i;
vis[i] = false;
}
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(input[i][j] == 3) {
return 0;
}
if(input[i][j] != 2) continue;
g[i].push_back(j);
g[j].push_back(i);
}
}
for(int i = 0; i < n; i++) {
if(!vis[i]) {
dfs(i, i);
}
}
for(int i = 0; i < n; i++) {
//cout << "parent_component[i] = " << parent_component[i] << "\n";
parent_component[i] = Find(parent_component[i]);
}
for(int i = 0; i < n; i++) {
if(i != parent_component[i] || components[i].size() == 1) continue;
//cout << "i = " << i << "\n";
sort(components[i].begin(), components[i].end());
for(int j = 0; j < components[i].size(); j++) {
//cout << "components[i][j] = " << components[i][j] << "\n";
for(int k = j + 1; k < components[i].size(); k++) {
if(input[components[i][j]][components[i][k]] == 1) {
representative[components[i][k]] = components[i][j];
//cout << "components[i][j] = " << components[i][j] << ", components[i][k] = " << components[i][k] << "\n";
answer[components[i][j]][components[i][k]] = 1;
answer[components[i][k]][components[i][j]] = 1;
}
}
}
int prev = -1, first_one = -1;
for(int j = 0; j < components[i].size(); j++) {
if(representative[components[i][j]] == components[i][j]) {
if(prev == -1) {
first_one = components[i][j];
}
else {
answer[prev][components[i][j]] = 1;
answer[components[i][j]][prev] = 1;
}
prev = components[i][j];
}
}
if(prev == first_one) return 0;
//cout << "first_one = " << first_one << ", prev = " << prev << "\n";
answer[first_one][prev] = 1;
answer[prev][first_one] = 1;
for(int j = 0; j < components[i].size(); j++) {
for(int k = 0; k < components[i].size(); k++) {
int node1 = components[i][j], node2 = components[i][k];
if(representative[node1] == representative[node2] && input[node1][node2] != 1) {
return 0;
}
if(representative[node1] != representative[node2] && input[node1][node2] != 2) {
return 0;
}
}
}
}
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
if(input[i][j] != 1) {
continue;
}
if(Find(i) == Find(j)) continue;
if(i != Find(i) || j != Find(j)) {
return 0;
}
answer[i][j] = 1;
answer[j][i] = 1;
}
}
build(answer);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'void dfs(int, int)':
supertrees.cpp:27:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i = 0; i < g[node].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int j = 0; j < components[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:67:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for(int k = j + 1; k < components[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:77:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int j = 0; j < components[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:93:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int j = 0; j < components[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
supertrees.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int k = 0; k < components[i].size(); k++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |