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], par2[1005];
vector<int> stuf[1005];
int getr(int x){
return (par[x] == x ? x : par[x] = getr(par[x]));
}
int g2(int x){
return (par2[x] == x ? x : par2[x] = g2(par2[x]));
}
void m2(int a, int b){
a = g2(a), b = g2(b);
if(a != b)par2[b] = a;
}
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);
for(auto k : pos)if(p[j][k] == 1)m2(j, k);
}
}
if((int)pos.size() <= 1)continue;
if(((int)pos2.size() == 2 && (int)pos.size() == 2))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((int)pos.size() == (int)pos2.size() && (int)pos2.size() > 2){
ans[pos2[0]][pos2[(int)pos2.size() - 1]] = 1;
ans[pos2[(int)pos2.size() - 1]][pos2[0]] = 1;
}
else if(pos2.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;
}
}
else if((int)pos.size() != (int)pos2.size()){
for(int j=0;j<n;j++)stuf[j].clear();
set <int> s;
for(auto j : pos3){
stuf[g2(j)].push_back(j);
s.insert(g2(j));
}
if((int)s.size() == 1 && (int)pos2.size() == 1)return 0;
int pre = -1;
for(auto j : s){
for(auto a : stuf[j]){
for(auto b : stuf[j]){
if(a != b && p[a][b] == 2)return 0;
}
}
for(int k=0;k<(int)stuf[j].size()-1;k++){
ans[stuf[j][k]][stuf[j][k+1]] = 1;
ans[stuf[j][k+1]][stuf[j][k]] = 1;
}
if(pre != -1){
ans[stuf[pre][0]][stuf[j][0]] = 1;
ans[stuf[j][0]][stuf[pre][0]] = 1;
}
else{
ans[pos2[0]][stuf[j][0]] = 1;
ans[stuf[j][0]][pos2[0]] = 1;
}
pre = j;
}
ans[pos2[(int)pos2.size()-1]][stuf[pre][0]] = 1;
ans[stuf[pre][0]][pos2[(int)pos2.size()-1]] = 1;
/*
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;
}
*/
}
for(int j=0;j<n;j++)par2[j] = j;
}
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... |