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 eb emplace_back
#define go(x) if (x(p)) { build(ans); return 1; } return 0
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
const int mxN=1e3+1;
int n;
vvi ans;
struct DSU {
int par[mxN], sz[mxN];
DSU(int n) {
for (int i=0; i<n; ++i)
par[i]=i, sz[i]=1;
}
int find(int v) {
return (par[v]==v?v:par[v]=find(par[v]));
}
bool merge(int a, int b) {
a=find(a), b=find(b);
if (a==b)
return 0;
if (sz[a]>sz[b])
swap(a, b);
par[a]=b, sz[b]+=sz[a];
return 1;
}
bool same(int a, int b) {
return (find(a)==find(b));
}
};
bool subtask_3(vvi &p) {
DSU d(n);
bool f=0;
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j) {
if (p[i][j])
d.merge(i, j), f|=(p[i][j]==2);
else if (!p[i][j] && d.same(i, j))
return 0;
}
vector<bool> vis(n);
for (int i=0; i<n; ++i) {
if (vis[d.find(i)])
continue;
vis[d.find(i)]=1;
vi v;
for (int j=0; j<n; ++j)
if (d.same(i, j))
v.eb(j);
int sz=v.size();
for (int j=0; j<sz; ++j) {
if (j)
ans[v[j]][v[j-1]]=ans[v[j-1]][v[j]]=1;
if (j<sz-1)
ans[v[j]][v[j+1]]=ans[v[j+1]][v[j]]=1;
}
if (f) {
if (sz>2)
ans[v[0]][v.back()]=ans[v.back()][v[0]]=1;
else if (sz==2)
return 0;
}
}
return 1;
}
bool subtask_4(vvi &p) {
DSU ones(n);
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j) {
if (p[i][j]==1)
ones.merge(i, j);
else if (!p[i][j] && ones.same(i, j))
return 0;
}
vector<bool> vis(n);
for (int i=0; i<n; ++i) {
if (vis[ones.find(i)])
continue;
vis[ones.find(i)]=1;
vi v;
for (int j=0; j<n; ++j)
if (ones.same(i, j)) {
if (p[i]!=p[j])
return 0;
v.eb(j);
}
int sz=v.size();
for (int j=0; j<sz; ++j) {
if (j)
ans[v[j]][v[j-1]]=ans[v[j-1]][v[j]]=1;
if (j<sz-1)
ans[v[j]][v[j+1]]=ans[v[j+1]][v[j]]=1;
}
}
DSU twos(n);
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j) {
if (p[i][j]==2)
twos.merge(ones.find(i), ones.find(j));
else if (!p[i][j] && twos.same(ones.find(i), ones.find(j)))
return 0;
}
vis=vector<bool>(n);
for (int i=0; i<n; ++i) {
if (vis[twos.find(i)])
continue;
vis[twos.find(i)]=1;
vi v;
for (int j=0; j<n; ++j)
if (twos.same(i, j))
v.eb(j);
int sz=v.size();
for (int j=0; j<sz; ++j) {
if (j)
ans[v[j]][v[j-1]]=ans[v[j-1]][v[j]]=1;
if (j<sz-1)
ans[v[j]][v[j+1]]=ans[v[j+1]][v[j]]=1;
}
if (sz>2)
ans[v[0]][v.back()]=ans[v.back()][v[0]]=1;
else if (sz==2)
return 0;
}
return 1;
}
int construct(vvi p) {
n=p.size();
ans=vvi(n, vi(n));
bool f0=0, f1=0, f2=0, f3=0;
for (int i=0; i<n; ++i)
for (int j=0; j<n; ++j) {
if (i==j)
continue;
f0|=(!p[i][j]);
f1|=(p[i][j]==1);
f2|=(p[i][j]==2);
f3|=(p[i][j]==3);
}
if (!f3) {
if (!(f1 && f2)) {
go(subtask_3);
}
else {
go(subtask_4);
}
}
else {
;
}
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(vvi)':
supertrees.cpp:156:1: warning: control reaches end of non-void function [-Wreturn-type]
156 | }
| ^
# | 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... |