#include <bits/stdc++.h>
#define IOI
#ifdef IOI
#include "supertrees.h"
#endif
using namespace std;
const int Nmax = 1e3 + 10;
int n;
#ifndef IOI
std::vector<std::vector<int>> p;
std::vector<std::vector<int>> b;
bool called = false;
void check(bool cond, std::string message) {
if (!cond) {
printf("%s\n", message.c_str());
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;
}
#endif // IOI
int root[2][Nmax], sz[2][Nmax];
vector <int> rt[Nmax];
int Find(int _, int u) {
return (u == root[_][u] ? u : root[_][u] = Find(_, root[_][u]));
}
void Union(int _, int u, int v) {
if ((u = Find(_, u)) == (v = Find(_, v))) return;
if (sz[_][u] < sz[_][v]) swap(u, v);
sz[_][u] += sz[_][v];
root[_][v] = u;
}
int construct(vector <vector <int> > p) {
int n = p.size();
vector <vector <int> > b(n, vector <int> (n, 0));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (p[i][j] != p[j][i] || p[i][j] == 3) return 0;
for (int i = 0; i < n; ++i) if (p[i][i] != 1) return 0;
for (int i = 0; i < n; ++i) for (int j = 0; j < 2; ++j) root[j][i] = i, sz[j][i] = 1;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (p[i][j] == 1) Union(0, i, j);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (Find(0, i) == Find(0, j) && p[i][j] != 1) return 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (p[i][j] == 2) Union(1, Find(0, i), Find(0, j));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (Find(0, i) != Find(0, j) && p[i][j] != 2 && Find(1, Find(0, i)) == Find(1, Find(0, j))) return 0;
for (int i = 0; i < n; ++i)
if (i != Find(0, i)) {
b[i][Find(0, i)] = b[Find(0, i)][i] = 1;
}
for (int i = 0; i < n; ++i) {
if (i == Find(0, i)) rt[Find(1, i)].push_back(i);
}
for (int i = 0; i < n; ++i) {
if (rt[i].size() <= 1) continue;
if (rt[i].size() == 2) return 0;
for (int j = 1; j < rt[i].size(); ++j) b[rt[i][j]][rt[i][j - 1]] = b[rt[i][j - 1]][rt[i][j]] = 1;
b[rt[i].front()][rt[i].back()] = b[rt[i].back()][rt[i].front()] = 1;
}
build(b);
return 1;
}
#ifndef IOI
int main() {
freopen("test.inp", "r", stdin);
assert(scanf("%d", &n) == 1);
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);
}
}
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");
}
}
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
if (b[i][j]) cout << i << " " << j << "\n";
}
#endif
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:82:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (int j = 1; j < rt[i].size(); ++j) b[rt[i][j]][rt[i][j - 1]] = b[rt[i][j - 1]][rt[i][j]] = 1;
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
14 ms |
1116 KB |
Output is correct |
7 |
Correct |
252 ms |
21980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
14 ms |
1116 KB |
Output is correct |
7 |
Correct |
252 ms |
21980 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1200 KB |
Output is correct |
13 |
Correct |
259 ms |
22172 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
7 ms |
732 KB |
Output is correct |
17 |
Correct |
116 ms |
12068 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
67 ms |
5784 KB |
Output is correct |
21 |
Correct |
270 ms |
22036 KB |
Output is correct |
22 |
Correct |
275 ms |
22092 KB |
Output is correct |
23 |
Correct |
277 ms |
21956 KB |
Output is correct |
24 |
Correct |
260 ms |
22052 KB |
Output is correct |
25 |
Correct |
100 ms |
12064 KB |
Output is correct |
26 |
Correct |
97 ms |
12056 KB |
Output is correct |
27 |
Correct |
276 ms |
22028 KB |
Output is correct |
28 |
Correct |
258 ms |
22020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
11 ms |
1228 KB |
Output is correct |
9 |
Correct |
254 ms |
22000 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1124 KB |
Output is correct |
13 |
Correct |
251 ms |
22052 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
716 KB |
Output is correct |
17 |
Correct |
118 ms |
12068 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
0 ms |
332 KB |
Output is correct |
21 |
Correct |
66 ms |
5788 KB |
Output is correct |
22 |
Correct |
257 ms |
22084 KB |
Output is correct |
23 |
Correct |
260 ms |
22160 KB |
Output is correct |
24 |
Correct |
274 ms |
22116 KB |
Output is correct |
25 |
Correct |
127 ms |
12184 KB |
Output is correct |
26 |
Correct |
106 ms |
12056 KB |
Output is correct |
27 |
Correct |
275 ms |
22064 KB |
Output is correct |
28 |
Correct |
276 ms |
22084 KB |
Output is correct |
29 |
Correct |
128 ms |
12172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
69 ms |
5784 KB |
Output is correct |
5 |
Correct |
264 ms |
22036 KB |
Output is correct |
6 |
Correct |
261 ms |
22052 KB |
Output is correct |
7 |
Correct |
284 ms |
22004 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
78 ms |
5768 KB |
Output is correct |
10 |
Correct |
261 ms |
22052 KB |
Output is correct |
11 |
Correct |
254 ms |
21980 KB |
Output is correct |
12 |
Correct |
280 ms |
22084 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
66 ms |
5784 KB |
Output is correct |
17 |
Correct |
261 ms |
22164 KB |
Output is correct |
18 |
Correct |
275 ms |
22052 KB |
Output is correct |
19 |
Correct |
268 ms |
22036 KB |
Output is correct |
20 |
Correct |
261 ms |
22200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
14 ms |
1116 KB |
Output is correct |
7 |
Correct |
252 ms |
21980 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1200 KB |
Output is correct |
13 |
Correct |
259 ms |
22172 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
7 ms |
732 KB |
Output is correct |
17 |
Correct |
116 ms |
12068 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
67 ms |
5784 KB |
Output is correct |
21 |
Correct |
270 ms |
22036 KB |
Output is correct |
22 |
Correct |
275 ms |
22092 KB |
Output is correct |
23 |
Correct |
277 ms |
21956 KB |
Output is correct |
24 |
Correct |
260 ms |
22052 KB |
Output is correct |
25 |
Correct |
100 ms |
12064 KB |
Output is correct |
26 |
Correct |
97 ms |
12056 KB |
Output is correct |
27 |
Correct |
276 ms |
22028 KB |
Output is correct |
28 |
Correct |
258 ms |
22020 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
0 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
11 ms |
1228 KB |
Output is correct |
37 |
Correct |
254 ms |
22000 KB |
Output is correct |
38 |
Correct |
0 ms |
332 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
11 ms |
1124 KB |
Output is correct |
41 |
Correct |
251 ms |
22052 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
332 KB |
Output is correct |
44 |
Correct |
5 ms |
716 KB |
Output is correct |
45 |
Correct |
118 ms |
12068 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
0 ms |
332 KB |
Output is correct |
49 |
Correct |
66 ms |
5788 KB |
Output is correct |
50 |
Correct |
257 ms |
22084 KB |
Output is correct |
51 |
Correct |
260 ms |
22160 KB |
Output is correct |
52 |
Correct |
274 ms |
22116 KB |
Output is correct |
53 |
Correct |
127 ms |
12184 KB |
Output is correct |
54 |
Correct |
106 ms |
12056 KB |
Output is correct |
55 |
Correct |
275 ms |
22064 KB |
Output is correct |
56 |
Correct |
276 ms |
22084 KB |
Output is correct |
57 |
Correct |
128 ms |
12172 KB |
Output is correct |
58 |
Correct |
0 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
5 ms |
716 KB |
Output is correct |
61 |
Correct |
114 ms |
12084 KB |
Output is correct |
62 |
Correct |
0 ms |
204 KB |
Output is correct |
63 |
Correct |
0 ms |
332 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Correct |
67 ms |
5704 KB |
Output is correct |
66 |
Correct |
104 ms |
12056 KB |
Output is correct |
67 |
Correct |
98 ms |
12104 KB |
Output is correct |
68 |
Correct |
106 ms |
12168 KB |
Output is correct |
69 |
Correct |
129 ms |
12184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
14 ms |
1116 KB |
Output is correct |
7 |
Correct |
252 ms |
21980 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
11 ms |
1200 KB |
Output is correct |
13 |
Correct |
259 ms |
22172 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
7 ms |
732 KB |
Output is correct |
17 |
Correct |
116 ms |
12068 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
67 ms |
5784 KB |
Output is correct |
21 |
Correct |
270 ms |
22036 KB |
Output is correct |
22 |
Correct |
275 ms |
22092 KB |
Output is correct |
23 |
Correct |
277 ms |
21956 KB |
Output is correct |
24 |
Correct |
260 ms |
22052 KB |
Output is correct |
25 |
Correct |
100 ms |
12064 KB |
Output is correct |
26 |
Correct |
97 ms |
12056 KB |
Output is correct |
27 |
Correct |
276 ms |
22028 KB |
Output is correct |
28 |
Correct |
258 ms |
22020 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
0 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
0 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
11 ms |
1228 KB |
Output is correct |
37 |
Correct |
254 ms |
22000 KB |
Output is correct |
38 |
Correct |
0 ms |
332 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
11 ms |
1124 KB |
Output is correct |
41 |
Correct |
251 ms |
22052 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
0 ms |
332 KB |
Output is correct |
44 |
Correct |
5 ms |
716 KB |
Output is correct |
45 |
Correct |
118 ms |
12068 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
332 KB |
Output is correct |
48 |
Correct |
0 ms |
332 KB |
Output is correct |
49 |
Correct |
66 ms |
5788 KB |
Output is correct |
50 |
Correct |
257 ms |
22084 KB |
Output is correct |
51 |
Correct |
260 ms |
22160 KB |
Output is correct |
52 |
Correct |
274 ms |
22116 KB |
Output is correct |
53 |
Correct |
127 ms |
12184 KB |
Output is correct |
54 |
Correct |
106 ms |
12056 KB |
Output is correct |
55 |
Correct |
275 ms |
22064 KB |
Output is correct |
56 |
Correct |
276 ms |
22084 KB |
Output is correct |
57 |
Correct |
128 ms |
12172 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
0 ms |
332 KB |
Output is correct |
61 |
Correct |
69 ms |
5784 KB |
Output is correct |
62 |
Correct |
264 ms |
22036 KB |
Output is correct |
63 |
Correct |
261 ms |
22052 KB |
Output is correct |
64 |
Correct |
284 ms |
22004 KB |
Output is correct |
65 |
Correct |
0 ms |
332 KB |
Output is correct |
66 |
Correct |
78 ms |
5768 KB |
Output is correct |
67 |
Correct |
261 ms |
22052 KB |
Output is correct |
68 |
Correct |
254 ms |
21980 KB |
Output is correct |
69 |
Correct |
280 ms |
22084 KB |
Output is correct |
70 |
Correct |
0 ms |
204 KB |
Output is correct |
71 |
Correct |
0 ms |
332 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
66 ms |
5784 KB |
Output is correct |
74 |
Correct |
261 ms |
22164 KB |
Output is correct |
75 |
Correct |
275 ms |
22052 KB |
Output is correct |
76 |
Correct |
268 ms |
22036 KB |
Output is correct |
77 |
Correct |
261 ms |
22200 KB |
Output is correct |
78 |
Correct |
0 ms |
332 KB |
Output is correct |
79 |
Correct |
1 ms |
332 KB |
Output is correct |
80 |
Correct |
5 ms |
716 KB |
Output is correct |
81 |
Correct |
114 ms |
12084 KB |
Output is correct |
82 |
Correct |
0 ms |
204 KB |
Output is correct |
83 |
Correct |
0 ms |
332 KB |
Output is correct |
84 |
Correct |
1 ms |
332 KB |
Output is correct |
85 |
Correct |
67 ms |
5704 KB |
Output is correct |
86 |
Correct |
104 ms |
12056 KB |
Output is correct |
87 |
Correct |
98 ms |
12104 KB |
Output is correct |
88 |
Correct |
106 ms |
12168 KB |
Output is correct |
89 |
Correct |
129 ms |
12184 KB |
Output is correct |
90 |
Correct |
0 ms |
204 KB |
Output is correct |
91 |
Correct |
0 ms |
204 KB |
Output is correct |
92 |
Correct |
5 ms |
716 KB |
Output is correct |
93 |
Correct |
98 ms |
12072 KB |
Output is correct |
94 |
Correct |
0 ms |
204 KB |
Output is correct |
95 |
Correct |
0 ms |
204 KB |
Output is correct |
96 |
Correct |
0 ms |
204 KB |
Output is correct |
97 |
Correct |
24 ms |
3268 KB |
Output is correct |
98 |
Correct |
97 ms |
12064 KB |
Output is correct |
99 |
Correct |
100 ms |
12056 KB |
Output is correct |
100 |
Correct |
97 ms |
12064 KB |
Output is correct |
101 |
Correct |
94 ms |
12064 KB |
Output is correct |
102 |
Correct |
95 ms |
12068 KB |
Output is correct |
103 |
Correct |
94 ms |
12100 KB |
Output is correct |
104 |
Correct |
94 ms |
12064 KB |
Output is correct |
105 |
Correct |
103 ms |
12052 KB |
Output is correct |