#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3;
int par[N + 5], sz[N + 5];
vector<int> ls[N + 5];
int vertex[N + 5];
bool vis[N + 5];
int find(int x) { return x == par[x] ? x : find(par[x]); }
bool same(int x, int y) { return find(x) == find(y); }
void unite(int x, int y) {
x = find(x), y = find(y);
if (sz[x] < sz[y]) swap(x, y);
sz[x] += sz[y], par[y] = x;
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (p[i][j] == 3) {
return 0;
}
}
}
// check connectivity
for (int i = 0; i < n; i++) par[i] = i, sz[i] = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i][j] && !same(i, j)) unite(i, j);
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (!p[i][j] && same(i, j)) return 0;
}
}
for (int i = 0; i < n; i++) ls[find(i)].push_back(i);
for (int i = 0; i < n; i++) vertex[i] = -1;
vector<vector<int>> b(n, vector<int>(n));
for (auto&arr : ls) {
if (arr.empty()) continue;
int sz = arr.size(), hasTwo = 0;
for (int i = 0; i < sz; i++) {
for (int j = i + 1; j < sz; j++) {
hasTwo |= p[arr[i]][arr[j]] >= 2;
}
}
if (!hasTwo) {
for (int i = 0; i + 1 < sz; i++) {
int u = arr[i], v = arr[i + 1];
b[u][v] = b[v][u] = 1;
}
continue;
}
vector<int> loop;
for (int i = 0, flag = 0; !flag && i < sz; i++) {
for (int j = i + 1; j < sz; j++) {
int u = arr[i], v = arr[j];
if (p[u][v] >= 2) {
loop.push_back(u), loop.push_back(v);
vertex[u] = -1, vertex[v] = -1;
flag = true;
break;
}
}
}
for (int i = 0; i < sz; i++) {
int valid = 1, u = arr[i];
for (auto&v : loop) valid &= u != v && p[u][v] >= 2;
if (valid) loop.push_back(u);
}
/*cout << "LOOP" << '\n';
for (auto&v : loop) cout << v << " ";
cout << '\n';*/
if (loop.size() > 0 && loop.size() <= 2) return 0;
for (int i = 0; i < loop.size(); i++) {
int u = loop[i], v = loop[(i + 1) % loop.size()];
b[u][v] = b[v][u] = 1;
}
queue<int> q;
for (auto&v : loop) q.push(v), vis[v] = true;
while (q.size()) {
int u = q.front(); q.pop();
for (int i = 0; i < sz; i++) {
int v = arr[i];
if (v == u || vis[v] || p[v][u] != 1) continue;
vis[v] = true;
if (vertex[u] == -1) vertex[v] = u;
else vertex[v] = vertex[u];
q.push(v);
b[u][v] = b[v][u] = 1;
}
}
}
build(b);
return 1;
}
/*static int n;
static std::vector<std::vector<int>> p;
static std::vector<std::vector<int>> b;
static bool called = false;
static void check(bool cond, std::string message) {
if (!cond) {
printf("%s\n", message.c_str());
fclose(stdout);
exit(0);
}
}
void build(std::vector<std::vector<int>> _b) {
check(!called, "build is called more than once");
called = true;
check((int)_b.size() == n, "Invalid number of rows in b");
for (int i = 0; i < n; i++) {
check((int)_b[i].size() == n, "Invalid number of columns in b");
}
b = _b;
}
int main() {
//assert(scanf("%d", &n) == 1);
cin >> n;
p.resize(n);
for (int i = 0; i < n; i++) {
p[i].resize(n);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
//assert(scanf("%d", &p[i][j]) == 1);
cin >> p[i][j];
}
}
//fclose(stdin);
int possible = construct(p);
check(possible == 0 || possible == 1, "Invalid return value of construct");
if (possible == 1) {
check(called, "construct returned 1 without calling build");
} else {
check(!called, "construct called build but returned 0");
}
printf("%d\n", possible);
if (possible == 1) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j) {
printf(" ");
}
printf("%d", b[i][j]);
}
printf("\n");
}
}
// fclose(stdout);
}*/
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:86:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int i = 0; i < loop.size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
150 ms |
22088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
150 ms |
22088 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
1200 KB |
Output is correct |
13 |
Correct |
145 ms |
22108 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
65 ms |
8224 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
37 ms |
5732 KB |
Output is correct |
21 |
Correct |
150 ms |
22052 KB |
Output is correct |
22 |
Correct |
147 ms |
22036 KB |
Output is correct |
23 |
Correct |
151 ms |
22032 KB |
Output is correct |
24 |
Correct |
143 ms |
22020 KB |
Output is correct |
25 |
Correct |
59 ms |
8140 KB |
Output is correct |
26 |
Correct |
57 ms |
8100 KB |
Output is correct |
27 |
Correct |
151 ms |
22052 KB |
Output is correct |
28 |
Correct |
150 ms |
22220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
6 ms |
1108 KB |
Output is correct |
9 |
Correct |
178 ms |
21988 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
146 ms |
22052 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
85 ms |
8112 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
36 ms |
5728 KB |
Output is correct |
22 |
Correct |
145 ms |
22028 KB |
Output is correct |
23 |
Correct |
145 ms |
22092 KB |
Output is correct |
24 |
Correct |
153 ms |
22032 KB |
Output is correct |
25 |
Correct |
60 ms |
12108 KB |
Output is correct |
26 |
Correct |
62 ms |
8096 KB |
Output is correct |
27 |
Correct |
149 ms |
22056 KB |
Output is correct |
28 |
Correct |
153 ms |
22100 KB |
Output is correct |
29 |
Correct |
60 ms |
12080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
37 ms |
5800 KB |
Output is correct |
5 |
Correct |
145 ms |
22048 KB |
Output is correct |
6 |
Correct |
146 ms |
22092 KB |
Output is correct |
7 |
Correct |
156 ms |
22052 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
37 ms |
5708 KB |
Output is correct |
10 |
Correct |
145 ms |
22012 KB |
Output is correct |
11 |
Correct |
147 ms |
22064 KB |
Output is correct |
12 |
Correct |
153 ms |
22064 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
44 ms |
5800 KB |
Output is correct |
17 |
Correct |
153 ms |
24012 KB |
Output is correct |
18 |
Correct |
151 ms |
24012 KB |
Output is correct |
19 |
Correct |
149 ms |
24016 KB |
Output is correct |
20 |
Correct |
147 ms |
23964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
150 ms |
22088 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
1200 KB |
Output is correct |
13 |
Correct |
145 ms |
22108 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
65 ms |
8224 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
37 ms |
5732 KB |
Output is correct |
21 |
Correct |
150 ms |
22052 KB |
Output is correct |
22 |
Correct |
147 ms |
22036 KB |
Output is correct |
23 |
Correct |
151 ms |
22032 KB |
Output is correct |
24 |
Correct |
143 ms |
22020 KB |
Output is correct |
25 |
Correct |
59 ms |
8140 KB |
Output is correct |
26 |
Correct |
57 ms |
8100 KB |
Output is correct |
27 |
Correct |
151 ms |
22052 KB |
Output is correct |
28 |
Correct |
150 ms |
22220 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
6 ms |
1108 KB |
Output is correct |
37 |
Correct |
178 ms |
21988 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
8 ms |
1236 KB |
Output is correct |
41 |
Correct |
146 ms |
22052 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
85 ms |
8112 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
36 ms |
5728 KB |
Output is correct |
50 |
Correct |
145 ms |
22028 KB |
Output is correct |
51 |
Correct |
145 ms |
22092 KB |
Output is correct |
52 |
Correct |
153 ms |
22032 KB |
Output is correct |
53 |
Correct |
60 ms |
12108 KB |
Output is correct |
54 |
Correct |
62 ms |
8096 KB |
Output is correct |
55 |
Correct |
149 ms |
22056 KB |
Output is correct |
56 |
Correct |
153 ms |
22100 KB |
Output is correct |
57 |
Correct |
60 ms |
12080 KB |
Output is correct |
58 |
Correct |
0 ms |
340 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
4 ms |
620 KB |
Output is correct |
61 |
Correct |
74 ms |
8104 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
37 ms |
5772 KB |
Output is correct |
66 |
Correct |
68 ms |
10128 KB |
Output is correct |
67 |
Correct |
61 ms |
10128 KB |
Output is correct |
68 |
Correct |
60 ms |
10256 KB |
Output is correct |
69 |
Correct |
60 ms |
14100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1108 KB |
Output is correct |
7 |
Correct |
150 ms |
22088 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
6 ms |
1200 KB |
Output is correct |
13 |
Correct |
145 ms |
22108 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
65 ms |
8224 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
37 ms |
5732 KB |
Output is correct |
21 |
Correct |
150 ms |
22052 KB |
Output is correct |
22 |
Correct |
147 ms |
22036 KB |
Output is correct |
23 |
Correct |
151 ms |
22032 KB |
Output is correct |
24 |
Correct |
143 ms |
22020 KB |
Output is correct |
25 |
Correct |
59 ms |
8140 KB |
Output is correct |
26 |
Correct |
57 ms |
8100 KB |
Output is correct |
27 |
Correct |
151 ms |
22052 KB |
Output is correct |
28 |
Correct |
150 ms |
22220 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
6 ms |
1108 KB |
Output is correct |
37 |
Correct |
178 ms |
21988 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
8 ms |
1236 KB |
Output is correct |
41 |
Correct |
146 ms |
22052 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
85 ms |
8112 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
36 ms |
5728 KB |
Output is correct |
50 |
Correct |
145 ms |
22028 KB |
Output is correct |
51 |
Correct |
145 ms |
22092 KB |
Output is correct |
52 |
Correct |
153 ms |
22032 KB |
Output is correct |
53 |
Correct |
60 ms |
12108 KB |
Output is correct |
54 |
Correct |
62 ms |
8096 KB |
Output is correct |
55 |
Correct |
149 ms |
22056 KB |
Output is correct |
56 |
Correct |
153 ms |
22100 KB |
Output is correct |
57 |
Correct |
60 ms |
12080 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
37 ms |
5800 KB |
Output is correct |
62 |
Correct |
145 ms |
22048 KB |
Output is correct |
63 |
Correct |
146 ms |
22092 KB |
Output is correct |
64 |
Correct |
156 ms |
22052 KB |
Output is correct |
65 |
Correct |
0 ms |
340 KB |
Output is correct |
66 |
Correct |
37 ms |
5708 KB |
Output is correct |
67 |
Correct |
145 ms |
22012 KB |
Output is correct |
68 |
Correct |
147 ms |
22064 KB |
Output is correct |
69 |
Correct |
153 ms |
22064 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
44 ms |
5800 KB |
Output is correct |
74 |
Correct |
153 ms |
24012 KB |
Output is correct |
75 |
Correct |
151 ms |
24012 KB |
Output is correct |
76 |
Correct |
149 ms |
24016 KB |
Output is correct |
77 |
Correct |
147 ms |
23964 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
0 ms |
340 KB |
Output is correct |
80 |
Correct |
4 ms |
620 KB |
Output is correct |
81 |
Correct |
74 ms |
8104 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
0 ms |
212 KB |
Output is correct |
84 |
Correct |
0 ms |
212 KB |
Output is correct |
85 |
Correct |
37 ms |
5772 KB |
Output is correct |
86 |
Correct |
68 ms |
10128 KB |
Output is correct |
87 |
Correct |
61 ms |
10128 KB |
Output is correct |
88 |
Correct |
60 ms |
10256 KB |
Output is correct |
89 |
Correct |
60 ms |
14100 KB |
Output is correct |
90 |
Correct |
1 ms |
212 KB |
Output is correct |
91 |
Correct |
0 ms |
212 KB |
Output is correct |
92 |
Correct |
3 ms |
640 KB |
Output is correct |
93 |
Correct |
60 ms |
10128 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
212 KB |
Output is correct |
96 |
Correct |
1 ms |
332 KB |
Output is correct |
97 |
Correct |
15 ms |
2772 KB |
Output is correct |
98 |
Correct |
63 ms |
10064 KB |
Output is correct |
99 |
Correct |
58 ms |
10056 KB |
Output is correct |
100 |
Correct |
59 ms |
10112 KB |
Output is correct |
101 |
Correct |
60 ms |
10060 KB |
Output is correct |
102 |
Correct |
62 ms |
10128 KB |
Output is correct |
103 |
Correct |
61 ms |
10132 KB |
Output is correct |
104 |
Correct |
58 ms |
10060 KB |
Output is correct |
105 |
Correct |
62 ms |
10148 KB |
Output is correct |