#include <bits/stdc++.h>
#ifndef LOCAL
#include "supertrees.h"
#endif
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define Time (clock() * 1.0 / CLOCKS_PER_SEC)
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
return y > x ? (x = y, true) : false;
}
void debug_out() {
cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
cerr << ' ' << A;
debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
for (int i = 0; i < n; ++i)
cerr << a[i] << ' ';
cerr << endl;
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
#define debug(...) 1337
#define mdebug(a, n) 1337
#endif
template<typename T> ostream operator << (ostream& stream, const vector<T>& v) {
for (auto& e : v)
stream << e << ' ';
return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
return stream << p.first << ' ' << p.second;
}
namespace sol {
const int maxN = 1003;
int n;
int p[maxN][maxN];
int comps1, comps2;
int comp1[maxN], comp2[maxN];
int topv[maxN];
vector<int> circle[maxN];
void dfs1(int v) {
comp1[v] = comps1;
for (int u = 0; u < n; ++u)
if (comp1[u] == -1 && p[v][u] >= 1)
dfs1(u);
}
void dfs2(int v) {
comp2[v] = comps2;
for (int u = 0; u < n; ++u)
if (comp2[u] == -1 && p[v][u] == 1)
dfs2(u);
}
}
void build(vector<vector<int>> adj);
int construct(vector<vector<int>> _p) {
using namespace sol;
n = _p.size();
for (int u = 0; u < n; ++u)
for (int v = 0; v < n; ++v) {
p[u][v] = _p[u][v];
if (p[u][v] == 3) {
return 0;
}
}
memset(comp1, 255, sizeof comp1);
comps1 = 0;
for (int v = 0; v < n; ++v) {
if (comp1[v] != -1) continue;
dfs1(v);
++comps1;
}
for (int u = 0; u < n; ++u)
for (int v = 0; v < n; ++v)
if (comp1[u] == comp1[v] && p[u][v] == 0)
return 0;
memset(comp2, 255, sizeof comp2);
comps2 = 0;
for (int v = 0; v < n; ++v) {
if (comp2[v] != -1) continue;
dfs2(v);
++comps2;
}
for (int u = 0; u < n; ++u)
for (int v = 0; v < n; ++v)
if (comp2[u] == comp2[v] && p[u][v] != 1)
return 0;
memset(topv, 255, sizeof topv);
vector<vector<int>> adj(n, vector<int>(n, 0));
auto addEdge = [&](int a, int b) {
adj[a][b] = adj[b][a] = 1;
};
for (int v = 0; v < n; ++v) {
int c2 = comp2[v];
if (topv[c2] == -1)
topv[c2] = v;
else {
addEdge(topv[c2], v);
}
}
for (int c2 = 0; c2 < comps2; ++c2) {
int v = topv[c2];
int c1 = comp1[v];
circle[c1].push_back(v);
}
for (int c1 = 0; c1 < comps1; ++c1) {
if (circle[c1].size() == 1) continue;
if (circle[c1].size() == 2) return 0;
for (int i = 0; i < circle[c1].size(); ++i) {
addEdge(circle[c1][i], circle[c1][(i + 1) % circle[c1].size()]);
}
}
build(adj);
return 1;
}
#ifdef LOCAL
int n;
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());
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() {
freopen("in", "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);
}
}
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);
}
#endif
/*
#ifdef LOCAL
int32_t main() {
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
return 0;
}
#endif*/
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:134:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for (int i = 0; i < circle[c1].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
10 ms |
2064 KB |
Output is correct |
7 |
Correct |
233 ms |
26580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
10 ms |
2064 KB |
Output is correct |
7 |
Correct |
233 ms |
26580 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
10 ms |
2068 KB |
Output is correct |
13 |
Correct |
261 ms |
26776 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
1460 KB |
Output is correct |
17 |
Correct |
102 ms |
12772 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
75 ms |
8196 KB |
Output is correct |
21 |
Correct |
235 ms |
26576 KB |
Output is correct |
22 |
Correct |
236 ms |
26656 KB |
Output is correct |
23 |
Correct |
249 ms |
26568 KB |
Output is correct |
24 |
Correct |
235 ms |
26564 KB |
Output is correct |
25 |
Correct |
104 ms |
12744 KB |
Output is correct |
26 |
Correct |
99 ms |
12764 KB |
Output is correct |
27 |
Correct |
239 ms |
26552 KB |
Output is correct |
28 |
Correct |
234 ms |
26588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
10 ms |
2076 KB |
Output is correct |
9 |
Correct |
234 ms |
26696 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
11 ms |
2044 KB |
Output is correct |
13 |
Correct |
234 ms |
26696 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
5 ms |
1484 KB |
Output is correct |
17 |
Correct |
102 ms |
13504 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
61 ms |
8196 KB |
Output is correct |
22 |
Correct |
237 ms |
26852 KB |
Output is correct |
23 |
Correct |
238 ms |
26760 KB |
Output is correct |
24 |
Correct |
238 ms |
26788 KB |
Output is correct |
25 |
Correct |
99 ms |
17480 KB |
Output is correct |
26 |
Correct |
121 ms |
13448 KB |
Output is correct |
27 |
Correct |
249 ms |
26724 KB |
Output is correct |
28 |
Correct |
240 ms |
26744 KB |
Output is correct |
29 |
Correct |
99 ms |
17512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
71 ms |
8244 KB |
Output is correct |
5 |
Correct |
238 ms |
26644 KB |
Output is correct |
6 |
Correct |
267 ms |
26692 KB |
Output is correct |
7 |
Correct |
276 ms |
26620 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
73 ms |
8208 KB |
Output is correct |
10 |
Correct |
231 ms |
26592 KB |
Output is correct |
11 |
Correct |
286 ms |
26644 KB |
Output is correct |
12 |
Correct |
269 ms |
26748 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
59 ms |
8212 KB |
Output is correct |
17 |
Correct |
235 ms |
26668 KB |
Output is correct |
18 |
Correct |
236 ms |
26628 KB |
Output is correct |
19 |
Correct |
235 ms |
26564 KB |
Output is correct |
20 |
Correct |
236 ms |
26608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
10 ms |
2064 KB |
Output is correct |
7 |
Correct |
233 ms |
26580 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
10 ms |
2068 KB |
Output is correct |
13 |
Correct |
261 ms |
26776 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
1460 KB |
Output is correct |
17 |
Correct |
102 ms |
12772 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
75 ms |
8196 KB |
Output is correct |
21 |
Correct |
235 ms |
26576 KB |
Output is correct |
22 |
Correct |
236 ms |
26656 KB |
Output is correct |
23 |
Correct |
249 ms |
26568 KB |
Output is correct |
24 |
Correct |
235 ms |
26564 KB |
Output is correct |
25 |
Correct |
104 ms |
12744 KB |
Output is correct |
26 |
Correct |
99 ms |
12764 KB |
Output is correct |
27 |
Correct |
239 ms |
26552 KB |
Output is correct |
28 |
Correct |
234 ms |
26588 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
316 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
324 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
10 ms |
2076 KB |
Output is correct |
37 |
Correct |
234 ms |
26696 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
11 ms |
2044 KB |
Output is correct |
41 |
Correct |
234 ms |
26696 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
320 KB |
Output is correct |
44 |
Correct |
5 ms |
1484 KB |
Output is correct |
45 |
Correct |
102 ms |
13504 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
61 ms |
8196 KB |
Output is correct |
50 |
Correct |
237 ms |
26852 KB |
Output is correct |
51 |
Correct |
238 ms |
26760 KB |
Output is correct |
52 |
Correct |
238 ms |
26788 KB |
Output is correct |
53 |
Correct |
99 ms |
17480 KB |
Output is correct |
54 |
Correct |
121 ms |
13448 KB |
Output is correct |
55 |
Correct |
249 ms |
26724 KB |
Output is correct |
56 |
Correct |
240 ms |
26744 KB |
Output is correct |
57 |
Correct |
99 ms |
17512 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
7 ms |
1484 KB |
Output is correct |
61 |
Correct |
105 ms |
13620 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
1 ms |
320 KB |
Output is correct |
65 |
Correct |
60 ms |
8132 KB |
Output is correct |
66 |
Correct |
101 ms |
13512 KB |
Output is correct |
67 |
Correct |
121 ms |
13424 KB |
Output is correct |
68 |
Correct |
107 ms |
13488 KB |
Output is correct |
69 |
Correct |
101 ms |
17512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
10 ms |
2064 KB |
Output is correct |
7 |
Correct |
233 ms |
26580 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
10 ms |
2068 KB |
Output is correct |
13 |
Correct |
261 ms |
26776 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
1460 KB |
Output is correct |
17 |
Correct |
102 ms |
12772 KB |
Output is correct |
18 |
Correct |
1 ms |
328 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
75 ms |
8196 KB |
Output is correct |
21 |
Correct |
235 ms |
26576 KB |
Output is correct |
22 |
Correct |
236 ms |
26656 KB |
Output is correct |
23 |
Correct |
249 ms |
26568 KB |
Output is correct |
24 |
Correct |
235 ms |
26564 KB |
Output is correct |
25 |
Correct |
104 ms |
12744 KB |
Output is correct |
26 |
Correct |
99 ms |
12764 KB |
Output is correct |
27 |
Correct |
239 ms |
26552 KB |
Output is correct |
28 |
Correct |
234 ms |
26588 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
316 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
324 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
10 ms |
2076 KB |
Output is correct |
37 |
Correct |
234 ms |
26696 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
11 ms |
2044 KB |
Output is correct |
41 |
Correct |
234 ms |
26696 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
320 KB |
Output is correct |
44 |
Correct |
5 ms |
1484 KB |
Output is correct |
45 |
Correct |
102 ms |
13504 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
61 ms |
8196 KB |
Output is correct |
50 |
Correct |
237 ms |
26852 KB |
Output is correct |
51 |
Correct |
238 ms |
26760 KB |
Output is correct |
52 |
Correct |
238 ms |
26788 KB |
Output is correct |
53 |
Correct |
99 ms |
17480 KB |
Output is correct |
54 |
Correct |
121 ms |
13448 KB |
Output is correct |
55 |
Correct |
249 ms |
26724 KB |
Output is correct |
56 |
Correct |
240 ms |
26744 KB |
Output is correct |
57 |
Correct |
99 ms |
17512 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
71 ms |
8244 KB |
Output is correct |
62 |
Correct |
238 ms |
26644 KB |
Output is correct |
63 |
Correct |
267 ms |
26692 KB |
Output is correct |
64 |
Correct |
276 ms |
26620 KB |
Output is correct |
65 |
Correct |
1 ms |
332 KB |
Output is correct |
66 |
Correct |
73 ms |
8208 KB |
Output is correct |
67 |
Correct |
231 ms |
26592 KB |
Output is correct |
68 |
Correct |
286 ms |
26644 KB |
Output is correct |
69 |
Correct |
269 ms |
26748 KB |
Output is correct |
70 |
Correct |
1 ms |
332 KB |
Output is correct |
71 |
Correct |
1 ms |
332 KB |
Output is correct |
72 |
Correct |
1 ms |
324 KB |
Output is correct |
73 |
Correct |
59 ms |
8212 KB |
Output is correct |
74 |
Correct |
235 ms |
26668 KB |
Output is correct |
75 |
Correct |
236 ms |
26628 KB |
Output is correct |
76 |
Correct |
235 ms |
26564 KB |
Output is correct |
77 |
Correct |
236 ms |
26608 KB |
Output is correct |
78 |
Correct |
1 ms |
332 KB |
Output is correct |
79 |
Correct |
1 ms |
332 KB |
Output is correct |
80 |
Correct |
7 ms |
1484 KB |
Output is correct |
81 |
Correct |
105 ms |
13620 KB |
Output is correct |
82 |
Correct |
1 ms |
332 KB |
Output is correct |
83 |
Correct |
1 ms |
332 KB |
Output is correct |
84 |
Correct |
1 ms |
320 KB |
Output is correct |
85 |
Correct |
60 ms |
8132 KB |
Output is correct |
86 |
Correct |
101 ms |
13512 KB |
Output is correct |
87 |
Correct |
121 ms |
13424 KB |
Output is correct |
88 |
Correct |
107 ms |
13488 KB |
Output is correct |
89 |
Correct |
101 ms |
17512 KB |
Output is correct |
90 |
Correct |
1 ms |
332 KB |
Output is correct |
91 |
Correct |
1 ms |
332 KB |
Output is correct |
92 |
Correct |
6 ms |
716 KB |
Output is correct |
93 |
Correct |
108 ms |
9548 KB |
Output is correct |
94 |
Correct |
1 ms |
332 KB |
Output is correct |
95 |
Correct |
1 ms |
332 KB |
Output is correct |
96 |
Correct |
1 ms |
332 KB |
Output is correct |
97 |
Correct |
23 ms |
2828 KB |
Output is correct |
98 |
Correct |
97 ms |
9788 KB |
Output is correct |
99 |
Correct |
103 ms |
10164 KB |
Output is correct |
100 |
Correct |
106 ms |
9884 KB |
Output is correct |
101 |
Correct |
97 ms |
10856 KB |
Output is correct |
102 |
Correct |
97 ms |
9536 KB |
Output is correct |
103 |
Correct |
114 ms |
9656 KB |
Output is correct |
104 |
Correct |
108 ms |
9516 KB |
Output is correct |
105 |
Correct |
101 ms |
9540 KB |
Output is correct |