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 all(x) (x).begin() (x).end()
#define sz(x) (int) (x).size()
using namespace std;
typedef vector<vector<int>> vvi;
int n;
vector<vector<int>> die = {{-1}};
struct ufds{
int p[1005];
int n;
void init(int N ){
n = N;
for(int i = 0;i <= n;i++) p[i] = i;
}
int findSet(int u){
if(u == p[u]) return u;
else{
p[u] = findSet(p[u]);
return p[u];
}
}
void unionSet(int u, int v){
u = findSet(u);
v = findSet(v);
if(u == v) return;
if(u&v) swap(u,v);
p[u] = v;
}
bool isSameSet(int u, int v){
return findSet(u) == findSet(v);
}
};
vector<vector<int>> adj;
vector<vector<int>> decomp1(){
int good = 1, bad = 0;
ufds uf; uf.init(n);
for(int i = 0;i < n;i++) for(int j = 0;j < n;j++){
if(adj[i][j] >= good) uf.unionSet(i, j);
}
for(int i = 0;i < n;i++) for(int j = 0;j < n;j++){
if(adj[i][j] >= good){
if(!uf.isSameSet(i,j)) return die;
}
else if(adj[i][j] <= bad){
if(uf.isSameSet(i,j)) return die;
}
}
map<int,vector<int> > M;
for(int i = 0;i < n;i++){
int u = uf.findSet(i);
M[u].push_back(i);
}
vvi res;
for(auto x : M){
res.push_back({});
for(int y : x.second) res.back().push_back(y);
}
return res;
}
ufds uf2;
vvi decomp2(vector<int> A){
int good = 1, bad = 2;
for(int i : A) for(int j : A){
if(adj[i][j] == 1) uf2.unionSet(i, j);
}
for(int i : A) for(int j : A){
if(adj[i][j] == 1){
if(!uf2.isSameSet(i,j)) return die;
}
else if(adj[i][j] == 2){
if(uf2.isSameSet(i,j)) return die;
}
}
map<int,vector<int> > M;
for(int i : A){
int u = uf2.findSet(i);
M[u].push_back(i);
}
vvi res;
for(auto x : M){
res.push_back({});
for(int y : x.second) res.back().push_back(y);
}
return res;
}
int construct(vector<vector<int>> ADJ) {
adj = ADJ;
n = adj.size();
vector<vector<int> > answer;
for (int i = 0; i < n; i++) {
std::vector<int> row;
row.resize(n);
answer.push_back(row);
}
for(auto x : ADJ) for(int y : x){
if(y == 3) return 0;
}
vvi res = decomp1();
if(res == die) return 0;
uf2.init(n);
for(vector<int> x : res){
vvi res2 = decomp2(x);
if(sz(res2) == 2) return 0;
for(auto line : res2){
for(int i = 1;i < sz(line);i++){
int a = line[i-1], b = line[i];
answer[a][b] = 1;
answer[b][a] = 1;
}
}
if(sz(res2) > 2){
for(int i = 0;i < sz(res2);i++){
int j = i+1; if(j == sz(res2)) j = 0;
int a = res2[i].back(), b = res2[j].back();
answer[a][b] = 1;
answer[b][a] = 1;
}
}
//cout << "F\n\n";
}
for(int i = 0;i < n;i++) answer[i][i] = 0;
build(answer);
return 1;
}
Compilation message (stderr)
supertrees.cpp: In function 'vvi decomp2(std::vector<int>)':
supertrees.cpp:77:6: warning: unused variable 'good' [-Wunused-variable]
77 | int good = 1, bad = 2;
| ^~~~
supertrees.cpp:77:16: warning: unused variable 'bad' [-Wunused-variable]
77 | int good = 1, bad = 2;
| ^~~
# | 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... |