#include <bits/stdc++.h>
#include "supertrees.h"
#define DIMN 1010
using namespace std;
int tt[DIMN] , c[DIMN] , f[DIMN] , t[DIMN];
vector <int> x , cycle;
int root (int x){
while (tt[x] > 0)
x = tt[x];
return x;
}
int roott (int x){
while (t[x] > 0)
x = t[x];
return x;
}
int construct(vector<vector<int>> p) {
int n = p.size() , i , ri , rj , j , caz , r;
vector<vector<int>> answer;
for (i = 0; i < n; i++) {
vector<int> row;
row.resize(n);
answer.push_back(row);
}
for (i = 0 ; i < n ; i++)
tt[i] = -1;
for (i = 0 ; i < n ; i++){
f[i] = 0;
c[i] = 0;
for (j = 0 ; j < n ; j++){
if (p[i][j] == 3)
return 0; /// ce teapa monumentala
if (p[i][j] && i != j){
c[i] = (c[i] | p[i][j]);
/// i si j in aceeasi componenta conexa
ri = root(i);
rj = root(j);
if (ri != rj){
if (tt[ri] < tt[rj]){
tt[ri] += tt[rj];
tt[rj] = ri;
}
else {
tt[rj] += tt[ri];
tt[ri] = rj;
}
}
}
}
}
for (i = 0 ; i < n ; i++){
for (j = 0 ; j < n ; j++){
if (!p[i][j]){
/// i si j in aceeasi componenta conexa
ri = root(i);
rj = root(j);
if (ri == rj) /// sunt in aceeasi cc si n ar trb sa fie
return 0;
}
}
}
for (r = 0 ; r < n ; r++){
if (tt[r] < 0){
x.clear();
for (i = 0 ; i < n ; i++){
if (root(i) == r)
x.push_back(i);
}
/// x = componenta conexa de acum
caz = 0;
for (i = 0 ; i < x.size() ; i++){
caz = (caz | c[x[i]]);
}
if (caz == 0)
continue;
else if (caz == 1){ /// lant
if (x.size() < 2)
return 0;
for (i = 0 ; i < x.size() - 1 ; i++){
answer[x[i]][x[i + 1]] = answer[x[i + 1]][x[i]] = 1;
}
}
else if (caz == 2){ /// un ciclu simplu
if (x.size() < 3)
return 0;
for (i = 0 ; i < x.size() - 1 ; i++){
answer[x[i]][x[i + 1]] = answer[x[i + 1]][x[i]] = 1;
}
answer[x[0]][x.back()] = answer[x.back()][x[0]] = 1;
}
else { /// cazul cand sunt si 1 si 2
for (i = 0 ; i < n ; i++)
t[i] = -1;
cycle.clear();
int ok = 0;
for (i = 0 ; i < x.size() ; i++){
if (c[x[i]] == 2){
cycle.push_back(x[i]);
f[x[i]] = 1;
}
else if (c[x[i]] == 1){
return 0;
}
}
for (i = 0 ; i < x.size() ; i++){
for (j = 0 ; j < x.size() ; j++){
//if (r == 24)
//printf ("a");
if (i != j && p[x[i]][x[j]] == 1){
ri = roott(x[i]);
rj = roott(x[j]);
if (ri != rj){
answer[ri][rj] = answer[rj][ri] = 1;
if (t[ri] < t[rj]){
t[ri] += t[rj];
t[rj] = ri;
}
else {
t[rj] += t[ri];
t[ri] = rj;
}
}
}
}
}
for (i = 0 ; i < x.size() ; i++){
if (c[x[i]] == 3){
ri = roott(x[i]);
if (!f[ri]){
f[ri] = 1;
cycle.push_back(ri);
}
}
}
if (cycle.size() < 3){
return 0;
}
for (i = 0 ; i < cycle.size() - 1 ; i++){
answer[cycle[i]][cycle[i + 1]] = answer[cycle[i + 1]][cycle[i]] = 1;
}
answer[cycle[0]][cycle.back()] = answer[cycle.back()][cycle[0]] = 1;
}
}
}
//printf ("%d\n\n",answer[0][228]);
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:108:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | for (i = 0 ; i < x.size() ; i++){
| ~~^~~~~~~~~~
supertrees.cpp:118:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | for (i = 0 ; i < x.size() - 1 ; i++){
| ~~^~~~~~~~~~~~~~
supertrees.cpp:128:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | for (i = 0 ; i < x.size() - 1 ; i++){
| ~~^~~~~~~~~~~~~~
supertrees.cpp:143:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
143 | for (i = 0 ; i < x.size() ; i++){
| ~~^~~~~~~~~~
supertrees.cpp:153:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
153 | for (i = 0 ; i < x.size() ; i++){
| ~~^~~~~~~~~~
supertrees.cpp:154:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
154 | for (j = 0 ; j < x.size() ; j++){
| ~~^~~~~~~~~~
supertrees.cpp:185:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
185 | for (i = 0 ; i < x.size() ; i++){
| ~~^~~~~~~~~~
supertrees.cpp:203:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
203 | for (i = 0 ; i < cycle.size() - 1 ; i++){
| ~~^~~~~~~~~~~~~~~~~~
supertrees.cpp:142:21: warning: unused variable 'ok' [-Wunused-variable]
142 | int ok = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
13 ms |
1180 KB |
Output is correct |
7 |
Correct |
248 ms |
22136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
13 ms |
1180 KB |
Output is correct |
7 |
Correct |
248 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
241 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
116 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
60 ms |
5880 KB |
Output is correct |
21 |
Correct |
245 ms |
22136 KB |
Output is correct |
22 |
Correct |
246 ms |
22264 KB |
Output is correct |
23 |
Correct |
257 ms |
22136 KB |
Output is correct |
24 |
Correct |
241 ms |
22136 KB |
Output is correct |
25 |
Correct |
101 ms |
12156 KB |
Output is correct |
26 |
Correct |
97 ms |
12152 KB |
Output is correct |
27 |
Correct |
259 ms |
22136 KB |
Output is correct |
28 |
Correct |
246 ms |
22264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 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 |
239 ms |
22104 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 |
251 ms |
22136 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
7 ms |
768 KB |
Output is correct |
17 |
Correct |
113 ms |
12228 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
256 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
62 ms |
5880 KB |
Output is correct |
22 |
Correct |
250 ms |
22136 KB |
Output is correct |
23 |
Correct |
241 ms |
22108 KB |
Output is correct |
24 |
Correct |
258 ms |
22264 KB |
Output is correct |
25 |
Correct |
102 ms |
12152 KB |
Output is correct |
26 |
Correct |
105 ms |
12152 KB |
Output is correct |
27 |
Correct |
240 ms |
22136 KB |
Output is correct |
28 |
Correct |
252 ms |
22136 KB |
Output is correct |
29 |
Correct |
101 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
60 ms |
5880 KB |
Output is correct |
5 |
Correct |
243 ms |
22136 KB |
Output is correct |
6 |
Correct |
244 ms |
22264 KB |
Output is correct |
7 |
Correct |
254 ms |
22108 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
62 ms |
5880 KB |
Output is correct |
10 |
Correct |
247 ms |
22136 KB |
Output is correct |
11 |
Correct |
238 ms |
22136 KB |
Output is correct |
12 |
Correct |
260 ms |
22264 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
62 ms |
5800 KB |
Output is correct |
17 |
Correct |
240 ms |
22136 KB |
Output is correct |
18 |
Correct |
248 ms |
22136 KB |
Output is correct |
19 |
Correct |
250 ms |
22136 KB |
Output is correct |
20 |
Correct |
242 ms |
22136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
13 ms |
1180 KB |
Output is correct |
7 |
Correct |
248 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
241 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
116 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
60 ms |
5880 KB |
Output is correct |
21 |
Correct |
245 ms |
22136 KB |
Output is correct |
22 |
Correct |
246 ms |
22264 KB |
Output is correct |
23 |
Correct |
257 ms |
22136 KB |
Output is correct |
24 |
Correct |
241 ms |
22136 KB |
Output is correct |
25 |
Correct |
101 ms |
12156 KB |
Output is correct |
26 |
Correct |
97 ms |
12152 KB |
Output is correct |
27 |
Correct |
259 ms |
22136 KB |
Output is correct |
28 |
Correct |
246 ms |
22264 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
0 ms |
384 KB |
Output is correct |
31 |
Correct |
0 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 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 |
239 ms |
22104 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 |
1280 KB |
Output is correct |
41 |
Correct |
251 ms |
22136 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
7 ms |
768 KB |
Output is correct |
45 |
Correct |
113 ms |
12228 KB |
Output is correct |
46 |
Correct |
0 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
256 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
62 ms |
5880 KB |
Output is correct |
50 |
Correct |
250 ms |
22136 KB |
Output is correct |
51 |
Correct |
241 ms |
22108 KB |
Output is correct |
52 |
Correct |
258 ms |
22264 KB |
Output is correct |
53 |
Correct |
102 ms |
12152 KB |
Output is correct |
54 |
Correct |
105 ms |
12152 KB |
Output is correct |
55 |
Correct |
240 ms |
22136 KB |
Output is correct |
56 |
Correct |
252 ms |
22136 KB |
Output is correct |
57 |
Correct |
101 ms |
12152 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 |
768 KB |
Output is correct |
61 |
Correct |
115 ms |
12152 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 |
5880 KB |
Output is correct |
66 |
Correct |
108 ms |
12152 KB |
Output is correct |
67 |
Correct |
110 ms |
12152 KB |
Output is correct |
68 |
Correct |
104 ms |
12152 KB |
Output is correct |
69 |
Correct |
104 ms |
12156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
13 ms |
1180 KB |
Output is correct |
7 |
Correct |
248 ms |
22136 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
10 ms |
1280 KB |
Output is correct |
13 |
Correct |
241 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
768 KB |
Output is correct |
17 |
Correct |
116 ms |
12152 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
60 ms |
5880 KB |
Output is correct |
21 |
Correct |
245 ms |
22136 KB |
Output is correct |
22 |
Correct |
246 ms |
22264 KB |
Output is correct |
23 |
Correct |
257 ms |
22136 KB |
Output is correct |
24 |
Correct |
241 ms |
22136 KB |
Output is correct |
25 |
Correct |
101 ms |
12156 KB |
Output is correct |
26 |
Correct |
97 ms |
12152 KB |
Output is correct |
27 |
Correct |
259 ms |
22136 KB |
Output is correct |
28 |
Correct |
246 ms |
22264 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
0 ms |
384 KB |
Output is correct |
31 |
Correct |
0 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 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 |
239 ms |
22104 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 |
1280 KB |
Output is correct |
41 |
Correct |
251 ms |
22136 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
7 ms |
768 KB |
Output is correct |
45 |
Correct |
113 ms |
12228 KB |
Output is correct |
46 |
Correct |
0 ms |
384 KB |
Output is correct |
47 |
Correct |
0 ms |
256 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
62 ms |
5880 KB |
Output is correct |
50 |
Correct |
250 ms |
22136 KB |
Output is correct |
51 |
Correct |
241 ms |
22108 KB |
Output is correct |
52 |
Correct |
258 ms |
22264 KB |
Output is correct |
53 |
Correct |
102 ms |
12152 KB |
Output is correct |
54 |
Correct |
105 ms |
12152 KB |
Output is correct |
55 |
Correct |
240 ms |
22136 KB |
Output is correct |
56 |
Correct |
252 ms |
22136 KB |
Output is correct |
57 |
Correct |
101 ms |
12152 KB |
Output is correct |
58 |
Correct |
1 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 |
60 ms |
5880 KB |
Output is correct |
62 |
Correct |
243 ms |
22136 KB |
Output is correct |
63 |
Correct |
244 ms |
22264 KB |
Output is correct |
64 |
Correct |
254 ms |
22108 KB |
Output is correct |
65 |
Correct |
1 ms |
384 KB |
Output is correct |
66 |
Correct |
62 ms |
5880 KB |
Output is correct |
67 |
Correct |
247 ms |
22136 KB |
Output is correct |
68 |
Correct |
238 ms |
22136 KB |
Output is correct |
69 |
Correct |
260 ms |
22264 KB |
Output is correct |
70 |
Correct |
0 ms |
384 KB |
Output is correct |
71 |
Correct |
1 ms |
384 KB |
Output is correct |
72 |
Correct |
1 ms |
512 KB |
Output is correct |
73 |
Correct |
62 ms |
5800 KB |
Output is correct |
74 |
Correct |
240 ms |
22136 KB |
Output is correct |
75 |
Correct |
248 ms |
22136 KB |
Output is correct |
76 |
Correct |
250 ms |
22136 KB |
Output is correct |
77 |
Correct |
242 ms |
22136 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 |
768 KB |
Output is correct |
81 |
Correct |
115 ms |
12152 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 |
5880 KB |
Output is correct |
86 |
Correct |
108 ms |
12152 KB |
Output is correct |
87 |
Correct |
110 ms |
12152 KB |
Output is correct |
88 |
Correct |
104 ms |
12152 KB |
Output is correct |
89 |
Correct |
104 ms |
12156 KB |
Output is correct |
90 |
Correct |
0 ms |
384 KB |
Output is correct |
91 |
Correct |
1 ms |
384 KB |
Output is correct |
92 |
Correct |
5 ms |
768 KB |
Output is correct |
93 |
Correct |
106 ms |
12152 KB |
Output is correct |
94 |
Correct |
1 ms |
256 KB |
Output is correct |
95 |
Correct |
1 ms |
384 KB |
Output is correct |
96 |
Correct |
1 ms |
384 KB |
Output is correct |
97 |
Correct |
24 ms |
3328 KB |
Output is correct |
98 |
Correct |
101 ms |
12152 KB |
Output is correct |
99 |
Correct |
97 ms |
12152 KB |
Output is correct |
100 |
Correct |
99 ms |
12152 KB |
Output is correct |
101 |
Correct |
100 ms |
12280 KB |
Output is correct |
102 |
Correct |
107 ms |
12152 KB |
Output is correct |
103 |
Correct |
106 ms |
12152 KB |
Output is correct |
104 |
Correct |
109 ms |
12152 KB |
Output is correct |
105 |
Correct |
108 ms |
12280 KB |
Output is correct |