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 ll long long
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define forn(i, n) for(int i=0;i < n; ++i)
const int N = 1005;
int par[N], ways[N][N];
bool vis[N];
bool ok = true;
int get(int a) {
return par[a] = (par[a] == a ? a : get(par[a]));
}
void uni(int a, int b) {
a = get(a);
b = get(b);
par[a] = b;
}
vector<int> adj[N];
void dfs(int u, int start) {
vis[u] = true;
if(++ways[start][u] >= 3 || !ok) {
ok = false;
return;
}
for(int v: adj[u]) {
if(!vis[v]) {
dfs(v, start);
}
}
vis[u] = false;
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
forn(i, n) forn(j, n) if(p[i][j] == 3) return 0;
forn(i, n) par[i] = i;
std::vector<std::vector<int>> answer(n, vector<int>(n, 0));
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j < n; ++j) {
if(p[i][j] == 1 && get(i) != get(j)) {
answer[i][j] = answer[j][i] = 1;
uni(i, j);
}
}
}
for(int i = 0; i < n; ++i) {
vector<int> v;
v.pb(i);
for(int j = i + 1; j < n; ++j) {
if(get(i) != get(j) && p[get(i)][get(j)] == 2) {
v.pb(j);
uni(i, j);
}
}
if(sz(v) >= 2) {
for(int k = 0; k < sz(v); ++k) {
answer[v[k]][v[(k + 1) % sz(v)]] = answer[v[(k + 1) % sz(v)]][v[k]] = 1;
}
}
}
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j < n; ++j) {
if(answer[i][j]) {
adj[i].pb(j);
adj[j].pb(i);
}
}
}
forn(i, n) {
forn(j, n) {
if(get(i) == get(j) && !p[i][j]) return 0;
if(get(i) != get(j) && p[i][j]) return 0;
}
}
forn(i, n) dfs(i, i);
if(!ok) return 0;
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j < n; ++j) {
if(ways[i][j] != p[i][j]) return 0;
}
}
build(answer);
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... |