This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In heads make different sets of heads
#include "supertrees.h"
#include <vector>
#include <set>
#include <iostream>
using namespace std;
vector<int> parent(1003);
vector<int> header_parent(1003);
vector<int> header_parent_current(1003);
vector<set<int>> header_counter;
int find_head(int n){
if(parent[n] == n){
return n;
}return find_head(parent[n]);
}
int find_head_parent(int n){
if(header_parent[n] == n){
return n;
}return find_head_parent(header_parent[n]);
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
vector<vector<int>>ans(n,vector<int>(n,0));
set<int> a;
for (int i = 0; i < n; i++) {
parent[i] = i;
header_parent[i] = i;
header_parent_current[i] = i;
header_counter.push_back(a);
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)
continue;
if(p[i][j] == 3){
return 0;
}else if(p[i][j] == 1){
int lower = min(i,j);
int higher = max(i,j);
if(find_head(lower) != find_head(higher)){
ans[i][j] = 1;
ans[j][i] = 1;
parent[higher] = find_head(lower);
header_parent[higher] = parent[higher];
}
}
}
}
// cout <<"process 2" << endl;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)
continue;
if(p[i][j] == 2){
int q = find_head(i);
int m = find_head(j);
if(q==m)
return 0;
if(header_parent[q] == header_parent[m])
continue;
int lower = min(q,m);
int higher = max(q,m);
ans[header_parent_current[q]][header_parent_current[m]] = 1;
ans[header_parent_current[m]][header_parent_current[q]] = 1;
header_parent[higher] = find_head_parent(lower);
header_counter[header_parent[higher]].insert(higher);
header_parent_current[header_parent[higher]] = higher;
// cout << i << " " << j << " " << header_parent[higher] << " " << header_counter[header_parent[higher]].size() << endl;
}
}
}
for(int i=0;i<n;i++){
if(i != header_parent_current[i]){
ans[header_parent_current[i]][i] = 1;
ans[i][header_parent_current[i]] = 1;
}
}
// cout << "headers connected" << endl;
// cout << endl;
// for (int i = 0; i < n; i++) {
// cout << i << " " << parent[i] << " " << header_parent[i] << " " << header_counter[i].size() << endl;
// }
// cout << endl;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int i_head = find_head(i);
int j_head = find_head(j);
int i_head_header = find_head_parent(i_head);
int j_head_header = find_head_parent(j_head);
if(i==j)
continue;
if(p[i][j] == 0){
if(i_head == j_head){
return 0;
}
if(i_head_header == j_head_header){
return 0;
}
}else if(p[i][j] == 1){
if(i_head != j_head){
return 0;
}
}else if(p[i][j] == 2){
// cout << i << " " << j << " " << i_head_header << " " << j_head_header << endl;
if((header_counter[i_head_header].size() < 2)){
return 0;
}
if(i_head == j_head){
return 0;
}
if(i_head_header != j_head_header){
return 0;
}
}
}
}
build(ans);
return 1;
}
# | 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... |