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 PB push_back
#define sz(x) ((int)x.size())
using namespace std;
const int N = 1010;
vector<int> vc, vct, cur;
int pre[N], prv[N];
int mrk[N], used[N];
int get(int x){
return (pre[x] == x ? x : pre[x] = get(pre[x]));
}
int getp(int x){
return (prv[x] == x ? x : prv[x] = getp(prv[x]));
}
int construct(vector<vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; i++) {
pre[i] = prv[i] = i;
}
vector<vector<int> > ans;
ans.resize(n);
for (int i = 0; i < n; i++)
ans[i].resize(n);
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
if (p[i][j] == 3) return 0;
if (p[i][j] > 0){
int xx = get(i);
int yy = get(j);
pre[xx] = yy;
}
}
for (int i = 0; i < n; i++){
if (mrk[get(i)]) continue;
mrk[get(i)] = 1;
vc.clear();
for (int j = i; j < n; j++)
if (get(j) == get(i))
vc.PB(j);
for (int it = 0; it < sz(vc); it++)
for (int jt = it + 1; jt < sz(vc); jt++) {
if (p[vc[it]][vc[jt]] == 0)
return 0;
if (p[vc[it]][vc[jt]] == 1){
int xx = getp(vc[it]);
int yy = getp(vc[jt]);
prv[xx] = yy;
}
}
vct.clear();
for (int it = 0; it < sz(vc); it++){
int v = vc[it];
if (used[getp(v)]) continue;
cur.clear();
for (int jt = it; jt < sz(vc); jt++)
if (getp(vc[jt]) == getp(v))
cur.PB(vc[jt]);
vct.PB(v);
used[getp(v)] = 1;
for (int it = 0; it < sz(cur); it++)
for (int jt = it + 1; jt < sz(cur); jt++)
if (p[cur[it]][cur[jt]] == 2)
return 0;
for (int it = 0; it < sz(cur); it++){
if (it > 0) ans[cur[it]][cur[it - 1]] = 1;
if (it + 1 < sz(cur)) ans[cur[it]][cur[it + 1]] = 1;
}
}
if (sz(vct) == 2) return 0;
//link 2
if (sz(vct) == 1) continue;
for (int it = 0; it < sz(vct); it++){
int nxt = (it + 1) % sz(vct);
int prd = (it + sz(vct) - 1) % sz(vct);
ans[vct[it]][vct[prd]] = 1;
ans[vct[it]][vct[nxt]] = 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... |