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>
using namespace std;
#define sz(x) ((int)(x).size())
vector<vector<int>> g;
vector<vector<bool>> adj;
vector<vector<int>> groups;
bool findGroups(){
vector<bool> vis(sz(g));
for(int u = 0; u < sz(g); u++){
if(vis[u]) continue;
vis[u] = true;
groups.push_back({u});
for(int v : g[u]){
vis[v] = true;
groups.back().push_back(v);
if(adj[u] != adj[v]) return false;
}
}
return true;
}
int construct(vector<vector<int>> p) {
int n = sz(p);
g.resize(n);
adj.resize(n, vector<bool>(n));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(p[i][j] == 3) return 0;
if(p[i][j] > 0) adj[i][j] = 1, g[i].push_back(j);
}
}
if(!findGroups()) return 0;
g.clear();
g.resize(n);
groups.clear();
adj.assign(n, vector<bool>(n));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(p[i][j] == 1){
adj[i][j] = 1;
if(i != j) g[i].push_back(j);
}
}
}
if(!findGroups()) return 0;
vector<vector<int>> ans(n, vector<int>(n));
int m = sz(groups);
vector<int> head(m), comp(n);
for(int i = 0; i < m; i++) {
vector<int> gr = groups[i];
comp[gr[0]] = i;
head[i] = gr[0];
for(int j = 1; j < sz(gr); j++){
ans[gr[j]][gr[j-1]] = ans[gr[j-1]][gr[j]] = 1;
comp[gr[j]] = i;
}
}
g.clear();
g.resize(m);
groups.clear();
adj.assign(m, vector<bool>(m));
for(int i = 0; i < m; i++) adj[i][i] = true;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(comp[i] != comp[j] && p[i][j] == 2 && !adj[comp[i]][comp[j]]){
adj[comp[i]][comp[j]] = true;
g[comp[i]].push_back(comp[j]);
}
}
}
if(!findGroups()) return 0;
int k = sz(groups);
for(int i = 0; i < k; i++){
vector<int> gr = groups[i];
if(sz(gr) == 2) return 0;
if(sz(gr) != 1)
for(int j = 0; j < sz(gr); j++){
ans[head[j]][head[(j+1)%sz(gr)]] = ans[head[(j+1)%sz(gr)]][head[j]] = 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... |