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>
using namespace std;
const int N = 1001;
struct DSU {
vector<int> p;
vector<vector<int>> els;
DSU() {
p.resize(N);
els.resize(N);
iota(p.begin(), p.end(), 0);
for (int i = 0; i < N; ++i) {
els[i].push_back(i);
}
}
int get(int a) {
return p[a] = (a == p[a] ? a : get(p[a]));
}
void merge(int a, int b) {
int x = get(a);
int y = get(b);
if (x == y) return;
if (els[x].size() > els[y].size()) {
swap(x, y);
}
p[x] = y;
while (els[x].size()) els[y].push_back(els[x].back()), els[x].pop_back();
}
};
vector<int> graph[N];
vector<vector<int>> cur_p(N, vector<int>(N, 0));
int cnt[N]={};
vector<int> path;
void dfs(int start, int v) {
path.push_back(v);
cur_p[start][v]++;
cnt[v]++;
for (int u : graph[v]) {
if (cnt[u] == 0) dfs(start, u);
}
cnt[v]--;
path.pop_back();
}
void build(vector<vector<int>> b);
int construct(vector<vector<int>> p) {
DSU lines;
const int n = p.size();
vector<vector<int>> b(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) {
int x = lines.get(i);
int y = lines.get(j);
if (x != y) {
b[i][j] = b[j][i] = 1;
lines.merge(x, y);
}
}
}
}
DSU comps;
for (int i = 0; i < n; ++i) {
for (int j = i+1; j < n; ++j) {
if (p[i][j] > 0) comps.merge(i, j);
}
}
vector<bool> vis(n, false);
for (int i = 0; i < n; ++i) {
int j = comps.get(i);
if (vis[j]) continue;
vis[j] = true;
vector<int> vs = comps.els[j];
vector<int> twos;
vector<bool> vis_lines(n, false);
for (int k = 0; k < vs.size(); ++k) {
int v = vs[k];
if (vis_lines[lines.get(v)]) continue;
int u = lines.els[lines.get(v)].front();
twos.push_back(u);
vis_lines[lines.get(v)] = true;
}
if (twos.size() == 1) continue;
for (int k = 0; k < twos.size(); ++k) {
int nxt = (k+1)%twos.size();
b[twos[k]][twos[nxt]] = b[twos[nxt]][twos[k]] = 1;
}
}
for (int i = 0; i < n; ++i) {
for (int j = i+1; j < n; ++j) {
if (b[i][j]) graph[i].push_back(j), graph[j].push_back(i);
}
}
for (int i = 0; i < n; ++i) {
dfs(i, i);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (cur_p[i][j] != p[i][j]) return 0;
}
}
build(b);
return true;
}
Compilation message (stderr)
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:79:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int k = 0; k < vs.size(); ++k) {
| ~~^~~~~~~~~~~
supertrees.cpp:87:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int k = 0; k < twos.size(); ++k) {
| ~~^~~~~~~~~~~~~
# | 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... |