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;
//#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
int par[1005];
int getr(int x){
return (par[x] == x ? x : par[x] = getr(par[x]));
}
void merge(int a, int b){
a = getr(a), b = getr(b);
if(a != b)par[b] = a;
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
for(int i=0;i<n;i++)par[i] = i;
std::vector<std::vector<int>> ans;
for (int i = 0; i < n; i++) {
std::vector<int> row(n, 0);
ans.push_back(row);
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(p[i][j])merge(i, j);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(!p[i][j] && getr(i) == getr(j))return 0;
}
}
for(int i=0;i<n;i++){
vector<int> pos;
for(int j=0;j<n;j++)if(getr(j) == i)pos.push_back(j);
vector<int> pos2, pos3;
for(auto j : pos){
bool f = 1;
for(auto k : pos){
if(j == k)continue;
if(p[j][k] != 2)f =0 ;
}
if(f)pos2.push_back(j);
else pos3.push_back(j);
}
if((int)pos.size() <= 1)continue;
//if(((int)pos2.size() == 1 && !pos3.empty())|| ((int)pos2.size() == 2 && pos3.empty()))return 0;
for(int j=0;j<(int)pos2.size() - 1;j++){
ans[pos2[j]][pos2[j+1]] = 1;
ans[pos2[j+1]][pos2[j]] = 1;
}
if(pos3.empty() && (int)pos2.size() > 2){
ans[pos2[0]][pos2[(int)pos2.size() - 1]] = 1;
ans[pos2[(int)pos2.size() - 1]][pos2[0]] = 1;
}
else if(!pos3.empty()){
for(int j=0;j<(int)pos3.size() - 1;j++){
ans[pos3[j]][pos3[j+1]] = 1;
ans[pos3[j+1]][pos3[j]] = 1;
}
if(!pos2.empty()){
ans[pos3[0]][pos2[0]] = 1;
ans[pos2[0]][pos3[0]] = 1;
ans[pos3[0]][pos2[(int)pos2.size()-1]] = 1;
ans[pos2[(int)pos2.size()-1]][pos3[0]] = 1;
}
}
}
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... |