#include "supertrees.h"
#include <vector>
#include <iostream>
using namespace std;
vector<vector<int>> answer;
#define nmax 1000
int daddy[nmax], daddy_cyc[nmax];
vector<int> cyc[nmax];
int find_daddy( int a ) {
int x;
if( daddy[a] == a )
return a;
x = find_daddy( daddy[a] );
daddy[a] = x;
return x;
}
void marry_daddy( int a, int b ) {
int x, y;
x = find_daddy( a );
y = find_daddy( b );
daddy[x] = y;
}
int find_daddy_cyc( int a ) {
int x;
if( daddy_cyc[a] == a )
return a;
x = find_daddy_cyc( daddy_cyc[a] );
daddy_cyc[a] = x;
return x;
}
void marry_daddy_cyc( int a, int b ) {
int x, y;
x = find_daddy_cyc( a );
y = find_daddy_cyc( b );
daddy_cyc[x] = y;
}
int construct( vector<vector<int>> p) {
int n = p.size();
for (int i = 0; i < n; i++) {
vector<int> row;
row.resize(n);
answer.push_back(row);
}
int i, j;
bool ok = true;
for( i = 0; i < n; i++ ) {
for( j = 0; j < n; j++ )
if( p[i][j] == 3 )
ok = false;
}
if( !ok )
return 0;
for( i = 0; i < n; i++ )
daddy[i] = daddy_cyc[i] = i;
for( i = 0; i < n; i++ ) {
for( j = 0; j < n; j++ )
if( p[i][j] == 1 )
marry_daddy( i, j );
}
for( i = 0; i < n; i++ ) {
for( j = 0; j < n; j++ )
if( p[i][j] != 1 && find_daddy(i) == find_daddy(j) )
ok = false;
}
if( !ok )
return 0;
for( i = 0; i < n; i++ )
if( find_daddy(i) != i ) {
answer[i][find_daddy(i)] = 1;
answer[find_daddy(i)][i] = 1;
}
for( i = 0; i < n; i++ ) {
for( j = 0; j < n; j++ )
if( p[i][j] == 2 )
marry_daddy_cyc(find_daddy(i), find_daddy(j) );
}
for( i = 0; i < n; i++ ) {
for( j = 0; j < n; j++ )
if( p[i][j] < 2 && find_daddy(i) != find_daddy(j) && find_daddy_cyc(find_daddy(i)) == find_daddy_cyc(find_daddy(j)) )
ok = false;
}
if( !ok )
return 0;
for( i = 0; i < n; i++ ) {
if( find_daddy_cyc(i) != i )
cyc[find_daddy_cyc(i)].push_back(i);
}
for( i = 0; i < n; i++ ) {
if( cyc[i].size() == 1 )
ok = false;
if( cyc[i].size() >= 2 ) {
for( j = 0; j < cyc[i].size() - 1; j++ ) {
answer[cyc[i][j+1]][cyc[i][j]] = 1;
answer[cyc[i][j]][cyc[i][j+1]] = 1;
}
answer[i][cyc[i][0]] = 1;
answer[cyc[i][0]][i] = 1;
answer[i][cyc[i][j]] = 1;
answer[cyc[i][j]][i] = 1;
}
}
if( !ok )
return 0;
build(answer);
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:102:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for( j = 0; j < cyc[i].size() - 1; j++ ) {
| ~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
106 ms |
22068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
106 ms |
22068 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1132 KB |
Output is correct |
13 |
Correct |
125 ms |
22068 KB |
Output is correct |
14 |
Correct |
0 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 |
75 ms |
12088 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
5872 KB |
Output is correct |
21 |
Correct |
111 ms |
22096 KB |
Output is correct |
22 |
Correct |
125 ms |
22060 KB |
Output is correct |
23 |
Correct |
117 ms |
22096 KB |
Output is correct |
24 |
Correct |
120 ms |
22040 KB |
Output is correct |
25 |
Correct |
52 ms |
12068 KB |
Output is correct |
26 |
Correct |
53 ms |
12332 KB |
Output is correct |
27 |
Correct |
122 ms |
22068 KB |
Output is correct |
28 |
Correct |
121 ms |
22100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
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 |
112 ms |
22144 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
1116 KB |
Output is correct |
13 |
Correct |
115 ms |
22180 KB |
Output is correct |
14 |
Correct |
0 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 |
68 ms |
12136 KB |
Output is correct |
18 |
Correct |
1 ms |
348 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 |
5740 KB |
Output is correct |
22 |
Correct |
119 ms |
22096 KB |
Output is correct |
23 |
Correct |
116 ms |
22096 KB |
Output is correct |
24 |
Correct |
114 ms |
22100 KB |
Output is correct |
25 |
Correct |
55 ms |
12312 KB |
Output is correct |
26 |
Correct |
62 ms |
12084 KB |
Output is correct |
27 |
Correct |
121 ms |
22096 KB |
Output is correct |
28 |
Correct |
111 ms |
22100 KB |
Output is correct |
29 |
Correct |
58 ms |
12120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
28 ms |
5872 KB |
Output is correct |
5 |
Correct |
114 ms |
22096 KB |
Output is correct |
6 |
Correct |
112 ms |
22100 KB |
Output is correct |
7 |
Correct |
119 ms |
22064 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
30 ms |
5700 KB |
Output is correct |
10 |
Correct |
109 ms |
22100 KB |
Output is correct |
11 |
Correct |
131 ms |
22064 KB |
Output is correct |
12 |
Correct |
116 ms |
22064 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
29 ms |
5896 KB |
Output is correct |
17 |
Correct |
108 ms |
22096 KB |
Output is correct |
18 |
Correct |
118 ms |
22096 KB |
Output is correct |
19 |
Correct |
115 ms |
22096 KB |
Output is correct |
20 |
Correct |
108 ms |
22068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
106 ms |
22068 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1132 KB |
Output is correct |
13 |
Correct |
125 ms |
22068 KB |
Output is correct |
14 |
Correct |
0 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 |
75 ms |
12088 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
5872 KB |
Output is correct |
21 |
Correct |
111 ms |
22096 KB |
Output is correct |
22 |
Correct |
125 ms |
22060 KB |
Output is correct |
23 |
Correct |
117 ms |
22096 KB |
Output is correct |
24 |
Correct |
120 ms |
22040 KB |
Output is correct |
25 |
Correct |
52 ms |
12068 KB |
Output is correct |
26 |
Correct |
53 ms |
12332 KB |
Output is correct |
27 |
Correct |
122 ms |
22068 KB |
Output is correct |
28 |
Correct |
121 ms |
22100 KB |
Output is correct |
29 |
Correct |
0 ms |
348 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 |
1 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 |
112 ms |
22144 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1116 KB |
Output is correct |
41 |
Correct |
115 ms |
22180 KB |
Output is correct |
42 |
Correct |
0 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 |
68 ms |
12136 KB |
Output is correct |
46 |
Correct |
1 ms |
348 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 |
5740 KB |
Output is correct |
50 |
Correct |
119 ms |
22096 KB |
Output is correct |
51 |
Correct |
116 ms |
22096 KB |
Output is correct |
52 |
Correct |
114 ms |
22100 KB |
Output is correct |
53 |
Correct |
55 ms |
12312 KB |
Output is correct |
54 |
Correct |
62 ms |
12084 KB |
Output is correct |
55 |
Correct |
121 ms |
22096 KB |
Output is correct |
56 |
Correct |
111 ms |
22100 KB |
Output is correct |
57 |
Correct |
58 ms |
12120 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
3 ms |
860 KB |
Output is correct |
61 |
Correct |
59 ms |
12084 KB |
Output is correct |
62 |
Correct |
1 ms |
344 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
31 ms |
5808 KB |
Output is correct |
66 |
Correct |
56 ms |
12068 KB |
Output is correct |
67 |
Correct |
50 ms |
12116 KB |
Output is correct |
68 |
Correct |
61 ms |
12084 KB |
Output is correct |
69 |
Correct |
51 ms |
12304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 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 |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
106 ms |
22068 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
1132 KB |
Output is correct |
13 |
Correct |
125 ms |
22068 KB |
Output is correct |
14 |
Correct |
0 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 |
75 ms |
12088 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
30 ms |
5872 KB |
Output is correct |
21 |
Correct |
111 ms |
22096 KB |
Output is correct |
22 |
Correct |
125 ms |
22060 KB |
Output is correct |
23 |
Correct |
117 ms |
22096 KB |
Output is correct |
24 |
Correct |
120 ms |
22040 KB |
Output is correct |
25 |
Correct |
52 ms |
12068 KB |
Output is correct |
26 |
Correct |
53 ms |
12332 KB |
Output is correct |
27 |
Correct |
122 ms |
22068 KB |
Output is correct |
28 |
Correct |
121 ms |
22100 KB |
Output is correct |
29 |
Correct |
0 ms |
348 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 |
1 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 |
112 ms |
22144 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
5 ms |
1116 KB |
Output is correct |
41 |
Correct |
115 ms |
22180 KB |
Output is correct |
42 |
Correct |
0 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 |
68 ms |
12136 KB |
Output is correct |
46 |
Correct |
1 ms |
348 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 |
5740 KB |
Output is correct |
50 |
Correct |
119 ms |
22096 KB |
Output is correct |
51 |
Correct |
116 ms |
22096 KB |
Output is correct |
52 |
Correct |
114 ms |
22100 KB |
Output is correct |
53 |
Correct |
55 ms |
12312 KB |
Output is correct |
54 |
Correct |
62 ms |
12084 KB |
Output is correct |
55 |
Correct |
121 ms |
22096 KB |
Output is correct |
56 |
Correct |
111 ms |
22100 KB |
Output is correct |
57 |
Correct |
58 ms |
12120 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
28 ms |
5872 KB |
Output is correct |
62 |
Correct |
114 ms |
22096 KB |
Output is correct |
63 |
Correct |
112 ms |
22100 KB |
Output is correct |
64 |
Correct |
119 ms |
22064 KB |
Output is correct |
65 |
Correct |
1 ms |
348 KB |
Output is correct |
66 |
Correct |
30 ms |
5700 KB |
Output is correct |
67 |
Correct |
109 ms |
22100 KB |
Output is correct |
68 |
Correct |
131 ms |
22064 KB |
Output is correct |
69 |
Correct |
116 ms |
22064 KB |
Output is correct |
70 |
Correct |
1 ms |
600 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
29 ms |
5896 KB |
Output is correct |
74 |
Correct |
108 ms |
22096 KB |
Output is correct |
75 |
Correct |
118 ms |
22096 KB |
Output is correct |
76 |
Correct |
115 ms |
22096 KB |
Output is correct |
77 |
Correct |
108 ms |
22068 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
3 ms |
860 KB |
Output is correct |
81 |
Correct |
59 ms |
12084 KB |
Output is correct |
82 |
Correct |
1 ms |
344 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
348 KB |
Output is correct |
85 |
Correct |
31 ms |
5808 KB |
Output is correct |
86 |
Correct |
56 ms |
12068 KB |
Output is correct |
87 |
Correct |
50 ms |
12116 KB |
Output is correct |
88 |
Correct |
61 ms |
12084 KB |
Output is correct |
89 |
Correct |
51 ms |
12304 KB |
Output is correct |
90 |
Correct |
1 ms |
348 KB |
Output is correct |
91 |
Correct |
0 ms |
348 KB |
Output is correct |
92 |
Correct |
2 ms |
860 KB |
Output is correct |
93 |
Correct |
49 ms |
12076 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 |
12 ms |
3744 KB |
Output is correct |
98 |
Correct |
50 ms |
14160 KB |
Output is correct |
99 |
Correct |
49 ms |
14160 KB |
Output is correct |
100 |
Correct |
51 ms |
14244 KB |
Output is correct |
101 |
Correct |
43 ms |
14164 KB |
Output is correct |
102 |
Correct |
52 ms |
14164 KB |
Output is correct |
103 |
Correct |
45 ms |
14164 KB |
Output is correct |
104 |
Correct |
48 ms |
14244 KB |
Output is correct |
105 |
Correct |
53 ms |
14248 KB |
Output is correct |