#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 1005;
template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }
vector<pi> g[mxN];
int vis[mxN];
vi cur1, cur2;
void dfs2(int cur){
vis[cur] = 1;
cur2.pb(cur);
for(pi x : g[cur]) if(x.se == 2 && !vis[x.fi]) dfs2(x.fi);
}
void dfs1(int cur){
vis[cur] = 1;
cur1.pb(cur);
for(pi x : g[cur]) if(x.se == 1 && !vis[x.fi]) dfs1(x.fi);
}
int construct(vector<vi> p) {
int n = p.size();
vector<vi> ans(n);
rep(i, n) ans[i].resize(n);
rep(i, n) rep(j, n) ans[i][j] = 0;
int mx = 0;
rep(i, n) rep(j, n) if(p[i][j] && i != j){
g[i].pb({j, p[i][j]});
mx = max(mx, p[i][j]);
}
if(mx == 3) return 0;
rep(i, n) if(!vis[i]){
int f = 0;
for(pi x : g[i]) if(x.se == 2) f |= 2;
if(f == 2){
cur2.clear();
dfs2(i);
vi circ; // the arc
vi vis1((int)cur2.size(), 0);
rep(j, cur2.size()) if(!vis1[j]){
int cr = cur2[j];
circ.pb(cr);
vi ch = {cr};
vis1[j] = 2;
rep(k, cur2.size()) if(j != k && !vis1[k]){
if(!p[cur2[j]][cur2[k]]) return 0;
if(p[cur2[j]][cur2[k]] == 1){
vis1[k] = 2;
ans[cr][cur2[k]] = 1;
ans[cur2[k]][cr] = 1;
ch.pb(cur2[k]);
}
}
rep(l, ch.size()){
rep(k, cur2.size()) if(vis1[k] != 2){
if(p[ch[l]][cur2[k]] != 2) return 0;
}
}
rep(k, cur2.size()) if(vis1[k] == 2) vis1[k] = 1;
}
if(circ.size() < 3) return 0;
rep(j, circ.size() - 1){
ans[circ[j]][circ[j + 1]] = 1;
ans[circ[j + 1]][circ[j]] = 1;
}
int m = (int)circ.size() - 1;
ans[circ[0]][circ[m]] = 1;
ans[circ[m]][circ[0]] = 1;
}
}
rep(i, n) if(!vis[i]){
int f = 0;
for(pi x : g[i]){
if(x.se == 1) f |= 1;
if(x.se == 2) f |= 2;
}
if(f == 1){
cur1.clear();
dfs1(i);
rep(j, cur1.size()) rep(k, cur1.size()) if(j != k){
if(p[cur1[j]][cur1[k]] != 1) return 0;
}
sort(all(cur1));
rep(j, cur1.size() - 1){
ans[cur1[j]][cur1[j + 1]] = 1;
ans[cur1[j + 1]][cur1[j]] = 1;
}
}
}
build(ans);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
12 ms |
1664 KB |
Output is correct |
7 |
Correct |
259 ms |
30200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
12 ms |
1664 KB |
Output is correct |
7 |
Correct |
259 ms |
30200 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
12 ms |
1256 KB |
Output is correct |
13 |
Correct |
239 ms |
22132 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
1024 KB |
Output is correct |
17 |
Correct |
127 ms |
17140 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
60 ms |
5880 KB |
Output is correct |
21 |
Correct |
252 ms |
23160 KB |
Output is correct |
22 |
Correct |
244 ms |
22140 KB |
Output is correct |
23 |
Correct |
249 ms |
26104 KB |
Output is correct |
24 |
Correct |
248 ms |
22092 KB |
Output is correct |
25 |
Correct |
116 ms |
13176 KB |
Output is correct |
26 |
Correct |
109 ms |
12280 KB |
Output is correct |
27 |
Correct |
263 ms |
28408 KB |
Output is correct |
28 |
Correct |
233 ms |
22136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
11 ms |
1280 KB |
Output is correct |
9 |
Correct |
237 ms |
22136 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
11 ms |
1664 KB |
Output is correct |
13 |
Correct |
274 ms |
30200 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
1024 KB |
Output is correct |
17 |
Correct |
124 ms |
17008 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
75 ms |
5880 KB |
Output is correct |
22 |
Correct |
248 ms |
23160 KB |
Output is correct |
23 |
Correct |
241 ms |
22132 KB |
Output is correct |
24 |
Correct |
249 ms |
28408 KB |
Output is correct |
25 |
Correct |
105 ms |
12152 KB |
Output is correct |
26 |
Correct |
102 ms |
13168 KB |
Output is correct |
27 |
Correct |
236 ms |
22136 KB |
Output is correct |
28 |
Correct |
276 ms |
26236 KB |
Output is correct |
29 |
Correct |
95 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
61 ms |
5880 KB |
Output is correct |
5 |
Correct |
238 ms |
23288 KB |
Output is correct |
6 |
Correct |
227 ms |
22264 KB |
Output is correct |
7 |
Correct |
249 ms |
26360 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
60 ms |
5880 KB |
Output is correct |
10 |
Correct |
243 ms |
23232 KB |
Output is correct |
11 |
Correct |
231 ms |
22264 KB |
Output is correct |
12 |
Correct |
253 ms |
28408 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
59 ms |
5880 KB |
Output is correct |
17 |
Correct |
235 ms |
23160 KB |
Output is correct |
18 |
Correct |
239 ms |
23160 KB |
Output is correct |
19 |
Correct |
236 ms |
22520 KB |
Output is correct |
20 |
Correct |
242 ms |
22136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
12 ms |
1664 KB |
Output is correct |
7 |
Correct |
259 ms |
30200 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
12 ms |
1256 KB |
Output is correct |
13 |
Correct |
239 ms |
22132 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
1024 KB |
Output is correct |
17 |
Correct |
127 ms |
17140 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
60 ms |
5880 KB |
Output is correct |
21 |
Correct |
252 ms |
23160 KB |
Output is correct |
22 |
Correct |
244 ms |
22140 KB |
Output is correct |
23 |
Correct |
249 ms |
26104 KB |
Output is correct |
24 |
Correct |
248 ms |
22092 KB |
Output is correct |
25 |
Correct |
116 ms |
13176 KB |
Output is correct |
26 |
Correct |
109 ms |
12280 KB |
Output is correct |
27 |
Correct |
263 ms |
28408 KB |
Output is correct |
28 |
Correct |
233 ms |
22136 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
0 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
0 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
11 ms |
1280 KB |
Output is correct |
37 |
Correct |
237 ms |
22136 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
0 ms |
384 KB |
Output is correct |
40 |
Correct |
11 ms |
1664 KB |
Output is correct |
41 |
Correct |
274 ms |
30200 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
1024 KB |
Output is correct |
45 |
Correct |
124 ms |
17008 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
75 ms |
5880 KB |
Output is correct |
50 |
Correct |
248 ms |
23160 KB |
Output is correct |
51 |
Correct |
241 ms |
22132 KB |
Output is correct |
52 |
Correct |
249 ms |
28408 KB |
Output is correct |
53 |
Correct |
105 ms |
12152 KB |
Output is correct |
54 |
Correct |
102 ms |
13168 KB |
Output is correct |
55 |
Correct |
236 ms |
22136 KB |
Output is correct |
56 |
Correct |
276 ms |
26236 KB |
Output is correct |
57 |
Correct |
95 ms |
12152 KB |
Output is correct |
58 |
Correct |
0 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
6 ms |
1152 KB |
Output is correct |
61 |
Correct |
127 ms |
20216 KB |
Output is correct |
62 |
Correct |
0 ms |
384 KB |
Output is correct |
63 |
Correct |
0 ms |
384 KB |
Output is correct |
64 |
Correct |
1 ms |
384 KB |
Output is correct |
65 |
Correct |
60 ms |
5860 KB |
Output is correct |
66 |
Correct |
110 ms |
13304 KB |
Output is correct |
67 |
Correct |
110 ms |
13176 KB |
Output is correct |
68 |
Correct |
98 ms |
12528 KB |
Output is correct |
69 |
Correct |
96 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
12 ms |
1664 KB |
Output is correct |
7 |
Correct |
259 ms |
30200 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
12 ms |
1256 KB |
Output is correct |
13 |
Correct |
239 ms |
22132 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
1024 KB |
Output is correct |
17 |
Correct |
127 ms |
17140 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
60 ms |
5880 KB |
Output is correct |
21 |
Correct |
252 ms |
23160 KB |
Output is correct |
22 |
Correct |
244 ms |
22140 KB |
Output is correct |
23 |
Correct |
249 ms |
26104 KB |
Output is correct |
24 |
Correct |
248 ms |
22092 KB |
Output is correct |
25 |
Correct |
116 ms |
13176 KB |
Output is correct |
26 |
Correct |
109 ms |
12280 KB |
Output is correct |
27 |
Correct |
263 ms |
28408 KB |
Output is correct |
28 |
Correct |
233 ms |
22136 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
0 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
0 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
11 ms |
1280 KB |
Output is correct |
37 |
Correct |
237 ms |
22136 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
0 ms |
384 KB |
Output is correct |
40 |
Correct |
11 ms |
1664 KB |
Output is correct |
41 |
Correct |
274 ms |
30200 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
1024 KB |
Output is correct |
45 |
Correct |
124 ms |
17008 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
75 ms |
5880 KB |
Output is correct |
50 |
Correct |
248 ms |
23160 KB |
Output is correct |
51 |
Correct |
241 ms |
22132 KB |
Output is correct |
52 |
Correct |
249 ms |
28408 KB |
Output is correct |
53 |
Correct |
105 ms |
12152 KB |
Output is correct |
54 |
Correct |
102 ms |
13168 KB |
Output is correct |
55 |
Correct |
236 ms |
22136 KB |
Output is correct |
56 |
Correct |
276 ms |
26236 KB |
Output is correct |
57 |
Correct |
95 ms |
12152 KB |
Output is correct |
58 |
Correct |
0 ms |
384 KB |
Output is correct |
59 |
Correct |
0 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
61 ms |
5880 KB |
Output is correct |
62 |
Correct |
238 ms |
23288 KB |
Output is correct |
63 |
Correct |
227 ms |
22264 KB |
Output is correct |
64 |
Correct |
249 ms |
26360 KB |
Output is correct |
65 |
Correct |
0 ms |
384 KB |
Output is correct |
66 |
Correct |
60 ms |
5880 KB |
Output is correct |
67 |
Correct |
243 ms |
23232 KB |
Output is correct |
68 |
Correct |
231 ms |
22264 KB |
Output is correct |
69 |
Correct |
253 ms |
28408 KB |
Output is correct |
70 |
Correct |
1 ms |
384 KB |
Output is correct |
71 |
Correct |
1 ms |
384 KB |
Output is correct |
72 |
Correct |
0 ms |
384 KB |
Output is correct |
73 |
Correct |
59 ms |
5880 KB |
Output is correct |
74 |
Correct |
235 ms |
23160 KB |
Output is correct |
75 |
Correct |
239 ms |
23160 KB |
Output is correct |
76 |
Correct |
236 ms |
22520 KB |
Output is correct |
77 |
Correct |
242 ms |
22136 KB |
Output is correct |
78 |
Correct |
0 ms |
384 KB |
Output is correct |
79 |
Correct |
1 ms |
384 KB |
Output is correct |
80 |
Correct |
6 ms |
1152 KB |
Output is correct |
81 |
Correct |
127 ms |
20216 KB |
Output is correct |
82 |
Correct |
0 ms |
384 KB |
Output is correct |
83 |
Correct |
0 ms |
384 KB |
Output is correct |
84 |
Correct |
1 ms |
384 KB |
Output is correct |
85 |
Correct |
60 ms |
5860 KB |
Output is correct |
86 |
Correct |
110 ms |
13304 KB |
Output is correct |
87 |
Correct |
110 ms |
13176 KB |
Output is correct |
88 |
Correct |
98 ms |
12528 KB |
Output is correct |
89 |
Correct |
96 ms |
12152 KB |
Output is correct |
90 |
Correct |
0 ms |
384 KB |
Output is correct |
91 |
Correct |
0 ms |
384 KB |
Output is correct |
92 |
Correct |
6 ms |
1280 KB |
Output is correct |
93 |
Correct |
121 ms |
20212 KB |
Output is correct |
94 |
Correct |
0 ms |
384 KB |
Output is correct |
95 |
Correct |
0 ms |
384 KB |
Output is correct |
96 |
Correct |
1 ms |
384 KB |
Output is correct |
97 |
Correct |
26 ms |
3328 KB |
Output is correct |
98 |
Correct |
103 ms |
13176 KB |
Output is correct |
99 |
Correct |
102 ms |
13176 KB |
Output is correct |
100 |
Correct |
102 ms |
12536 KB |
Output is correct |
101 |
Correct |
99 ms |
12152 KB |
Output is correct |
102 |
Correct |
119 ms |
20216 KB |
Output is correct |
103 |
Correct |
119 ms |
20216 KB |
Output is correct |
104 |
Correct |
124 ms |
20216 KB |
Output is correct |
105 |
Correct |
119 ms |
17016 KB |
Output is correct |