#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
int N, fa[maxn], sz[maxn];
bool ok = true;
int find(int u) {
return (fa[u] == u) ? u : fa[u] = find(fa[u]);
}
bool merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return false;
if (sz[v] > sz[u]) swap(u, v);
fa[v] = u;
sz[u] += sz[v];
return true;
}
bool vis[maxn];
vector<int> edits;
void dfs(int u, vector<vector<int>>& ans, vector<vector<int>>& p) {
vis[u] = true;
edits.push_back(u);
for (int i = 0; i < N; i++) {
if (!vis[i] && p[u][i] == 1 && find(u) == find(i)) {
ans[u][i] = ans[i][u] = 1;
dfs(i, ans, p);
}
}
}
void process(vector<int> v, vector<vector<int>>& ans, vector<vector<int>>& p) {
if ((int) v.size() == 1) return;
// for (int i = 0; i < v.size(); i++) cout << v[i] << endl;
int n = v.size(), cnt = 0;
vector<int> cyc;
vector<vector<int>> seen(n);
vector<bool> in_cyc(n, 0);
bool two = false;
for (int i : v) {
vector<bool> b(3, 0);
for (int j = 0; j < N; j++) {
if (i == j) continue;
b[p[i][j]] = true;
// cout << i << ' ' << j << ' ' << p[i][j] << '\n';
}
for (int j = 0; j < N; j++) {
if (p[i][j] == 1) merge(i, j);
}
if (b[2] && !b[1]) {
cyc.push_back(i);
// cout << "in cycle!" << ' ' << i << endl;
in_cyc[i] = true;
}
two = (two | b[2]);
}
// cout << "alive\n";
for (int i = 0; i < n; i++) if (!in_cyc[v[i]]) {
seen[find(v[i])].push_back(v[i]);
// cout << "corresponding find: " << v[i] << ' ' << find(v[i]) << '\n';
}
// cout << "alive\n";
for (int i = 0; i < n; i++) {
if (seen[i].empty()) continue;
dfs(seen[i].front(), ans, p);
cyc.push_back(seen[i].front());
in_cyc[seen[i].front()] = true;
seen[find(seen[i].front())].push_back(seen[i].front());
for (int j : edits) vis[j] = 0;
}
if (two && ((int) cyc.size() == 1 || (int) cyc.size() == 2)) {
ok = false;
return;
}
for (int i = 0; i < n; i++) if (!seen[i].empty()) cnt++;
if (cnt > (int) cyc.size()) {
ok = false;
return;
}
for (int i = 0; i < cyc.size(); i++) {
int nxt = (i + 1) % cyc.size();
ans[cyc[i]][cyc[nxt]] = ans[cyc[nxt]][cyc[i]] = 1;
}
}
int construct(std::vector<std::vector<int>> p) {
int n = p[0].size();
N = n;
vector<vector<int>> ans(n, vector<int> (n, 0));
for (int i = 0; i < n; i++) {
fa[i] = i, sz[i] = 1;
for (int j = 0; j < n; j++) {
if (p[i][j] >= 3) return 0;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j]) merge(i, j);
}
}
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
if (!p[i][j] && find(i) == find(j)) return 0;
}
vector<vector<int>> g(n);
for (int i = 0; i < n; i++) g[find(i)].push_back(i);
for (int i = 0; i < n; i++) fa[i] = i, sz[i] = 1;
for (int i = 0; i < n; i++) {
if (!g[i].empty()) {
process(g[i], ans, p);
if (!ok) return 0;
}
}
build(ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'void process(std::vector<int>, std::vector<std::vector<int> >&, std::vector<std::vector<int> >&)':
supertrees.cpp:84:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for (int i = 0; i < cyc.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
b[0][0] is not 0 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
b[0][0] is not 0 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
7 ms |
1236 KB |
Output is correct |
9 |
Correct |
151 ms |
23632 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1236 KB |
Output is correct |
13 |
Correct |
155 ms |
23628 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
824 KB |
Output is correct |
17 |
Correct |
69 ms |
14084 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
304 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Runtime error |
19 ms |
6988 KB |
Execution killed with signal 6 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
b[1][1] is not 0 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
b[0][0] is not 0 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
b[0][0] is not 0 |
3 |
Halted |
0 ms |
0 KB |
- |