// 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));
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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
1108 KB |
Output is correct |
7 |
Correct |
118 ms |
22028 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
1108 KB |
Output is correct |
7 |
Correct |
118 ms |
22028 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1116 KB |
Output is correct |
13 |
Correct |
115 ms |
22084 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 |
860 KB |
Output is correct |
17 |
Correct |
61 ms |
12060 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
37 ms |
5920 KB |
Output is correct |
21 |
Correct |
123 ms |
22096 KB |
Output is correct |
22 |
Correct |
124 ms |
22040 KB |
Output is correct |
23 |
Correct |
138 ms |
22024 KB |
Output is correct |
24 |
Correct |
144 ms |
22016 KB |
Output is correct |
25 |
Correct |
61 ms |
12116 KB |
Output is correct |
26 |
Correct |
55 ms |
12116 KB |
Output is correct |
27 |
Correct |
131 ms |
22144 KB |
Output is correct |
28 |
Correct |
118 ms |
22100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
6 ms |
1372 KB |
Output is correct |
9 |
Correct |
118 ms |
24148 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
141 ms |
24072 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
932 KB |
Output is correct |
17 |
Correct |
84 ms |
14088 KB |
Output is correct |
18 |
Correct |
1 ms |
436 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
31 ms |
6312 KB |
Output is correct |
22 |
Correct |
134 ms |
24056 KB |
Output is correct |
23 |
Correct |
135 ms |
24072 KB |
Output is correct |
24 |
Correct |
180 ms |
24072 KB |
Output is correct |
25 |
Correct |
94 ms |
14164 KB |
Output is correct |
26 |
Correct |
56 ms |
14216 KB |
Output is correct |
27 |
Correct |
132 ms |
24056 KB |
Output is correct |
28 |
Correct |
137 ms |
23928 KB |
Output is correct |
29 |
Correct |
57 ms |
14252 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 |
344 KB |
Output is correct |
4 |
Correct |
37 ms |
5724 KB |
Output is correct |
5 |
Correct |
140 ms |
22140 KB |
Output is correct |
6 |
Correct |
135 ms |
22100 KB |
Output is correct |
7 |
Correct |
152 ms |
22028 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
33 ms |
5776 KB |
Output is correct |
10 |
Correct |
130 ms |
22040 KB |
Output is correct |
11 |
Correct |
119 ms |
22040 KB |
Output is correct |
12 |
Correct |
128 ms |
22036 KB |
Output is correct |
13 |
Correct |
0 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 |
29 ms |
5724 KB |
Output is correct |
17 |
Correct |
124 ms |
22096 KB |
Output is correct |
18 |
Correct |
121 ms |
22096 KB |
Output is correct |
19 |
Correct |
124 ms |
22044 KB |
Output is correct |
20 |
Correct |
121 ms |
22096 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
1108 KB |
Output is correct |
7 |
Correct |
118 ms |
22028 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1116 KB |
Output is correct |
13 |
Correct |
115 ms |
22084 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 |
860 KB |
Output is correct |
17 |
Correct |
61 ms |
12060 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
37 ms |
5920 KB |
Output is correct |
21 |
Correct |
123 ms |
22096 KB |
Output is correct |
22 |
Correct |
124 ms |
22040 KB |
Output is correct |
23 |
Correct |
138 ms |
22024 KB |
Output is correct |
24 |
Correct |
144 ms |
22016 KB |
Output is correct |
25 |
Correct |
61 ms |
12116 KB |
Output is correct |
26 |
Correct |
55 ms |
12116 KB |
Output is correct |
27 |
Correct |
131 ms |
22144 KB |
Output is correct |
28 |
Correct |
118 ms |
22100 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 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 |
6 ms |
1372 KB |
Output is correct |
37 |
Correct |
118 ms |
24148 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1372 KB |
Output is correct |
41 |
Correct |
141 ms |
24072 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
4 ms |
932 KB |
Output is correct |
45 |
Correct |
84 ms |
14088 KB |
Output is correct |
46 |
Correct |
1 ms |
436 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
31 ms |
6312 KB |
Output is correct |
50 |
Correct |
134 ms |
24056 KB |
Output is correct |
51 |
Correct |
135 ms |
24072 KB |
Output is correct |
52 |
Correct |
180 ms |
24072 KB |
Output is correct |
53 |
Correct |
94 ms |
14164 KB |
Output is correct |
54 |
Correct |
56 ms |
14216 KB |
Output is correct |
55 |
Correct |
132 ms |
24056 KB |
Output is correct |
56 |
Correct |
137 ms |
23928 KB |
Output is correct |
57 |
Correct |
57 ms |
14252 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
964 KB |
Output is correct |
61 |
Correct |
59 ms |
13992 KB |
Output is correct |
62 |
Correct |
0 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
344 KB |
Output is correct |
64 |
Correct |
0 ms |
344 KB |
Output is correct |
65 |
Correct |
31 ms |
6264 KB |
Output is correct |
66 |
Correct |
54 ms |
14216 KB |
Output is correct |
67 |
Correct |
53 ms |
14164 KB |
Output is correct |
68 |
Correct |
55 ms |
14420 KB |
Output is correct |
69 |
Correct |
56 ms |
14088 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 |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
1108 KB |
Output is correct |
7 |
Correct |
118 ms |
22028 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1116 KB |
Output is correct |
13 |
Correct |
115 ms |
22084 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 |
860 KB |
Output is correct |
17 |
Correct |
61 ms |
12060 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
37 ms |
5920 KB |
Output is correct |
21 |
Correct |
123 ms |
22096 KB |
Output is correct |
22 |
Correct |
124 ms |
22040 KB |
Output is correct |
23 |
Correct |
138 ms |
22024 KB |
Output is correct |
24 |
Correct |
144 ms |
22016 KB |
Output is correct |
25 |
Correct |
61 ms |
12116 KB |
Output is correct |
26 |
Correct |
55 ms |
12116 KB |
Output is correct |
27 |
Correct |
131 ms |
22144 KB |
Output is correct |
28 |
Correct |
118 ms |
22100 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 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 |
6 ms |
1372 KB |
Output is correct |
37 |
Correct |
118 ms |
24148 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1372 KB |
Output is correct |
41 |
Correct |
141 ms |
24072 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
4 ms |
932 KB |
Output is correct |
45 |
Correct |
84 ms |
14088 KB |
Output is correct |
46 |
Correct |
1 ms |
436 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
31 ms |
6312 KB |
Output is correct |
50 |
Correct |
134 ms |
24056 KB |
Output is correct |
51 |
Correct |
135 ms |
24072 KB |
Output is correct |
52 |
Correct |
180 ms |
24072 KB |
Output is correct |
53 |
Correct |
94 ms |
14164 KB |
Output is correct |
54 |
Correct |
56 ms |
14216 KB |
Output is correct |
55 |
Correct |
132 ms |
24056 KB |
Output is correct |
56 |
Correct |
137 ms |
23928 KB |
Output is correct |
57 |
Correct |
57 ms |
14252 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
344 KB |
Output is correct |
61 |
Correct |
37 ms |
5724 KB |
Output is correct |
62 |
Correct |
140 ms |
22140 KB |
Output is correct |
63 |
Correct |
135 ms |
22100 KB |
Output is correct |
64 |
Correct |
152 ms |
22028 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
33 ms |
5776 KB |
Output is correct |
67 |
Correct |
130 ms |
22040 KB |
Output is correct |
68 |
Correct |
119 ms |
22040 KB |
Output is correct |
69 |
Correct |
128 ms |
22036 KB |
Output is correct |
70 |
Correct |
0 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 |
29 ms |
5724 KB |
Output is correct |
74 |
Correct |
124 ms |
22096 KB |
Output is correct |
75 |
Correct |
121 ms |
22096 KB |
Output is correct |
76 |
Correct |
124 ms |
22044 KB |
Output is correct |
77 |
Correct |
121 ms |
22096 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
1 ms |
348 KB |
Output is correct |
80 |
Correct |
3 ms |
964 KB |
Output is correct |
81 |
Correct |
59 ms |
13992 KB |
Output is correct |
82 |
Correct |
0 ms |
344 KB |
Output is correct |
83 |
Correct |
0 ms |
344 KB |
Output is correct |
84 |
Correct |
0 ms |
344 KB |
Output is correct |
85 |
Correct |
31 ms |
6264 KB |
Output is correct |
86 |
Correct |
54 ms |
14216 KB |
Output is correct |
87 |
Correct |
53 ms |
14164 KB |
Output is correct |
88 |
Correct |
55 ms |
14420 KB |
Output is correct |
89 |
Correct |
56 ms |
14088 KB |
Output is correct |
90 |
Correct |
1 ms |
348 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
3 ms |
860 KB |
Output is correct |
93 |
Correct |
59 ms |
14084 KB |
Output is correct |
94 |
Incorrect |
0 ms |
348 KB |
Answer gives possible 1 while actual possible 0 |
95 |
Halted |
0 ms |
0 KB |
- |