#include <bits/stdc++.h>
#include "supertrees.h"
using namespace std;
//#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
int par[1005], par2[1005];
vector<int> stuf[1005];
int getr(int x){
return (par[x] == x ? x : par[x] = getr(par[x]));
}
int g2(int x){
return (par2[x] == x ? x : par2[x] = g2(par2[x]));
}
void m2(int a, int b){
a = g2(a), b = g2(b);
if(a != b)par2[b] = a;
}
void merge(int a, int b){
a = getr(a), b = getr(b);
if(a != b)par[b] = a;
}
int construct(std::vector<std::vector<int>> p) {
int n = p.size();
for(int i=0;i<n;i++)par[i] = i;
std::vector<std::vector<int>> ans;
for (int i = 0; i < n; i++) {
std::vector<int> row(n, 0);
ans.push_back(row);
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(p[i][j] == 3)return 0;
if(p[i][j])merge(i, j);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(!p[i][j] && getr(i) == getr(j))return 0;
}
}
for(int i=0;i<n;i++)par2[i] = i;
for(int i=0;i<n;i++){
vector<int> pos;
for(int j=0;j<n;j++)if(getr(j) == i)pos.push_back(j);
vector<int> pos2, pos3;
for(auto j : pos){
bool f = 1;
for(auto k : pos){
if(j == k)continue;
if(p[j][k] != 2)f =0 ;
}
if(f)pos2.push_back(j);
else {
pos3.push_back(j);
for(auto k : pos)if(p[j][k] == 1)m2(j, k);
}
}
if((int)pos.size() <= 1)continue;
if(((int)pos2.size() == 2 && (int)pos.size() == 2))return 0;
for(int j=0;j<(int)pos2.size() - 1;j++){
ans[pos2[j]][pos2[j+1]] = 1;
ans[pos2[j+1]][pos2[j]] = 1;
}
if((int)pos.size() == (int)pos2.size() && (int)pos2.size() > 2){
ans[pos2[0]][pos2[(int)pos2.size() - 1]] = 1;
ans[pos2[(int)pos2.size() - 1]][pos2[0]] = 1;
}
else if((int)pos.size() != (int)pos2.size()){
for(int j=0;j<n;j++)stuf[j].clear();
set <int> s;
for(auto j : pos3){
//cout << j << " " << g2(j) << '\n';
stuf[g2(j)].push_back(j);
s.insert(g2(j));
}
if((int)s.size() == 1 && (int)pos2.size() == 1)return 0;
//cout << s.size() << '\n';
int pre = -1, c1 = -1;
for(auto j : s){
for(int k=0;k<(int)stuf[j].size()-1;k++){
ans[stuf[j][k]][stuf[j][k+1]] = 1;
ans[stuf[j][k+1]][stuf[j][k]] = 1;
}
if(pre != -1){
ans[stuf[pre][0]][stuf[j][0]] = 1;
ans[stuf[j][0]][stuf[pre][0]] = 1;
}
else{
if(pos2.size()){
ans[pos2[0]][stuf[j][0]] = 1;
ans[stuf[j][0]][pos2[0]] = 1;
}
else{
c1 = j;
}
}
pre = j;
}
if(pos2.size()){
ans[pos2[(int)pos2.size()-1]][stuf[pre][0]] = 1;
ans[stuf[pre][0]][pos2[(int)pos2.size()-1]] = 1;
}
else if(c1 != pre){
ans[stuf[c1][0]][stuf[pre][0]] = 1;
ans[stuf[pre][0]][stuf[c1][0]] = 1;
}
/*
for(int j=0;j<(int)pos3.size() - 1;j++){
ans[pos3[j]][pos3[j+1]] = 1;
ans[pos3[j+1]][pos3[j]] = 1;
}
if(!pos2.empty()){
ans[pos3[0]][pos2[0]] = 1;
ans[pos2[0]][pos3[0]] = 1;
ans[pos3[0]][pos2[(int)pos2.size()-1]] = 1;
ans[pos2[(int)pos2.size()-1]][pos3[0]] = 1;
}
*/
}
for(int j=0;j<n;j++)par2[j] = j;
}
build(ans);
return 1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
8 ms |
1232 KB |
Output is correct |
7 |
Correct |
183 ms |
23964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
8 ms |
1232 KB |
Output is correct |
7 |
Correct |
183 ms |
23964 KB |
Output is correct |
8 |
Correct |
1 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 |
332 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
172 ms |
23952 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
4 ms |
820 KB |
Output is correct |
17 |
Correct |
87 ms |
14104 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
42 ms |
6220 KB |
Output is correct |
21 |
Correct |
178 ms |
24008 KB |
Output is correct |
22 |
Correct |
168 ms |
23964 KB |
Output is correct |
23 |
Correct |
200 ms |
24080 KB |
Output is correct |
24 |
Correct |
163 ms |
23940 KB |
Output is correct |
25 |
Correct |
70 ms |
14028 KB |
Output is correct |
26 |
Correct |
68 ms |
14092 KB |
Output is correct |
27 |
Correct |
193 ms |
23956 KB |
Output is correct |
28 |
Correct |
166 ms |
24024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
8 ms |
1236 KB |
Output is correct |
9 |
Correct |
166 ms |
24092 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
177 ms |
24000 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
4 ms |
852 KB |
Output is correct |
17 |
Correct |
89 ms |
14096 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
43 ms |
6252 KB |
Output is correct |
22 |
Correct |
178 ms |
23968 KB |
Output is correct |
23 |
Correct |
179 ms |
23948 KB |
Output is correct |
24 |
Correct |
192 ms |
24012 KB |
Output is correct |
25 |
Correct |
73 ms |
14028 KB |
Output is correct |
26 |
Correct |
68 ms |
14112 KB |
Output is correct |
27 |
Correct |
169 ms |
23948 KB |
Output is correct |
28 |
Correct |
189 ms |
24048 KB |
Output is correct |
29 |
Correct |
69 ms |
14108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
46 ms |
6288 KB |
Output is correct |
5 |
Correct |
183 ms |
23964 KB |
Output is correct |
6 |
Correct |
168 ms |
24048 KB |
Output is correct |
7 |
Correct |
188 ms |
23956 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
51 ms |
6252 KB |
Output is correct |
10 |
Correct |
177 ms |
23952 KB |
Output is correct |
11 |
Correct |
167 ms |
24012 KB |
Output is correct |
12 |
Correct |
194 ms |
23952 KB |
Output is correct |
13 |
Correct |
0 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
45 ms |
6184 KB |
Output is correct |
17 |
Correct |
174 ms |
23960 KB |
Output is correct |
18 |
Correct |
173 ms |
24012 KB |
Output is correct |
19 |
Correct |
173 ms |
23952 KB |
Output is correct |
20 |
Correct |
162 ms |
23952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
8 ms |
1232 KB |
Output is correct |
7 |
Correct |
183 ms |
23964 KB |
Output is correct |
8 |
Correct |
1 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 |
332 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
172 ms |
23952 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
4 ms |
820 KB |
Output is correct |
17 |
Correct |
87 ms |
14104 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
42 ms |
6220 KB |
Output is correct |
21 |
Correct |
178 ms |
24008 KB |
Output is correct |
22 |
Correct |
168 ms |
23964 KB |
Output is correct |
23 |
Correct |
200 ms |
24080 KB |
Output is correct |
24 |
Correct |
163 ms |
23940 KB |
Output is correct |
25 |
Correct |
70 ms |
14028 KB |
Output is correct |
26 |
Correct |
68 ms |
14092 KB |
Output is correct |
27 |
Correct |
193 ms |
23956 KB |
Output is correct |
28 |
Correct |
166 ms |
24024 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
166 ms |
24092 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
8 ms |
1236 KB |
Output is correct |
41 |
Correct |
177 ms |
24000 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
89 ms |
14096 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
43 ms |
6252 KB |
Output is correct |
50 |
Correct |
178 ms |
23968 KB |
Output is correct |
51 |
Correct |
179 ms |
23948 KB |
Output is correct |
52 |
Correct |
192 ms |
24012 KB |
Output is correct |
53 |
Correct |
73 ms |
14028 KB |
Output is correct |
54 |
Correct |
68 ms |
14112 KB |
Output is correct |
55 |
Correct |
169 ms |
23948 KB |
Output is correct |
56 |
Correct |
189 ms |
24048 KB |
Output is correct |
57 |
Correct |
69 ms |
14108 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
4 ms |
856 KB |
Output is correct |
61 |
Correct |
90 ms |
14096 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
1 ms |
212 KB |
Output is correct |
64 |
Correct |
1 ms |
212 KB |
Output is correct |
65 |
Correct |
43 ms |
6220 KB |
Output is correct |
66 |
Correct |
69 ms |
14096 KB |
Output is correct |
67 |
Correct |
72 ms |
14096 KB |
Output is correct |
68 |
Correct |
65 ms |
14092 KB |
Output is correct |
69 |
Correct |
68 ms |
14028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
8 ms |
1232 KB |
Output is correct |
7 |
Correct |
183 ms |
23964 KB |
Output is correct |
8 |
Correct |
1 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 |
332 KB |
Output is correct |
12 |
Correct |
8 ms |
1236 KB |
Output is correct |
13 |
Correct |
172 ms |
23952 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
4 ms |
820 KB |
Output is correct |
17 |
Correct |
87 ms |
14104 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
42 ms |
6220 KB |
Output is correct |
21 |
Correct |
178 ms |
24008 KB |
Output is correct |
22 |
Correct |
168 ms |
23964 KB |
Output is correct |
23 |
Correct |
200 ms |
24080 KB |
Output is correct |
24 |
Correct |
163 ms |
23940 KB |
Output is correct |
25 |
Correct |
70 ms |
14028 KB |
Output is correct |
26 |
Correct |
68 ms |
14092 KB |
Output is correct |
27 |
Correct |
193 ms |
23956 KB |
Output is correct |
28 |
Correct |
166 ms |
24024 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
1236 KB |
Output is correct |
37 |
Correct |
166 ms |
24092 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
8 ms |
1236 KB |
Output is correct |
41 |
Correct |
177 ms |
24000 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
4 ms |
852 KB |
Output is correct |
45 |
Correct |
89 ms |
14096 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
43 ms |
6252 KB |
Output is correct |
50 |
Correct |
178 ms |
23968 KB |
Output is correct |
51 |
Correct |
179 ms |
23948 KB |
Output is correct |
52 |
Correct |
192 ms |
24012 KB |
Output is correct |
53 |
Correct |
73 ms |
14028 KB |
Output is correct |
54 |
Correct |
68 ms |
14112 KB |
Output is correct |
55 |
Correct |
169 ms |
23948 KB |
Output is correct |
56 |
Correct |
189 ms |
24048 KB |
Output is correct |
57 |
Correct |
69 ms |
14108 KB |
Output is correct |
58 |
Correct |
1 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
46 ms |
6288 KB |
Output is correct |
62 |
Correct |
183 ms |
23964 KB |
Output is correct |
63 |
Correct |
168 ms |
24048 KB |
Output is correct |
64 |
Correct |
188 ms |
23956 KB |
Output is correct |
65 |
Correct |
1 ms |
212 KB |
Output is correct |
66 |
Correct |
51 ms |
6252 KB |
Output is correct |
67 |
Correct |
177 ms |
23952 KB |
Output is correct |
68 |
Correct |
167 ms |
24012 KB |
Output is correct |
69 |
Correct |
194 ms |
23952 KB |
Output is correct |
70 |
Correct |
0 ms |
324 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
328 KB |
Output is correct |
73 |
Correct |
45 ms |
6184 KB |
Output is correct |
74 |
Correct |
174 ms |
23960 KB |
Output is correct |
75 |
Correct |
173 ms |
24012 KB |
Output is correct |
76 |
Correct |
173 ms |
23952 KB |
Output is correct |
77 |
Correct |
162 ms |
23952 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
0 ms |
340 KB |
Output is correct |
80 |
Correct |
4 ms |
856 KB |
Output is correct |
81 |
Correct |
90 ms |
14096 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
212 KB |
Output is correct |
84 |
Correct |
1 ms |
212 KB |
Output is correct |
85 |
Correct |
43 ms |
6220 KB |
Output is correct |
86 |
Correct |
69 ms |
14096 KB |
Output is correct |
87 |
Correct |
72 ms |
14096 KB |
Output is correct |
88 |
Correct |
65 ms |
14092 KB |
Output is correct |
89 |
Correct |
68 ms |
14028 KB |
Output is correct |
90 |
Correct |
0 ms |
332 KB |
Output is correct |
91 |
Correct |
1 ms |
328 KB |
Output is correct |
92 |
Correct |
4 ms |
852 KB |
Output is correct |
93 |
Correct |
96 ms |
14100 KB |
Output is correct |
94 |
Correct |
1 ms |
212 KB |
Output is correct |
95 |
Correct |
1 ms |
328 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
17 ms |
3804 KB |
Output is correct |
98 |
Correct |
68 ms |
14104 KB |
Output is correct |
99 |
Correct |
66 ms |
14100 KB |
Output is correct |
100 |
Correct |
72 ms |
14104 KB |
Output is correct |
101 |
Correct |
67 ms |
14096 KB |
Output is correct |
102 |
Correct |
76 ms |
14108 KB |
Output is correct |
103 |
Correct |
76 ms |
14028 KB |
Output is correct |
104 |
Correct |
93 ms |
14092 KB |
Output is correct |
105 |
Correct |
80 ms |
14092 KB |
Output is correct |