#include <bits/stdc++.h>
using namespace std;
#define pb push_back
struct dsu {
vector<int> parent, rank;
int size;
dsu(int sz) {
size = sz;
rank.assign(size, 1);
parent.resize(size);
for(int i = 0; i<size; i++) parent[i] = i;
}
int search(int v) {
if(v == parent[v]) return v;
return parent[v] = search(parent[v]);
}
void connect(int v, int u) {
v = search(v);
u = search(u);
if(v == u) return;
if(rank[v]>=rank[u]) parent[u] = v;
else parent[v] = u;
if(rank[v] == rank[u]) rank[v]++;
return;
}
};
void build(std::vector<std::vector<int>> b);
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
bool ctr = 1;
vector<vector<int>> b(n);
for(int i = 0; i<n; i++) b[i].resize(n, 0);
vector<int> a[n];
dsu d = dsu(n);
for(int i = 0; i<n; i++) {
for(int j = 0; j<n; j++) {
if(p[i][j]) d.connect(i, j);
}
}
for(int i = 0; i<n; i++) {
a[d.search(i)].pb(i);
}
for(int i = 0; i<n; i++) {
int sz = a[i].size();
for(int ii = 0; ii<sz; ii++) {
for(int j = 0; j<sz; j++) {
if(p[a[i][ii]][a[i][j]] == 0 || p[a[i][ii]][a[i][j]] == 3) ctr = 0;
}
}
if(sz<=1) continue;
vector<int> tree[sz], circle;
dsu dd = dsu(sz);
for(int ii = 0; ii<sz; ii++) {
for(int j = 0; j<sz; j++) {
if(p[a[i][ii]][a[i][j]] == 1) dd.connect(ii, j);
}
}
for(int ii = 0; ii<sz; ii++) {
tree[dd.search(ii)].pb(a[i][ii]);
}
for(int ii = 0; ii<sz; ii++) {
if(tree[ii].size() == 0) continue;
if(tree[ii].size()>1) {
for(size_t j = 1; j<tree[ii].size(); j++) b[tree[ii][j]][tree[ii][j-1]] = b[tree[ii][j-1]][tree[ii][j]] = 1;
}
for(size_t iii = 0; iii < tree[ii].size(); iii++) {
for(size_t j = 0; j < tree[ii].size(); j++) {
if(p[tree[ii][iii]][tree[ii][j]] != 1) ctr = 0;
}
}
circle.pb(tree[ii][0]);
}
for(size_t ii = 1; ii<circle.size(); ii++) {
b[circle[ii]][circle[ii-1]] = b[circle[ii-1]][circle[ii]] = 1;
}
if(circle.size()>1) b[circle[0]][circle.back()] = b[circle.back()][circle[0]] = 1;
if(circle.size() == 2) ctr = 0;
}
if(ctr) build(b);
return ctr;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
1112 KB |
Output is correct |
7 |
Correct |
137 ms |
22096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
1112 KB |
Output is correct |
7 |
Correct |
137 ms |
22096 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
126 ms |
22036 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
79 ms |
12272 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
32 ms |
5708 KB |
Output is correct |
21 |
Correct |
135 ms |
22100 KB |
Output is correct |
22 |
Correct |
128 ms |
22100 KB |
Output is correct |
23 |
Correct |
138 ms |
22100 KB |
Output is correct |
24 |
Correct |
119 ms |
22100 KB |
Output is correct |
25 |
Correct |
58 ms |
12312 KB |
Output is correct |
26 |
Correct |
51 ms |
12116 KB |
Output is correct |
27 |
Correct |
125 ms |
22040 KB |
Output is correct |
28 |
Correct |
116 ms |
22040 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 |
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 |
1116 KB |
Output is correct |
9 |
Correct |
117 ms |
22044 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
1116 KB |
Output is correct |
13 |
Correct |
121 ms |
22036 KB |
Output is correct |
14 |
Correct |
1 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 |
73 ms |
14164 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
32 ms |
6236 KB |
Output is correct |
22 |
Correct |
130 ms |
24184 KB |
Output is correct |
23 |
Correct |
124 ms |
24148 KB |
Output is correct |
24 |
Correct |
127 ms |
23944 KB |
Output is correct |
25 |
Correct |
51 ms |
14164 KB |
Output is correct |
26 |
Correct |
51 ms |
14164 KB |
Output is correct |
27 |
Correct |
117 ms |
24148 KB |
Output is correct |
28 |
Correct |
132 ms |
24148 KB |
Output is correct |
29 |
Correct |
51 ms |
14160 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 |
32 ms |
5728 KB |
Output is correct |
5 |
Correct |
126 ms |
22232 KB |
Output is correct |
6 |
Correct |
140 ms |
22100 KB |
Output is correct |
7 |
Correct |
131 ms |
22100 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
29 ms |
5928 KB |
Output is correct |
10 |
Correct |
127 ms |
22044 KB |
Output is correct |
11 |
Correct |
117 ms |
22144 KB |
Output is correct |
12 |
Correct |
127 ms |
22100 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
28 ms |
5712 KB |
Output is correct |
17 |
Correct |
125 ms |
22096 KB |
Output is correct |
18 |
Correct |
117 ms |
22100 KB |
Output is correct |
19 |
Correct |
117 ms |
22100 KB |
Output is correct |
20 |
Correct |
121 ms |
22040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
1112 KB |
Output is correct |
7 |
Correct |
137 ms |
22096 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
126 ms |
22036 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
79 ms |
12272 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
32 ms |
5708 KB |
Output is correct |
21 |
Correct |
135 ms |
22100 KB |
Output is correct |
22 |
Correct |
128 ms |
22100 KB |
Output is correct |
23 |
Correct |
138 ms |
22100 KB |
Output is correct |
24 |
Correct |
119 ms |
22100 KB |
Output is correct |
25 |
Correct |
58 ms |
12312 KB |
Output is correct |
26 |
Correct |
51 ms |
12116 KB |
Output is correct |
27 |
Correct |
125 ms |
22040 KB |
Output is correct |
28 |
Correct |
116 ms |
22040 KB |
Output is correct |
29 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
5 ms |
1116 KB |
Output is correct |
37 |
Correct |
117 ms |
22044 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
6 ms |
1116 KB |
Output is correct |
41 |
Correct |
121 ms |
22036 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
73 ms |
14164 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
32 ms |
6236 KB |
Output is correct |
50 |
Correct |
130 ms |
24184 KB |
Output is correct |
51 |
Correct |
124 ms |
24148 KB |
Output is correct |
52 |
Correct |
127 ms |
23944 KB |
Output is correct |
53 |
Correct |
51 ms |
14164 KB |
Output is correct |
54 |
Correct |
51 ms |
14164 KB |
Output is correct |
55 |
Correct |
117 ms |
24148 KB |
Output is correct |
56 |
Correct |
132 ms |
24148 KB |
Output is correct |
57 |
Correct |
51 ms |
14160 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
1 ms |
344 KB |
Output is correct |
60 |
Correct |
4 ms |
860 KB |
Output is correct |
61 |
Correct |
94 ms |
14248 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
32 ms |
6232 KB |
Output is correct |
66 |
Correct |
56 ms |
14068 KB |
Output is correct |
67 |
Correct |
53 ms |
14164 KB |
Output is correct |
68 |
Correct |
55 ms |
14160 KB |
Output is correct |
69 |
Correct |
67 ms |
14160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
5 ms |
1112 KB |
Output is correct |
7 |
Correct |
137 ms |
22096 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
126 ms |
22036 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
79 ms |
12272 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
32 ms |
5708 KB |
Output is correct |
21 |
Correct |
135 ms |
22100 KB |
Output is correct |
22 |
Correct |
128 ms |
22100 KB |
Output is correct |
23 |
Correct |
138 ms |
22100 KB |
Output is correct |
24 |
Correct |
119 ms |
22100 KB |
Output is correct |
25 |
Correct |
58 ms |
12312 KB |
Output is correct |
26 |
Correct |
51 ms |
12116 KB |
Output is correct |
27 |
Correct |
125 ms |
22040 KB |
Output is correct |
28 |
Correct |
116 ms |
22040 KB |
Output is correct |
29 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
5 ms |
1116 KB |
Output is correct |
37 |
Correct |
117 ms |
22044 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
6 ms |
1116 KB |
Output is correct |
41 |
Correct |
121 ms |
22036 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
860 KB |
Output is correct |
45 |
Correct |
73 ms |
14164 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
32 ms |
6236 KB |
Output is correct |
50 |
Correct |
130 ms |
24184 KB |
Output is correct |
51 |
Correct |
124 ms |
24148 KB |
Output is correct |
52 |
Correct |
127 ms |
23944 KB |
Output is correct |
53 |
Correct |
51 ms |
14164 KB |
Output is correct |
54 |
Correct |
51 ms |
14164 KB |
Output is correct |
55 |
Correct |
117 ms |
24148 KB |
Output is correct |
56 |
Correct |
132 ms |
24148 KB |
Output is correct |
57 |
Correct |
51 ms |
14160 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 |
32 ms |
5728 KB |
Output is correct |
62 |
Correct |
126 ms |
22232 KB |
Output is correct |
63 |
Correct |
140 ms |
22100 KB |
Output is correct |
64 |
Correct |
131 ms |
22100 KB |
Output is correct |
65 |
Correct |
0 ms |
344 KB |
Output is correct |
66 |
Correct |
29 ms |
5928 KB |
Output is correct |
67 |
Correct |
127 ms |
22044 KB |
Output is correct |
68 |
Correct |
117 ms |
22144 KB |
Output is correct |
69 |
Correct |
127 ms |
22100 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
1 ms |
344 KB |
Output is correct |
73 |
Correct |
28 ms |
5712 KB |
Output is correct |
74 |
Correct |
125 ms |
22096 KB |
Output is correct |
75 |
Correct |
117 ms |
22100 KB |
Output is correct |
76 |
Correct |
117 ms |
22100 KB |
Output is correct |
77 |
Correct |
121 ms |
22040 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
1 ms |
344 KB |
Output is correct |
80 |
Correct |
4 ms |
860 KB |
Output is correct |
81 |
Correct |
94 ms |
14248 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
32 ms |
6232 KB |
Output is correct |
86 |
Correct |
56 ms |
14068 KB |
Output is correct |
87 |
Correct |
53 ms |
14164 KB |
Output is correct |
88 |
Correct |
55 ms |
14160 KB |
Output is correct |
89 |
Correct |
67 ms |
14160 KB |
Output is correct |
90 |
Correct |
0 ms |
344 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
4 ms |
860 KB |
Output is correct |
93 |
Correct |
71 ms |
14240 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 |
3932 KB |
Output is correct |
98 |
Correct |
54 ms |
14084 KB |
Output is correct |
99 |
Correct |
53 ms |
14164 KB |
Output is correct |
100 |
Correct |
50 ms |
14220 KB |
Output is correct |
101 |
Correct |
66 ms |
14164 KB |
Output is correct |
102 |
Correct |
54 ms |
14160 KB |
Output is correct |
103 |
Correct |
59 ms |
14160 KB |
Output is correct |
104 |
Correct |
68 ms |
14164 KB |
Output is correct |
105 |
Correct |
72 ms |
14068 KB |
Output is correct |