#include <bits/stdc++.h>
#include "supertrees.h"
#define DIM 1010
using namespace std;
int t[DIM],t2[DIM],a[DIM][DIM],f[DIM],last[DIM];
vector <int> v[DIM],w;
/*void build (vector <vector <int> > a){
int n = a.size();
for (int i=0;i<n;i++,cout<<"\n")
for (int j=0;j<n;j++)
cout<<a[i][j]<<" ";
}*/
int get_rad (int t[], int x){
while (t[x] > 0)
x = t[x];
return x;
}
void uneste (int t[], int x, int y){
int radx = get_rad (t,x), rady = get_rad (t,y);
if (radx == rady)
return;
if (t[radx] > t[rady])
swap (radx,rady);
t[radx] += t[rady];
t[rady] = radx;
}
int construct (vector <vector<int> > p){
int n = p.size();
memset (t,-1,sizeof t);
memset (f,0,sizeof f);
for (int i=1;i<=n;i++)
v[i].clear();
int ok = 0;
for (int i=0;i<n;i++)
for (int j=i+1;j<n;j++)
if (p[i][j]){
uneste (t,i+1,j+1);
if (p[i][j] == 3)
ok = 1;
}
if (ok)
return 0;
int nr_comp = 0;
for (int i=1;i<=n;i++){
int rad = get_rad (t,i);
if (!f[rad])
f[rad] = ++nr_comp;
/// mai verific daca e conectat cu toate nodurile din componenta
for (auto it : v[f[rad]]){
if (p[it-1][i-1] == 0)
return 0;
}
v[f[rad]].push_back(i);
}
for (int i=1;i<=nr_comp;i++){
/// fac iar paduri cu nodurile din multimea asta
memset (t2,-1,sizeof t2);
memset (last,0,sizeof last);
for (auto it : v[i]){
for (auto it2 : v[i]){
if (it != it2 && p[it-1][it2-1] == 1)
uneste (t2,it,it2);
}}
w.clear();
for (auto it : v[i]){
int rad = get_rad (t2,it);
if (t2[rad] == -1){
w.push_back(it);
continue;
}
if (!last[rad]){
last[rad] = it;
w.push_back(it);
} else {
a[last[rad]][it] = a[it][last[rad]] = 1;
last[rad] = it;
}
}
if (w.size() == 2)
return 0;
for (int j=0;j<w.size()-1;j++)
a[w[j]][w[j+1]] = a[w[j+1]][w[j]] = 1;
if (w.size() > 1)
a[w[0]][ w[w.size()-1] ] = a[ w[w.size()-1] ][w[0]] = 1;
}
vector <vector<int> > ans;
for (int i=1;i<=n;i++){
vector <int> lin;
for (int j=1;j<=n;j++)
lin.push_back(a[i][j]);
ans.push_back (lin);
}
build (ans);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:102:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for (int j=0;j<w.size()-1;j++)
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
2048 KB |
Output is correct |
7 |
Correct |
248 ms |
26104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
2048 KB |
Output is correct |
7 |
Correct |
248 ms |
26104 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
241 ms |
22264 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
109 ms |
8308 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
62 ms |
7672 KB |
Output is correct |
21 |
Correct |
245 ms |
26100 KB |
Output is correct |
22 |
Correct |
245 ms |
25720 KB |
Output is correct |
23 |
Correct |
262 ms |
26232 KB |
Output is correct |
24 |
Correct |
245 ms |
22392 KB |
Output is correct |
25 |
Correct |
104 ms |
8188 KB |
Output is correct |
26 |
Correct |
108 ms |
8312 KB |
Output is correct |
27 |
Correct |
257 ms |
26104 KB |
Output is correct |
28 |
Correct |
240 ms |
22392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
10 ms |
1280 KB |
Output is correct |
9 |
Correct |
249 ms |
22132 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 |
2048 KB |
Output is correct |
13 |
Correct |
251 ms |
26104 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 |
640 KB |
Output is correct |
17 |
Correct |
111 ms |
8188 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
65 ms |
7672 KB |
Output is correct |
22 |
Correct |
242 ms |
26108 KB |
Output is correct |
23 |
Correct |
245 ms |
26104 KB |
Output is correct |
24 |
Correct |
257 ms |
26108 KB |
Output is correct |
25 |
Correct |
101 ms |
8568 KB |
Output is correct |
26 |
Correct |
102 ms |
8184 KB |
Output is correct |
27 |
Correct |
247 ms |
26232 KB |
Output is correct |
28 |
Correct |
250 ms |
26104 KB |
Output is correct |
29 |
Correct |
100 ms |
8568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
62 ms |
7672 KB |
Output is correct |
5 |
Correct |
245 ms |
26236 KB |
Output is correct |
6 |
Correct |
246 ms |
25720 KB |
Output is correct |
7 |
Correct |
254 ms |
26104 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
64 ms |
7768 KB |
Output is correct |
10 |
Correct |
246 ms |
26088 KB |
Output is correct |
11 |
Correct |
243 ms |
26232 KB |
Output is correct |
12 |
Correct |
260 ms |
26232 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
61 ms |
7672 KB |
Output is correct |
17 |
Correct |
243 ms |
26104 KB |
Output is correct |
18 |
Correct |
242 ms |
25980 KB |
Output is correct |
19 |
Correct |
243 ms |
26104 KB |
Output is correct |
20 |
Correct |
245 ms |
22520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
2048 KB |
Output is correct |
7 |
Correct |
248 ms |
26104 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
241 ms |
22264 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
109 ms |
8308 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
62 ms |
7672 KB |
Output is correct |
21 |
Correct |
245 ms |
26100 KB |
Output is correct |
22 |
Correct |
245 ms |
25720 KB |
Output is correct |
23 |
Correct |
262 ms |
26232 KB |
Output is correct |
24 |
Correct |
245 ms |
22392 KB |
Output is correct |
25 |
Correct |
104 ms |
8188 KB |
Output is correct |
26 |
Correct |
108 ms |
8312 KB |
Output is correct |
27 |
Correct |
257 ms |
26104 KB |
Output is correct |
28 |
Correct |
240 ms |
22392 KB |
Output is correct |
29 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
10 ms |
1280 KB |
Output is correct |
37 |
Correct |
249 ms |
22132 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 |
2048 KB |
Output is correct |
41 |
Correct |
251 ms |
26104 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 |
640 KB |
Output is correct |
45 |
Correct |
111 ms |
8188 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
65 ms |
7672 KB |
Output is correct |
50 |
Correct |
242 ms |
26108 KB |
Output is correct |
51 |
Correct |
245 ms |
26104 KB |
Output is correct |
52 |
Correct |
257 ms |
26108 KB |
Output is correct |
53 |
Correct |
101 ms |
8568 KB |
Output is correct |
54 |
Correct |
102 ms |
8184 KB |
Output is correct |
55 |
Correct |
247 ms |
26232 KB |
Output is correct |
56 |
Correct |
250 ms |
26104 KB |
Output is correct |
57 |
Correct |
100 ms |
8568 KB |
Output is correct |
58 |
Correct |
0 ms |
384 KB |
Output is correct |
59 |
Correct |
0 ms |
384 KB |
Output is correct |
60 |
Correct |
5 ms |
640 KB |
Output is correct |
61 |
Correct |
108 ms |
8184 KB |
Output is correct |
62 |
Correct |
1 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 |
61 ms |
7672 KB |
Output is correct |
66 |
Correct |
102 ms |
8312 KB |
Output is correct |
67 |
Correct |
107 ms |
8184 KB |
Output is correct |
68 |
Correct |
104 ms |
8176 KB |
Output is correct |
69 |
Correct |
105 ms |
8308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
11 ms |
2048 KB |
Output is correct |
7 |
Correct |
248 ms |
26104 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 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 |
11 ms |
1280 KB |
Output is correct |
13 |
Correct |
241 ms |
22264 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
109 ms |
8308 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
62 ms |
7672 KB |
Output is correct |
21 |
Correct |
245 ms |
26100 KB |
Output is correct |
22 |
Correct |
245 ms |
25720 KB |
Output is correct |
23 |
Correct |
262 ms |
26232 KB |
Output is correct |
24 |
Correct |
245 ms |
22392 KB |
Output is correct |
25 |
Correct |
104 ms |
8188 KB |
Output is correct |
26 |
Correct |
108 ms |
8312 KB |
Output is correct |
27 |
Correct |
257 ms |
26104 KB |
Output is correct |
28 |
Correct |
240 ms |
22392 KB |
Output is correct |
29 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
10 ms |
1280 KB |
Output is correct |
37 |
Correct |
249 ms |
22132 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 |
2048 KB |
Output is correct |
41 |
Correct |
251 ms |
26104 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 |
640 KB |
Output is correct |
45 |
Correct |
111 ms |
8188 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
65 ms |
7672 KB |
Output is correct |
50 |
Correct |
242 ms |
26108 KB |
Output is correct |
51 |
Correct |
245 ms |
26104 KB |
Output is correct |
52 |
Correct |
257 ms |
26108 KB |
Output is correct |
53 |
Correct |
101 ms |
8568 KB |
Output is correct |
54 |
Correct |
102 ms |
8184 KB |
Output is correct |
55 |
Correct |
247 ms |
26232 KB |
Output is correct |
56 |
Correct |
250 ms |
26104 KB |
Output is correct |
57 |
Correct |
100 ms |
8568 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 |
62 ms |
7672 KB |
Output is correct |
62 |
Correct |
245 ms |
26236 KB |
Output is correct |
63 |
Correct |
246 ms |
25720 KB |
Output is correct |
64 |
Correct |
254 ms |
26104 KB |
Output is correct |
65 |
Correct |
1 ms |
384 KB |
Output is correct |
66 |
Correct |
64 ms |
7768 KB |
Output is correct |
67 |
Correct |
246 ms |
26088 KB |
Output is correct |
68 |
Correct |
243 ms |
26232 KB |
Output is correct |
69 |
Correct |
260 ms |
26232 KB |
Output is correct |
70 |
Correct |
1 ms |
384 KB |
Output is correct |
71 |
Correct |
0 ms |
384 KB |
Output is correct |
72 |
Correct |
0 ms |
384 KB |
Output is correct |
73 |
Correct |
61 ms |
7672 KB |
Output is correct |
74 |
Correct |
243 ms |
26104 KB |
Output is correct |
75 |
Correct |
242 ms |
25980 KB |
Output is correct |
76 |
Correct |
243 ms |
26104 KB |
Output is correct |
77 |
Correct |
245 ms |
22520 KB |
Output is correct |
78 |
Correct |
0 ms |
384 KB |
Output is correct |
79 |
Correct |
0 ms |
384 KB |
Output is correct |
80 |
Correct |
5 ms |
640 KB |
Output is correct |
81 |
Correct |
108 ms |
8184 KB |
Output is correct |
82 |
Correct |
1 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 |
61 ms |
7672 KB |
Output is correct |
86 |
Correct |
102 ms |
8312 KB |
Output is correct |
87 |
Correct |
107 ms |
8184 KB |
Output is correct |
88 |
Correct |
104 ms |
8176 KB |
Output is correct |
89 |
Correct |
105 ms |
8308 KB |
Output is correct |
90 |
Correct |
0 ms |
384 KB |
Output is correct |
91 |
Correct |
0 ms |
384 KB |
Output is correct |
92 |
Correct |
5 ms |
640 KB |
Output is correct |
93 |
Correct |
105 ms |
8184 KB |
Output is correct |
94 |
Correct |
0 ms |
384 KB |
Output is correct |
95 |
Correct |
1 ms |
384 KB |
Output is correct |
96 |
Correct |
1 ms |
384 KB |
Output is correct |
97 |
Correct |
25 ms |
2304 KB |
Output is correct |
98 |
Correct |
102 ms |
8184 KB |
Output is correct |
99 |
Correct |
104 ms |
8184 KB |
Output is correct |
100 |
Correct |
102 ms |
8184 KB |
Output is correct |
101 |
Correct |
100 ms |
8184 KB |
Output is correct |
102 |
Correct |
102 ms |
8184 KB |
Output is correct |
103 |
Correct |
101 ms |
8312 KB |
Output is correct |
104 |
Correct |
100 ms |
8312 KB |
Output is correct |
105 |
Correct |
110 ms |
8188 KB |
Output is correct |