// Author: Kajetan Ramsza
#include "bits/stdc++.h"
#include "supertrees.h"
using namespace std;
#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename F, typename S> ostream& operator<<(ostream& os, const pair<F, S> &p) { return os<<"("<<p.first<<", "<<p.second<<")"; }
template<typename T> ostream &operator<<(ostream & os, const vector<T> &v) { os << "{"; typename vector< T > :: const_iterator it;
for( it = v.begin(); it != v.end(); it++ ) { if( it != v.begin() ) os << ", "; os << *it; } return os << "}"; }
void dbg_out() { cerr<<'\n'; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr<<' '<<H; dbg_out(T...); }
#ifdef DEBUG
#define dbg(...) cerr<<"(" << #__VA_ARGS__ <<"):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
struct DSU {
vi dsu, siz;
DSU(int n) : dsu(n), siz(n, 1) {
iota(all(dsu), 0);
}
int find(int x) {
if(dsu[x] == x) return x;
return dsu[x] = find(dsu[x]);
}
void merge(int a, int b) {
a = find(a); b = find(b);
if(a == b) return;
if(siz[a] < siz[b]) swap(a, b);
else if(siz[a] == siz[b]) siz[a]++;
dsu[b] = a;
}
bool same(int a, int b) {
return find(a) == find(b);
}
};
int n;
vector<vi> answer;
pair<bool, vi> find_connected(vector<vi> &p) {
DSU dsu(n);
rep(i,0,n) rep(j,0,n)
if(p[i][j] > 0) dsu.merge(i, j);
rep(i,0,n) rep(j,0,n)
if(p[i][j] == 0 && dsu.same(i, j))
return {false, {}};
return {true, dsu.dsu};
}
pair<bool, vi> find_one_connected(vector<vi> &p) {
DSU dsu(n);
rep(i,0,n) rep(j,0,n)
if(p[i][j] == 1) dsu.merge(i, j);
rep(i,0,n) rep(j,0,n)
if(p[i][j] != 1 && dsu.same(i, j))
return {false, {}};
return {true, dsu.dsu};
}
int construct(vector<vi> p) {
n = p.size();
answer.assign(n, vi(n, 0));
rep(i,0,n) rep(j,0,n)
if(p[i][j] == 3) return 0;
auto [correct, conn] = find_connected(p);
if(!correct) return 0;
auto [one_correct, one_conn] = find_one_connected(p);
if(!one_correct) return 0;
rep(i,0,n) {
if(one_conn[i] == i) continue;
answer[i][one_conn[i]] = 1;
answer[one_conn[i]][i] = 1;
}
vector<vi> cycles(n);
rep(i,0,n) if(one_conn[i] == i)
cycles[conn[i]].push_back(i);
rep(i,0,n) {
if(sz(cycles[i]) == 2) return 0;
rep(j,1,sz(cycles[i])) {
answer[cycles[i][j]][cycles[i][j-1]] = true;
answer[cycles[i][j-1]][cycles[i][j]] = true;
}
if(sz(cycles[i]) >= 3) {
answer[cycles[i].back()][cycles[i][0]] = true;
answer[cycles[i][0]][cycles[i].back()] = true;
}
}
build(answer);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
130 ms |
23484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
130 ms |
23484 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
368 KB |
Output is correct |
12 |
Correct |
5 ms |
1188 KB |
Output is correct |
13 |
Correct |
118 ms |
23380 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
64 ms |
13460 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
6284 KB |
Output is correct |
21 |
Correct |
124 ms |
23376 KB |
Output is correct |
22 |
Correct |
120 ms |
23376 KB |
Output is correct |
23 |
Correct |
131 ms |
23292 KB |
Output is correct |
24 |
Correct |
135 ms |
23380 KB |
Output is correct |
25 |
Correct |
52 ms |
13392 KB |
Output is correct |
26 |
Correct |
61 ms |
13392 KB |
Output is correct |
27 |
Correct |
127 ms |
23376 KB |
Output is correct |
28 |
Correct |
119 ms |
23380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
1372 KB |
Output is correct |
9 |
Correct |
125 ms |
23496 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
1372 KB |
Output is correct |
13 |
Correct |
116 ms |
23276 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
996 KB |
Output is correct |
17 |
Correct |
64 ms |
13396 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
432 KB |
Output is correct |
21 |
Correct |
30 ms |
6296 KB |
Output is correct |
22 |
Correct |
121 ms |
23432 KB |
Output is correct |
23 |
Correct |
118 ms |
23380 KB |
Output is correct |
24 |
Correct |
127 ms |
23376 KB |
Output is correct |
25 |
Correct |
61 ms |
13372 KB |
Output is correct |
26 |
Correct |
54 ms |
13396 KB |
Output is correct |
27 |
Correct |
122 ms |
23376 KB |
Output is correct |
28 |
Correct |
130 ms |
23276 KB |
Output is correct |
29 |
Correct |
57 ms |
13392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
43 ms |
6268 KB |
Output is correct |
5 |
Correct |
126 ms |
23380 KB |
Output is correct |
6 |
Correct |
121 ms |
23376 KB |
Output is correct |
7 |
Correct |
127 ms |
23380 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
30 ms |
6400 KB |
Output is correct |
10 |
Correct |
125 ms |
23380 KB |
Output is correct |
11 |
Correct |
123 ms |
23376 KB |
Output is correct |
12 |
Correct |
140 ms |
23380 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
31 ms |
6236 KB |
Output is correct |
17 |
Correct |
125 ms |
23384 KB |
Output is correct |
18 |
Correct |
171 ms |
23376 KB |
Output is correct |
19 |
Correct |
123 ms |
23380 KB |
Output is correct |
20 |
Correct |
120 ms |
23424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
130 ms |
23484 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
368 KB |
Output is correct |
12 |
Correct |
5 ms |
1188 KB |
Output is correct |
13 |
Correct |
118 ms |
23380 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
64 ms |
13460 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
6284 KB |
Output is correct |
21 |
Correct |
124 ms |
23376 KB |
Output is correct |
22 |
Correct |
120 ms |
23376 KB |
Output is correct |
23 |
Correct |
131 ms |
23292 KB |
Output is correct |
24 |
Correct |
135 ms |
23380 KB |
Output is correct |
25 |
Correct |
52 ms |
13392 KB |
Output is correct |
26 |
Correct |
61 ms |
13392 KB |
Output is correct |
27 |
Correct |
127 ms |
23376 KB |
Output is correct |
28 |
Correct |
119 ms |
23380 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
5 ms |
1372 KB |
Output is correct |
37 |
Correct |
125 ms |
23496 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
6 ms |
1372 KB |
Output is correct |
41 |
Correct |
116 ms |
23276 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
996 KB |
Output is correct |
45 |
Correct |
64 ms |
13396 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
432 KB |
Output is correct |
49 |
Correct |
30 ms |
6296 KB |
Output is correct |
50 |
Correct |
121 ms |
23432 KB |
Output is correct |
51 |
Correct |
118 ms |
23380 KB |
Output is correct |
52 |
Correct |
127 ms |
23376 KB |
Output is correct |
53 |
Correct |
61 ms |
13372 KB |
Output is correct |
54 |
Correct |
54 ms |
13396 KB |
Output is correct |
55 |
Correct |
122 ms |
23376 KB |
Output is correct |
56 |
Correct |
130 ms |
23276 KB |
Output is correct |
57 |
Correct |
57 ms |
13392 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
856 KB |
Output is correct |
61 |
Correct |
63 ms |
13536 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
436 KB |
Output is correct |
65 |
Correct |
37 ms |
6232 KB |
Output is correct |
66 |
Correct |
57 ms |
13448 KB |
Output is correct |
67 |
Correct |
57 ms |
13392 KB |
Output is correct |
68 |
Correct |
58 ms |
13316 KB |
Output is correct |
69 |
Correct |
57 ms |
13572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1372 KB |
Output is correct |
7 |
Correct |
130 ms |
23484 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
368 KB |
Output is correct |
12 |
Correct |
5 ms |
1188 KB |
Output is correct |
13 |
Correct |
118 ms |
23380 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
64 ms |
13460 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
6284 KB |
Output is correct |
21 |
Correct |
124 ms |
23376 KB |
Output is correct |
22 |
Correct |
120 ms |
23376 KB |
Output is correct |
23 |
Correct |
131 ms |
23292 KB |
Output is correct |
24 |
Correct |
135 ms |
23380 KB |
Output is correct |
25 |
Correct |
52 ms |
13392 KB |
Output is correct |
26 |
Correct |
61 ms |
13392 KB |
Output is correct |
27 |
Correct |
127 ms |
23376 KB |
Output is correct |
28 |
Correct |
119 ms |
23380 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
5 ms |
1372 KB |
Output is correct |
37 |
Correct |
125 ms |
23496 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
6 ms |
1372 KB |
Output is correct |
41 |
Correct |
116 ms |
23276 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
996 KB |
Output is correct |
45 |
Correct |
64 ms |
13396 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
432 KB |
Output is correct |
49 |
Correct |
30 ms |
6296 KB |
Output is correct |
50 |
Correct |
121 ms |
23432 KB |
Output is correct |
51 |
Correct |
118 ms |
23380 KB |
Output is correct |
52 |
Correct |
127 ms |
23376 KB |
Output is correct |
53 |
Correct |
61 ms |
13372 KB |
Output is correct |
54 |
Correct |
54 ms |
13396 KB |
Output is correct |
55 |
Correct |
122 ms |
23376 KB |
Output is correct |
56 |
Correct |
130 ms |
23276 KB |
Output is correct |
57 |
Correct |
57 ms |
13392 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
43 ms |
6268 KB |
Output is correct |
62 |
Correct |
126 ms |
23380 KB |
Output is correct |
63 |
Correct |
121 ms |
23376 KB |
Output is correct |
64 |
Correct |
127 ms |
23380 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
30 ms |
6400 KB |
Output is correct |
67 |
Correct |
125 ms |
23380 KB |
Output is correct |
68 |
Correct |
123 ms |
23376 KB |
Output is correct |
69 |
Correct |
140 ms |
23380 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
31 ms |
6236 KB |
Output is correct |
74 |
Correct |
125 ms |
23384 KB |
Output is correct |
75 |
Correct |
171 ms |
23376 KB |
Output is correct |
76 |
Correct |
123 ms |
23380 KB |
Output is correct |
77 |
Correct |
120 ms |
23424 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
3 ms |
856 KB |
Output is correct |
81 |
Correct |
63 ms |
13536 KB |
Output is correct |
82 |
Correct |
0 ms |
344 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
436 KB |
Output is correct |
85 |
Correct |
37 ms |
6232 KB |
Output is correct |
86 |
Correct |
57 ms |
13448 KB |
Output is correct |
87 |
Correct |
57 ms |
13392 KB |
Output is correct |
88 |
Correct |
58 ms |
13316 KB |
Output is correct |
89 |
Correct |
57 ms |
13572 KB |
Output is correct |
90 |
Correct |
0 ms |
348 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
2 ms |
856 KB |
Output is correct |
93 |
Correct |
52 ms |
13396 KB |
Output is correct |
94 |
Correct |
0 ms |
348 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
15 ms |
3712 KB |
Output is correct |
98 |
Correct |
55 ms |
14220 KB |
Output is correct |
99 |
Correct |
57 ms |
14160 KB |
Output is correct |
100 |
Correct |
51 ms |
14068 KB |
Output is correct |
101 |
Correct |
50 ms |
14160 KB |
Output is correct |
102 |
Correct |
52 ms |
14164 KB |
Output is correct |
103 |
Correct |
59 ms |
13992 KB |
Output is correct |
104 |
Correct |
52 ms |
14160 KB |
Output is correct |
105 |
Correct |
56 ms |
14160 KB |
Output is correct |