#include "supertrees.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a), end(a)
using ar2 = array<int,2>;
using vi = vector<int>;
using ll = long long;
const int INF = (int)2e9;
const int mxN = 1010;
template<int SZ> struct DSU{
int n, p[SZ], sz[SZ];
void init(int _n){
n = _n;
for(int i = 0; i < n; i++) p[i]=i,sz[i]=1;
}
int findSet(int i){return i==p[i]?i:p[i]=findSet(p[i]);}
bool isSameSet(int i, int j){return findSet(i)==findSet(j);}
int findSz(int i){return sz[findSet(i)];}
void unionSet(int i, int j){
int x = findSet(i), y = findSet(j);
if(x==y) return;
if(sz[x]<sz[y])swap(x,y);
p[y]=x,sz[x]+=sz[y];
}
};
DSU<mxN> dsu1, dsu2;
vector<int> v[mxN], w[mxN];
int construct(vector<vi> p) {
int n = sz(p);
vector<vi> ans(n,vi(n,0));
dsu1.init(n); dsu2.init(n);
for(int i = 0; i < n; i++){
for(int j = i+1; j < n; j++){
if(p[i][j]==3) return 0;
if(p[i][j]) dsu1.unionSet(i,j);
}
}
for(int i = 0; i < n; i++)
v[dsu1.findSet(i)].pb(i);
for(int _ = 0; _ < n; _++){
if(sz(v[_])<=1) continue;
bool justCycle=1, justLine=1;
for(auto i : v[_]){
for(auto j : v[_]){
if(i==j) continue;
if(p[i][j]==1) dsu2.unionSet(i,j),justCycle=0;
if(p[i][j]==2) justLine=0;
if(!p[i][j]) return 0;
}
}
if(justCycle or justLine){
for(int i = 0; i < sz(v[_])-1; i++)
ans[v[_][i]][v[_][i+1]]=1;
if(justCycle){
ans[v[_][0]][v[_].back()]=1;
if(sz(v[_])<=2) return 0;
}
}
else{
for(int i = 0; i < n; i++) w[i].clear();
for(auto i : v[_]) w[dsu2.findSet(i)].pb(i);
vector<int> rep; rep.clear();
for(int i = 0; i < n; i++){
if(!sz(w[i])) continue;
for(int j = 0; j < sz(w[i])-1; j++)
ans[w[i][j]][w[i][j+1]]=1;
for(auto j : w[i])
for(auto k : w[i])
if(j!=k and p[j][k]!=1) return 0;
rep.pb(w[i][0]);
}
if(sz(rep)<=2) return 0;
for(int i = 0; i < sz(rep)-1; i++)
ans[rep[i]][rep[i+1]]=1;
ans[rep[0]][rep.back()]=1;
}
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
ans[i][j]|=ans[j][i];
build(ans);
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1440 KB |
Output is correct |
7 |
Correct |
128 ms |
24148 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1440 KB |
Output is correct |
7 |
Correct |
128 ms |
24148 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
488 KB |
Output is correct |
12 |
Correct |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
113 ms |
24144 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
65 ms |
14268 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
504 KB |
Output is correct |
20 |
Correct |
28 ms |
6348 KB |
Output is correct |
21 |
Correct |
115 ms |
24152 KB |
Output is correct |
22 |
Correct |
122 ms |
24148 KB |
Output is correct |
23 |
Correct |
129 ms |
24148 KB |
Output is correct |
24 |
Correct |
125 ms |
24076 KB |
Output is correct |
25 |
Correct |
54 ms |
14164 KB |
Output is correct |
26 |
Correct |
49 ms |
14164 KB |
Output is correct |
27 |
Correct |
126 ms |
24120 KB |
Output is correct |
28 |
Correct |
111 ms |
24072 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
1368 KB |
Output is correct |
9 |
Correct |
158 ms |
23980 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1236 KB |
Output is correct |
13 |
Correct |
120 ms |
24052 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 |
1012 KB |
Output is correct |
17 |
Correct |
77 ms |
14164 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 |
348 KB |
Output is correct |
21 |
Correct |
28 ms |
6400 KB |
Output is correct |
22 |
Correct |
114 ms |
24124 KB |
Output is correct |
23 |
Correct |
116 ms |
24144 KB |
Output is correct |
24 |
Correct |
119 ms |
24144 KB |
Output is correct |
25 |
Correct |
49 ms |
14164 KB |
Output is correct |
26 |
Correct |
55 ms |
14164 KB |
Output is correct |
27 |
Correct |
121 ms |
24148 KB |
Output is correct |
28 |
Correct |
121 ms |
24144 KB |
Output is correct |
29 |
Correct |
51 ms |
14120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
28 ms |
6236 KB |
Output is correct |
5 |
Correct |
116 ms |
24148 KB |
Output is correct |
6 |
Correct |
111 ms |
24144 KB |
Output is correct |
7 |
Correct |
126 ms |
24148 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
29 ms |
6444 KB |
Output is correct |
10 |
Correct |
116 ms |
24148 KB |
Output is correct |
11 |
Correct |
120 ms |
24148 KB |
Output is correct |
12 |
Correct |
124 ms |
23996 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 |
344 KB |
Output is correct |
16 |
Correct |
28 ms |
6460 KB |
Output is correct |
17 |
Correct |
115 ms |
24148 KB |
Output is correct |
18 |
Correct |
114 ms |
24124 KB |
Output is correct |
19 |
Correct |
117 ms |
24144 KB |
Output is correct |
20 |
Correct |
137 ms |
24148 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1440 KB |
Output is correct |
7 |
Correct |
128 ms |
24148 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
488 KB |
Output is correct |
12 |
Correct |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
113 ms |
24144 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
65 ms |
14268 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
504 KB |
Output is correct |
20 |
Correct |
28 ms |
6348 KB |
Output is correct |
21 |
Correct |
115 ms |
24152 KB |
Output is correct |
22 |
Correct |
122 ms |
24148 KB |
Output is correct |
23 |
Correct |
129 ms |
24148 KB |
Output is correct |
24 |
Correct |
125 ms |
24076 KB |
Output is correct |
25 |
Correct |
54 ms |
14164 KB |
Output is correct |
26 |
Correct |
49 ms |
14164 KB |
Output is correct |
27 |
Correct |
126 ms |
24120 KB |
Output is correct |
28 |
Correct |
111 ms |
24072 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 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 |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
5 ms |
1368 KB |
Output is correct |
37 |
Correct |
158 ms |
23980 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1236 KB |
Output is correct |
41 |
Correct |
120 ms |
24052 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 |
1012 KB |
Output is correct |
45 |
Correct |
77 ms |
14164 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 |
348 KB |
Output is correct |
49 |
Correct |
28 ms |
6400 KB |
Output is correct |
50 |
Correct |
114 ms |
24124 KB |
Output is correct |
51 |
Correct |
116 ms |
24144 KB |
Output is correct |
52 |
Correct |
119 ms |
24144 KB |
Output is correct |
53 |
Correct |
49 ms |
14164 KB |
Output is correct |
54 |
Correct |
55 ms |
14164 KB |
Output is correct |
55 |
Correct |
121 ms |
24148 KB |
Output is correct |
56 |
Correct |
121 ms |
24144 KB |
Output is correct |
57 |
Correct |
51 ms |
14120 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 |
860 KB |
Output is correct |
61 |
Correct |
57 ms |
14164 KB |
Output is correct |
62 |
Correct |
1 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
28 ms |
6328 KB |
Output is correct |
66 |
Correct |
60 ms |
14164 KB |
Output is correct |
67 |
Correct |
53 ms |
14164 KB |
Output is correct |
68 |
Correct |
51 ms |
14160 KB |
Output is correct |
69 |
Correct |
50 ms |
14160 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
5 ms |
1440 KB |
Output is correct |
7 |
Correct |
128 ms |
24148 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
488 KB |
Output is correct |
12 |
Correct |
5 ms |
1372 KB |
Output is correct |
13 |
Correct |
113 ms |
24144 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
65 ms |
14268 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
504 KB |
Output is correct |
20 |
Correct |
28 ms |
6348 KB |
Output is correct |
21 |
Correct |
115 ms |
24152 KB |
Output is correct |
22 |
Correct |
122 ms |
24148 KB |
Output is correct |
23 |
Correct |
129 ms |
24148 KB |
Output is correct |
24 |
Correct |
125 ms |
24076 KB |
Output is correct |
25 |
Correct |
54 ms |
14164 KB |
Output is correct |
26 |
Correct |
49 ms |
14164 KB |
Output is correct |
27 |
Correct |
126 ms |
24120 KB |
Output is correct |
28 |
Correct |
111 ms |
24072 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 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 |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
5 ms |
1368 KB |
Output is correct |
37 |
Correct |
158 ms |
23980 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1236 KB |
Output is correct |
41 |
Correct |
120 ms |
24052 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 |
1012 KB |
Output is correct |
45 |
Correct |
77 ms |
14164 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 |
348 KB |
Output is correct |
49 |
Correct |
28 ms |
6400 KB |
Output is correct |
50 |
Correct |
114 ms |
24124 KB |
Output is correct |
51 |
Correct |
116 ms |
24144 KB |
Output is correct |
52 |
Correct |
119 ms |
24144 KB |
Output is correct |
53 |
Correct |
49 ms |
14164 KB |
Output is correct |
54 |
Correct |
55 ms |
14164 KB |
Output is correct |
55 |
Correct |
121 ms |
24148 KB |
Output is correct |
56 |
Correct |
121 ms |
24144 KB |
Output is correct |
57 |
Correct |
51 ms |
14120 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
28 ms |
6236 KB |
Output is correct |
62 |
Correct |
116 ms |
24148 KB |
Output is correct |
63 |
Correct |
111 ms |
24144 KB |
Output is correct |
64 |
Correct |
126 ms |
24148 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
29 ms |
6444 KB |
Output is correct |
67 |
Correct |
116 ms |
24148 KB |
Output is correct |
68 |
Correct |
120 ms |
24148 KB |
Output is correct |
69 |
Correct |
124 ms |
23996 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 |
344 KB |
Output is correct |
73 |
Correct |
28 ms |
6460 KB |
Output is correct |
74 |
Correct |
115 ms |
24148 KB |
Output is correct |
75 |
Correct |
114 ms |
24124 KB |
Output is correct |
76 |
Correct |
117 ms |
24144 KB |
Output is correct |
77 |
Correct |
137 ms |
24148 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 |
860 KB |
Output is correct |
81 |
Correct |
57 ms |
14164 KB |
Output is correct |
82 |
Correct |
1 ms |
344 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
28 ms |
6328 KB |
Output is correct |
86 |
Correct |
60 ms |
14164 KB |
Output is correct |
87 |
Correct |
53 ms |
14164 KB |
Output is correct |
88 |
Correct |
51 ms |
14160 KB |
Output is correct |
89 |
Correct |
50 ms |
14160 KB |
Output is correct |
90 |
Correct |
1 ms |
348 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
2 ms |
860 KB |
Output is correct |
93 |
Correct |
54 ms |
14140 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 |
13 ms |
3764 KB |
Output is correct |
98 |
Correct |
52 ms |
14160 KB |
Output is correct |
99 |
Correct |
50 ms |
14140 KB |
Output is correct |
100 |
Correct |
49 ms |
14164 KB |
Output is correct |
101 |
Correct |
52 ms |
14268 KB |
Output is correct |
102 |
Correct |
72 ms |
14160 KB |
Output is correct |
103 |
Correct |
54 ms |
14264 KB |
Output is correct |
104 |
Correct |
49 ms |
14164 KB |
Output is correct |
105 |
Correct |
55 ms |
14164 KB |
Output is correct |