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 rep(i,a,b) for(int i = (a); i <= (b); i++)
#define rng(a) a.begin(), a.end()
#define ina(n,a) cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]
#define sz(x) (int)(x).size()
#define se second
#define fi first
#define prev coyhhhhhhyoc
#define next sdNNNmNNNNNNNmds
#define y0 hNNNNy_yNNNNNN_sNh
#define y1 mNNNNNdtdNNNNNNtsNNm
#define yn mNNNNNNNNy___smNNNms
#define tm oooooosyysooooot
#define read tyhyt
#define rank ytmNmo
#define index yyy
#define pb push_back
#define pcnt __builtin_popcountll
#define rrep(i,a,b) for(int i = (b); i >= (a); i--)
#define rall(x,a) for(auto x : a)
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;
const int N = 1111;
int p[N][N], b[N][N], dsu[N], rank[N];
int n;
int solve1() {
rep(i, 1, n - 1) {
int ii = i % n + 1;
b[i][ii] = 1;
b[ii][i] = 1;
}
return 1;
}
int find(int u) {
if(dsu[u] == u) return u;
dsu[u] = find(dsu[u]);
return dsu[u];
}
bool merge(int u, int v) {
u = find(u), v = find(v);
if(u == v) return false;
if(rank[u] > rank[v]) {
dsu[v] = u;
} else {
dsu[u] = v;
if(rank[u] == rank[v]) {
rank[v]++;
}
}
return true;
}
int buildDSU() {
rep(i, 1, n) {
dsu[i] = i;
}
rep(i, 1, n) {
rep(j, 1, n) {
if(p[i][j]) {
merge(i, j);
}
}
}
return 1;
}
int solve2() {
if(!buildDSU()) {
return 0;
}
rep(i, 1, n) {
rep(j, 1, n) {
int u = find(i), v = find(j);
if((u == v) != p[i][j]) {
return 0;
}
}
}
rep(u, 1, n) {
int v = find(u);
if(u != v) {
b[u][v] = 1;
b[v][u] = 1;
}
}
return 1;
}
int solve3() {
if(!buildDSU()) {
return 0;
}
vector<vector<int> > gps(n + 1);
rep(u, 1, n) {
int r = find(u);
gps[r].pb(u);
}
rep(i, 1, n) {
rep(j, i + 1, n) {
int u = find(i), v = find(j);
if((u == v) != (p[i][j] == 2)) {
return 0;
}
if(p[i][j] == 2 && sz(gps[u]) < 3) {
return 0;
}
}
}
rep(r, 1, n) {
int k = sz(gps[r]);
rep(i, 0, k - 1) {
int u = gps[r][i], v = gps[r][(i + 1) % k];
if(u != v) {
b[u][v] = 1;
b[v][u] = 1;
}
}
}
return 1;
}
int updateComp(vector<int>& comp) {
set<int> st;
rall(u, comp) {
st.insert(u);
rall(v, comp) {
if(u == v) continue;
if(p[u][v] == 0) {
return 0;
}
}
}
map<vector<int>, int> trees;
rep(u, 1, n) {
if(st.find(u) == st.end()) continue;
vector<int> tree;
rep(v, 1, n) {
if(st.find(v) == st.end()) continue;
if(p[u][v] == 1) {
tree.pb(v);
}
}
trees[tree]++;
}
int fi, pr = -1;
rall(pa, trees) {
auto tree = pa.fi;
if(tree.size() != pa.se) {
return 0;
}
rall(u, tree) {
set<int> tot;
rall(v, tree) {
if(u != v) {
tot.insert(v);
}
}
rep(v, 1, n) {
if(v == u) continue;
if(st.find(v) == st.end()) {
if(p[u][v] != 0) {
return 0;
}
} else if(tot.find(v) != tot.end()) {
if(p[u][v] != 1) {
return 0;
}
} else if(p[u][v] != 2) {
return 0;
}
}
}
int root = tree[0];
if(pr > 0) {
b[root][pr] = 1;
b[pr][root] = 1;
} else {
fi = root;
}
rall(x, tree) {
if(x != root) {
b[root][x] = 1;
b[x][root] = 1;
}
}
pr = root;
}
if(sz(trees) > 2) {
b[pr][fi] = 1;
b[fi][pr] = 1;
} else {
rall(u, comp) {
rall(v, comp) {
if(p[u][v] != 1) {
return 0;
}
}
}
}
return 1;
}
int solve45() {
if(!buildDSU()) {
return 0;
}
vector<vector<int> > gps(n + 1);
rep(u, 1, n) {
int r = find(u);
gps[r].pb(u);
}
rep(u, 1, n) {
if(gps[u].size() > 0) {
int ret = updateComp(gps[u]);
if(ret == 0) {
return 0;
}
}
}
return 1;
}
int solve6() {
return 0;
}
int construct(vector<vector<int>> in) {
n = sz(in);
bool task1 = true, task2 = true, task3 = true, task45 = true;
rep(i, 1, n) {
rep(j, 1, n) {
p[i][j] = in[i - 1][j - 1];
if(i != j) {
if(p[i][j] != 1) {
task1 = false;
}
if(p[i][j] > 1) {
task2 = false;
}
if(p[i][j] != 0 && p[i][j] != 2) {
task3 = false;
}
if(p[i][j] > 2) {
task45 = false;
}
}
}
}
bool ret;
if(task1) {
ret = solve1();
} else if(task2) {
ret = solve2();
} else if(task3) {
ret = solve3();
} else if(task45) {
ret = solve45();
} else {
ret = solve6();
}
if(ret) {
vector<vector<int> > ans(n, vector<int>(n));
rep(i, 1, n) {
rep(j, 1, n) {
ans[i - 1][j - 1] = b[i][j];
}
}
build(ans);
return 1;
}
return 0;
}
Compilation message (stderr)
supertrees.cpp: In function 'int updateComp(std::vector<int>&)':
supertrees.cpp:156:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
156 | if(tree.size() != pa.se) {
| ^
supertrees.cpp:198:19: warning: 'first' may be used uninitialized in this function [-Wmaybe-uninitialized]
198 | b[fi][pr] = 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... |