#include "supertrees.h"
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll,ll>;
#define ln "\n"
#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define pb emplace_back
#define mp std::make_pair
#define mtp std::make_tuple
#define all(a) a.begin(),a.end()
int construct(std::vector<std::vector<int>> p) {
int N = p.size();
vector<vector<int>> ans(N, vector<int>(N));
vector<bool> flag(N);
auto tree = [&](vi v){
rep(i,1,v.size()){
ans[v[i-1]][v[i]] = ans[v[i]][v[i-1]] = 1;
}
rep(i,0,v.size()){
rep(j,i+1,v.size()){
if(p[v[i]][v[j]] != 1) return true;
}
}
return false;
};
auto loop = [&](vi v){
if(v.size() == 2) return true;
rep(i,0,v.size()){
ans[v[(i+1)%v.size()]][v[i]] = ans[v[i]][v[(i+1)%v.size()]] = 1;
}
rep(i,0,v.size()){
rep(j,i+1,v.size()){
if(p[v[i]][v[j]] != 2) return true;
}
}
return false;
};
vector<bool> mem_flag(N);
rep(i,0,N){
if(flag[i]) continue;
flag[i] = true;
std::queue<ll> que;
que.push(i);
vi v;
while(!que.empty()){
ll now = que.front(); que.pop();
v.pb(now);
rep(next, 0, N){
if(p[now][next]){
if(!flag[next]){
flag[next] = true;
que.push(next);
}
}
}
}
vi cnt(4);
for(auto el: v){
rep(j,0,N){
if(el == j) continue;
cnt[p[el][j]]++;
}
}
if(cnt[2] == 0){
if(tree(v)) return 0;
continue;
}
if(cnt[1] == 0){
if(loop(v)) return 0;
continue;
}
vector<pii> data;
for(auto el: v){
ll cnt = 0;
rep(j,0,N){
if(el == j) continue;
if(p[el][j] == 1) cnt++;
}
data.pb(mp(cnt, el));
}
sort(all(data));
reverse(all(data));
vi par;
vii member;
for(auto el: data){
if(mem_flag[el.second]) continue;
mem_flag[el.second] = true;
vi mb;
par.pb(el.second);
rep(j,0,N){
if(el.second == j) continue;
if(p[el.second][j] == 1){
mb.pb(j);
if(mem_flag[j]) return 0;
mem_flag[j] = true;
}
}
member.pb(mb);
}
if(par.size() <= 2) return 0;
rep(j,0,par.size()){
ans[par[j]][par[(j+1)%par.size()]] = ans[par[(j+1)%par.size()]][par[j]] = 1;
}
rep(j,0,par.size()){
for(auto el: member[j]) ans[par[j]][el] = ans[el][par[j]] = 1;
}
rep(j,0,par.size()){
rep(k,j+1,par.size()){
if(p[par[j]][par[k]] != 2) return 0;
for(auto el1: member[j]){
for(auto el2: member[k]){
if(p[el1][el2] != 2) return 0;
}
}
}
for(auto el1: member[j]){
for(auto el2: member[j]){
if(el1 == el2) continue;
if(p[el1][el2] != 1) return 0;
}
}
}
}
build(ans);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1120 KB |
Output is correct |
7 |
Correct |
163 ms |
21972 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1120 KB |
Output is correct |
7 |
Correct |
163 ms |
21972 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1236 KB |
Output is correct |
13 |
Correct |
152 ms |
22100 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 |
852 KB |
Output is correct |
17 |
Correct |
71 ms |
13140 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
40 ms |
6236 KB |
Output is correct |
21 |
Correct |
160 ms |
23028 KB |
Output is correct |
22 |
Correct |
160 ms |
23008 KB |
Output is correct |
23 |
Correct |
201 ms |
23104 KB |
Output is correct |
24 |
Correct |
149 ms |
23120 KB |
Output is correct |
25 |
Correct |
97 ms |
13248 KB |
Output is correct |
26 |
Correct |
59 ms |
13164 KB |
Output is correct |
27 |
Correct |
164 ms |
23172 KB |
Output is correct |
28 |
Correct |
174 ms |
23076 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 |
1 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 |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
7 ms |
1272 KB |
Output is correct |
9 |
Correct |
161 ms |
23300 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1236 KB |
Output is correct |
13 |
Correct |
157 ms |
23448 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
3 ms |
820 KB |
Output is correct |
17 |
Correct |
70 ms |
14072 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
40 ms |
6260 KB |
Output is correct |
22 |
Correct |
158 ms |
23564 KB |
Output is correct |
23 |
Correct |
162 ms |
23596 KB |
Output is correct |
24 |
Correct |
169 ms |
23712 KB |
Output is correct |
25 |
Correct |
68 ms |
14096 KB |
Output is correct |
26 |
Correct |
107 ms |
14064 KB |
Output is correct |
27 |
Correct |
233 ms |
23992 KB |
Output is correct |
28 |
Correct |
161 ms |
23972 KB |
Output is correct |
29 |
Correct |
60 ms |
14068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
50 ms |
6140 KB |
Output is correct |
5 |
Correct |
157 ms |
23920 KB |
Output is correct |
6 |
Correct |
157 ms |
24012 KB |
Output is correct |
7 |
Correct |
160 ms |
24012 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
37 ms |
6196 KB |
Output is correct |
10 |
Correct |
170 ms |
23992 KB |
Output is correct |
11 |
Correct |
162 ms |
23928 KB |
Output is correct |
12 |
Correct |
157 ms |
23900 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
45 ms |
6220 KB |
Output is correct |
17 |
Correct |
160 ms |
23948 KB |
Output is correct |
18 |
Correct |
168 ms |
23904 KB |
Output is correct |
19 |
Correct |
172 ms |
24020 KB |
Output is correct |
20 |
Correct |
159 ms |
23984 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1120 KB |
Output is correct |
7 |
Correct |
163 ms |
21972 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1236 KB |
Output is correct |
13 |
Correct |
152 ms |
22100 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 |
852 KB |
Output is correct |
17 |
Correct |
71 ms |
13140 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
40 ms |
6236 KB |
Output is correct |
21 |
Correct |
160 ms |
23028 KB |
Output is correct |
22 |
Correct |
160 ms |
23008 KB |
Output is correct |
23 |
Correct |
201 ms |
23104 KB |
Output is correct |
24 |
Correct |
149 ms |
23120 KB |
Output is correct |
25 |
Correct |
97 ms |
13248 KB |
Output is correct |
26 |
Correct |
59 ms |
13164 KB |
Output is correct |
27 |
Correct |
164 ms |
23172 KB |
Output is correct |
28 |
Correct |
174 ms |
23076 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
300 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1272 KB |
Output is correct |
37 |
Correct |
161 ms |
23300 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1236 KB |
Output is correct |
41 |
Correct |
157 ms |
23448 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
820 KB |
Output is correct |
45 |
Correct |
70 ms |
14072 KB |
Output is correct |
46 |
Correct |
1 ms |
300 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
300 KB |
Output is correct |
49 |
Correct |
40 ms |
6260 KB |
Output is correct |
50 |
Correct |
158 ms |
23564 KB |
Output is correct |
51 |
Correct |
162 ms |
23596 KB |
Output is correct |
52 |
Correct |
169 ms |
23712 KB |
Output is correct |
53 |
Correct |
68 ms |
14096 KB |
Output is correct |
54 |
Correct |
107 ms |
14064 KB |
Output is correct |
55 |
Correct |
233 ms |
23992 KB |
Output is correct |
56 |
Correct |
161 ms |
23972 KB |
Output is correct |
57 |
Correct |
60 ms |
14068 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
4 ms |
852 KB |
Output is correct |
61 |
Correct |
71 ms |
14140 KB |
Output is correct |
62 |
Correct |
2 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
38 ms |
6264 KB |
Output is correct |
66 |
Correct |
59 ms |
14028 KB |
Output is correct |
67 |
Correct |
61 ms |
14080 KB |
Output is correct |
68 |
Correct |
59 ms |
14028 KB |
Output is correct |
69 |
Correct |
57 ms |
14068 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
7 ms |
1120 KB |
Output is correct |
7 |
Correct |
163 ms |
21972 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
7 ms |
1236 KB |
Output is correct |
13 |
Correct |
152 ms |
22100 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 |
852 KB |
Output is correct |
17 |
Correct |
71 ms |
13140 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
40 ms |
6236 KB |
Output is correct |
21 |
Correct |
160 ms |
23028 KB |
Output is correct |
22 |
Correct |
160 ms |
23008 KB |
Output is correct |
23 |
Correct |
201 ms |
23104 KB |
Output is correct |
24 |
Correct |
149 ms |
23120 KB |
Output is correct |
25 |
Correct |
97 ms |
13248 KB |
Output is correct |
26 |
Correct |
59 ms |
13164 KB |
Output is correct |
27 |
Correct |
164 ms |
23172 KB |
Output is correct |
28 |
Correct |
174 ms |
23076 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
300 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
1272 KB |
Output is correct |
37 |
Correct |
161 ms |
23300 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
7 ms |
1236 KB |
Output is correct |
41 |
Correct |
157 ms |
23448 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
1 ms |
212 KB |
Output is correct |
44 |
Correct |
3 ms |
820 KB |
Output is correct |
45 |
Correct |
70 ms |
14072 KB |
Output is correct |
46 |
Correct |
1 ms |
300 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
300 KB |
Output is correct |
49 |
Correct |
40 ms |
6260 KB |
Output is correct |
50 |
Correct |
158 ms |
23564 KB |
Output is correct |
51 |
Correct |
162 ms |
23596 KB |
Output is correct |
52 |
Correct |
169 ms |
23712 KB |
Output is correct |
53 |
Correct |
68 ms |
14096 KB |
Output is correct |
54 |
Correct |
107 ms |
14064 KB |
Output is correct |
55 |
Correct |
233 ms |
23992 KB |
Output is correct |
56 |
Correct |
161 ms |
23972 KB |
Output is correct |
57 |
Correct |
60 ms |
14068 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
304 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
50 ms |
6140 KB |
Output is correct |
62 |
Correct |
157 ms |
23920 KB |
Output is correct |
63 |
Correct |
157 ms |
24012 KB |
Output is correct |
64 |
Correct |
160 ms |
24012 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
37 ms |
6196 KB |
Output is correct |
67 |
Correct |
170 ms |
23992 KB |
Output is correct |
68 |
Correct |
162 ms |
23928 KB |
Output is correct |
69 |
Correct |
157 ms |
23900 KB |
Output is correct |
70 |
Correct |
0 ms |
212 KB |
Output is correct |
71 |
Correct |
0 ms |
296 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
45 ms |
6220 KB |
Output is correct |
74 |
Correct |
160 ms |
23948 KB |
Output is correct |
75 |
Correct |
168 ms |
23904 KB |
Output is correct |
76 |
Correct |
172 ms |
24020 KB |
Output is correct |
77 |
Correct |
159 ms |
23984 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
4 ms |
852 KB |
Output is correct |
81 |
Correct |
71 ms |
14140 KB |
Output is correct |
82 |
Correct |
2 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
0 ms |
212 KB |
Output is correct |
85 |
Correct |
38 ms |
6264 KB |
Output is correct |
86 |
Correct |
59 ms |
14028 KB |
Output is correct |
87 |
Correct |
61 ms |
14080 KB |
Output is correct |
88 |
Correct |
59 ms |
14028 KB |
Output is correct |
89 |
Correct |
57 ms |
14068 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 |
852 KB |
Output is correct |
93 |
Correct |
69 ms |
14064 KB |
Output is correct |
94 |
Correct |
1 ms |
296 KB |
Output is correct |
95 |
Correct |
1 ms |
296 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
19 ms |
3668 KB |
Output is correct |
98 |
Correct |
59 ms |
14076 KB |
Output is correct |
99 |
Correct |
62 ms |
14076 KB |
Output is correct |
100 |
Correct |
62 ms |
14076 KB |
Output is correct |
101 |
Correct |
58 ms |
14072 KB |
Output is correct |
102 |
Correct |
98 ms |
14116 KB |
Output is correct |
103 |
Correct |
75 ms |
14068 KB |
Output is correct |
104 |
Correct |
60 ms |
14064 KB |
Output is correct |
105 |
Correct |
74 ms |
14080 KB |
Output is correct |