#include "supertrees.h"
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
using namespace std;
const int MAXN = 1e3+10;
const double SMALL = 1e-6;
typedef pair<int,int> pii;
typedef pair<pii,int> ipii;
int n;
bool tag[MAXN], done[MAXN];
vector <vector<int>> adj;
vector <int> cc[MAXN];
void bd(int x, int y){
adj[x][y] = 1; adj[y][x] = 1;
}
struct DSU {
int par[MAXN], siz[MAXN];
void bd(){
for(int i=0; i<n; i++){
par[i] = i; siz[i] = 1;
cc[i].pb(i);
}
}
int f(int x){
if(x == par[x]) return x;
return par[x] = f(par[x]);
}
bool con(int x, int y){
return f(x) == f(y);
}
void mer(int x, int y){
if(con(x, y)) return;
x = f(x); y = f(y);
if(siz[x] > siz[y]) swap(x, y);
par[x] = y; siz[y] += siz[x];
for(auto in : cc[x]) cc[y].pb(in);
}
} A;
int construct(vector<vector<int>> p) {
n = p.size();
vector <int> te(n);
for(int i=0; i<n; i++) adj.pb(te);
A.bd();
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(p[i][j] == 3) return 0;
}
}
for(int i = 0; i < n; i++) {
if(done[i]) continue;
for(int x=0; x<n; x++) tag[x] = 0;
vector <int> vec; vec.pb(i); tag[i] = 1;
for(int j=i+1; j < n; j++){
if(p[i][j] == 1){
vec.pb(j); tag[j] = 1;
}
}
//for(auto in : vec) cout << in << "pp\n";
for(auto in : vec){
//cout << in << " in\n";
done[in] = 1;
for(int x=0; x<n; x++){
if(tag[x] && p[in][x] != 1) return 0; //harusnya 1
if(!tag[x] && p[in][x] == 1) return 0;
}
}
for(int i=0; i+1<vec.size(); i++){ // build
bd(vec[i], vec[i+1]);
A.mer(vec[i], vec[i+1]);
}
}
// for(int i=0; i<n; i++){
// for(int j=0; j<n; j++) cout << adj[i][j] << ' ';
// cout << '\n';
// }
// for(int i=0; i<n; i++){
// if(A.f(i) == i){
// for(auto in : cc[i]) cout << i << ' ' << in << " in\n";
// }
// }
for(int i=0; i<n; i++) done[i] = 0;
for(int i = 0; i < n; i++) {
if(i != A.f(i) || done[A.f(i)]) continue;
for(int x=0; x<n; x++) tag[x] = 0;
vector <int> vec; vec.pb(A.f(i));
for(int j=i+1; j < n; j++){
if(p[i][j] == 2 && A.f(j)==j){ // par nya aj
vec.pb(j);
}
}
if(vec.size() == 1) continue;
if(vec.size() == 2) return 0;
vector <int> node;
for(auto x : vec){
for(auto in : cc[x]){
node.pb(in); tag[in] = 1;
}
}
//for(auto in: node) cout << in << " node\n";
for(auto x : node){ // udh pernah connect
for(auto y : node){
if(x==y) continue;
if(p[x][y]==2 && A.con(x, y)) return 0;
}
}
//for(auto in : vec) cout << in << "pp\n";
for(auto in : node){
//cout << in << " in\n";
done[A.f(in)] = 1;
for(int x=0; x<n; x++){
if(x==in) continue;
if(tag[x] && !A.con(in, x) && p[in][x] != 2) return 0; //harusnya 1
if(!tag[x] && p[in][x] == 2) return 0;
}
}
vector <int> s;
for(auto in : node){
s.pb(A.f(in));
}
sort(s.begin(), s.end());
s.resize(unique(s.begin(), s.end()) - s.begin());
//for(auto in : s) cout << in << " s\n";
for(int i=0; i+1<s.size(); i++){ // build
bd(s[i], s[i+1]);
}
bd(s[0], s.back());
}
for(int i=0; i<n; i++) adj[i][i] = 0;
build(adj);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:77:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i=0; i+1<vec.size(); i++){ // build
| ~~~^~~~~~~~~~~
supertrees.cpp:141:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for(int i=0; i+1<s.size(); i++){ // build
| ~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1380 KB |
Output is correct |
7 |
Correct |
192 ms |
23488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1380 KB |
Output is correct |
7 |
Correct |
192 ms |
23488 KB |
Output is correct |
8 |
Correct |
1 ms |
484 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
360 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
7 ms |
1232 KB |
Output is correct |
13 |
Correct |
156 ms |
23464 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
3 ms |
964 KB |
Output is correct |
17 |
Correct |
65 ms |
13496 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
500 KB |
Output is correct |
20 |
Correct |
58 ms |
6308 KB |
Output is correct |
21 |
Correct |
153 ms |
23396 KB |
Output is correct |
22 |
Correct |
166 ms |
23652 KB |
Output is correct |
23 |
Correct |
159 ms |
23532 KB |
Output is correct |
24 |
Correct |
159 ms |
23516 KB |
Output is correct |
25 |
Correct |
63 ms |
13396 KB |
Output is correct |
26 |
Correct |
64 ms |
13468 KB |
Output is correct |
27 |
Correct |
175 ms |
23380 KB |
Output is correct |
28 |
Correct |
160 ms |
23476 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
1400 KB |
Output is correct |
9 |
Correct |
162 ms |
23496 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
7 ms |
1372 KB |
Output is correct |
13 |
Correct |
162 ms |
23476 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
964 KB |
Output is correct |
17 |
Correct |
69 ms |
13608 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
500 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
38 ms |
6188 KB |
Output is correct |
22 |
Correct |
157 ms |
23444 KB |
Output is correct |
23 |
Correct |
169 ms |
23336 KB |
Output is correct |
24 |
Correct |
161 ms |
23332 KB |
Output is correct |
25 |
Correct |
78 ms |
13592 KB |
Output is correct |
26 |
Correct |
67 ms |
13456 KB |
Output is correct |
27 |
Correct |
185 ms |
23320 KB |
Output is correct |
28 |
Correct |
161 ms |
23452 KB |
Output is correct |
29 |
Correct |
62 ms |
13416 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 |
42 ms |
6300 KB |
Output is correct |
5 |
Correct |
153 ms |
23300 KB |
Output is correct |
6 |
Correct |
156 ms |
23532 KB |
Output is correct |
7 |
Correct |
170 ms |
23340 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
46 ms |
6228 KB |
Output is correct |
10 |
Correct |
149 ms |
23444 KB |
Output is correct |
11 |
Correct |
171 ms |
23356 KB |
Output is correct |
12 |
Correct |
167 ms |
23644 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
37 ms |
6212 KB |
Output is correct |
17 |
Correct |
163 ms |
23436 KB |
Output is correct |
18 |
Correct |
241 ms |
23460 KB |
Output is correct |
19 |
Correct |
154 ms |
23324 KB |
Output is correct |
20 |
Correct |
166 ms |
23300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1380 KB |
Output is correct |
7 |
Correct |
192 ms |
23488 KB |
Output is correct |
8 |
Correct |
1 ms |
484 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
360 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
7 ms |
1232 KB |
Output is correct |
13 |
Correct |
156 ms |
23464 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
3 ms |
964 KB |
Output is correct |
17 |
Correct |
65 ms |
13496 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
500 KB |
Output is correct |
20 |
Correct |
58 ms |
6308 KB |
Output is correct |
21 |
Correct |
153 ms |
23396 KB |
Output is correct |
22 |
Correct |
166 ms |
23652 KB |
Output is correct |
23 |
Correct |
159 ms |
23532 KB |
Output is correct |
24 |
Correct |
159 ms |
23516 KB |
Output is correct |
25 |
Correct |
63 ms |
13396 KB |
Output is correct |
26 |
Correct |
64 ms |
13468 KB |
Output is correct |
27 |
Correct |
175 ms |
23380 KB |
Output is correct |
28 |
Correct |
160 ms |
23476 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1400 KB |
Output is correct |
37 |
Correct |
162 ms |
23496 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
1372 KB |
Output is correct |
41 |
Correct |
162 ms |
23476 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
964 KB |
Output is correct |
45 |
Correct |
69 ms |
13608 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
500 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
38 ms |
6188 KB |
Output is correct |
50 |
Correct |
157 ms |
23444 KB |
Output is correct |
51 |
Correct |
169 ms |
23336 KB |
Output is correct |
52 |
Correct |
161 ms |
23332 KB |
Output is correct |
53 |
Correct |
78 ms |
13592 KB |
Output is correct |
54 |
Correct |
67 ms |
13456 KB |
Output is correct |
55 |
Correct |
185 ms |
23320 KB |
Output is correct |
56 |
Correct |
161 ms |
23452 KB |
Output is correct |
57 |
Correct |
62 ms |
13416 KB |
Output is correct |
58 |
Correct |
1 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
988 KB |
Output is correct |
61 |
Correct |
70 ms |
13604 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
460 KB |
Output is correct |
64 |
Correct |
0 ms |
472 KB |
Output is correct |
65 |
Correct |
37 ms |
6356 KB |
Output is correct |
66 |
Correct |
78 ms |
13480 KB |
Output is correct |
67 |
Correct |
83 ms |
13476 KB |
Output is correct |
68 |
Correct |
77 ms |
13392 KB |
Output is correct |
69 |
Correct |
62 ms |
13392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
1380 KB |
Output is correct |
7 |
Correct |
192 ms |
23488 KB |
Output is correct |
8 |
Correct |
1 ms |
484 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
360 KB |
Output is correct |
11 |
Correct |
1 ms |
360 KB |
Output is correct |
12 |
Correct |
7 ms |
1232 KB |
Output is correct |
13 |
Correct |
156 ms |
23464 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
3 ms |
964 KB |
Output is correct |
17 |
Correct |
65 ms |
13496 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
500 KB |
Output is correct |
20 |
Correct |
58 ms |
6308 KB |
Output is correct |
21 |
Correct |
153 ms |
23396 KB |
Output is correct |
22 |
Correct |
166 ms |
23652 KB |
Output is correct |
23 |
Correct |
159 ms |
23532 KB |
Output is correct |
24 |
Correct |
159 ms |
23516 KB |
Output is correct |
25 |
Correct |
63 ms |
13396 KB |
Output is correct |
26 |
Correct |
64 ms |
13468 KB |
Output is correct |
27 |
Correct |
175 ms |
23380 KB |
Output is correct |
28 |
Correct |
160 ms |
23476 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
7 ms |
1400 KB |
Output is correct |
37 |
Correct |
162 ms |
23496 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
7 ms |
1372 KB |
Output is correct |
41 |
Correct |
162 ms |
23476 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
3 ms |
964 KB |
Output is correct |
45 |
Correct |
69 ms |
13608 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
500 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
38 ms |
6188 KB |
Output is correct |
50 |
Correct |
157 ms |
23444 KB |
Output is correct |
51 |
Correct |
169 ms |
23336 KB |
Output is correct |
52 |
Correct |
161 ms |
23332 KB |
Output is correct |
53 |
Correct |
78 ms |
13592 KB |
Output is correct |
54 |
Correct |
67 ms |
13456 KB |
Output is correct |
55 |
Correct |
185 ms |
23320 KB |
Output is correct |
56 |
Correct |
161 ms |
23452 KB |
Output is correct |
57 |
Correct |
62 ms |
13416 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 |
42 ms |
6300 KB |
Output is correct |
62 |
Correct |
153 ms |
23300 KB |
Output is correct |
63 |
Correct |
156 ms |
23532 KB |
Output is correct |
64 |
Correct |
170 ms |
23340 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
46 ms |
6228 KB |
Output is correct |
67 |
Correct |
149 ms |
23444 KB |
Output is correct |
68 |
Correct |
171 ms |
23356 KB |
Output is correct |
69 |
Correct |
167 ms |
23644 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
464 KB |
Output is correct |
72 |
Correct |
1 ms |
344 KB |
Output is correct |
73 |
Correct |
37 ms |
6212 KB |
Output is correct |
74 |
Correct |
163 ms |
23436 KB |
Output is correct |
75 |
Correct |
241 ms |
23460 KB |
Output is correct |
76 |
Correct |
154 ms |
23324 KB |
Output is correct |
77 |
Correct |
166 ms |
23300 KB |
Output is correct |
78 |
Correct |
1 ms |
344 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
3 ms |
988 KB |
Output is correct |
81 |
Correct |
70 ms |
13604 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
1 ms |
460 KB |
Output is correct |
84 |
Correct |
0 ms |
472 KB |
Output is correct |
85 |
Correct |
37 ms |
6356 KB |
Output is correct |
86 |
Correct |
78 ms |
13480 KB |
Output is correct |
87 |
Correct |
83 ms |
13476 KB |
Output is correct |
88 |
Correct |
77 ms |
13392 KB |
Output is correct |
89 |
Correct |
62 ms |
13392 KB |
Output is correct |
90 |
Correct |
1 ms |
600 KB |
Output is correct |
91 |
Correct |
1 ms |
344 KB |
Output is correct |
92 |
Correct |
3 ms |
860 KB |
Output is correct |
93 |
Correct |
61 ms |
13604 KB |
Output is correct |
94 |
Correct |
1 ms |
344 KB |
Output is correct |
95 |
Correct |
0 ms |
348 KB |
Output is correct |
96 |
Correct |
0 ms |
348 KB |
Output is correct |
97 |
Correct |
23 ms |
3932 KB |
Output is correct |
98 |
Correct |
60 ms |
14236 KB |
Output is correct |
99 |
Correct |
63 ms |
14120 KB |
Output is correct |
100 |
Correct |
66 ms |
14292 KB |
Output is correct |
101 |
Correct |
62 ms |
14108 KB |
Output is correct |
102 |
Correct |
62 ms |
14160 KB |
Output is correct |
103 |
Correct |
81 ms |
14120 KB |
Output is correct |
104 |
Correct |
60 ms |
14164 KB |
Output is correct |
105 |
Correct |
66 ms |
14104 KB |
Output is correct |