#include <iostream>
#include <vector>
#include "supertrees.h"
#define MAX_N 1000
using namespace std;
struct dsu {
int n;
int sef[MAX_N];
void init( int x ) {
int i;
n = x;
for ( i = 0; i < n; i++ )
sef[i] = i;
}
int find( int x ) {
if ( sef[x] != x )
sef[x] = find( sef[x] );
return sef[x];
}
void unionn( int x, int y ) {
x = find( x );
y = find( y );
sef[y] = x;
}
};
int construct( vector <vector <int>> p ) {
int n, x, y, i, j, first, last, sef1, sef2, e2;
dsu forest1, forest2;
vector <int> f1[MAX_N], f2[MAX_N];
n = p.size();
vector <int> v;
vector <vector <int>> b( n );
forest1.init( n );
forest2.init( n );
for ( x = 0; x < n; x++ ) {
for ( y = 0; y < n; y++ ) {
if ( p[x][y] >= 1 )
forest1.unionn( x, y );
if ( p[x][y] == 1 )
forest2.unionn( x, y );
}
}
for ( x = 0; x < n; x++ ) {
f1[forest1.find( x )].push_back( x );
f2[forest2.find( x )].push_back( x );
}
for ( i = 0; i < n; i++ ) {
for ( j = 0; j < n; j++ )
b[i].push_back( 0 );
}
for ( x = 0; x < n; x++ ) {
v = f1[x];
if ( v.size() > 1 ) {
e2 = 0;
for ( i = 0; i < v.size(); i++ ) {
for ( j = i + 1; j < v.size(); j++ ) {
if ( p[v[i]][v[j]] == 0 )
return 0;
if ( p[v[i]][v[j]] == 2 )
e2 = 1;
}
}
if ( e2 ) {
if ( v.size() == 2 )
return 0;
sef1 = sef2 = -1;
for ( i = 0; i < v.size(); i++ ) {
if ( forest2.find( v[i] ) != v[i] && forest2.find( v[i] ) != sef1 && forest2.find( v[i] ) != sef2 ) {
if ( sef1 == -1 )
sef1 = forest2.find( v[i] );
else if ( sef2 == -1 )
sef2 = forest2.find( v[i] );
else
return 0;
}
}
first = -1;
last = -1;
for ( i = 0; i < v.size(); i++ ) {
if ( forest2.find( v[i] ) != v[i] ) {
b[v[i]][forest2.find( v[i] )] = 1;
b[forest2.find( v[i] )][v[i]] = 1;
} else {
if ( last != -1 ) {
b[last][v[i]] = 1;
b[v[i]][last] = 1;
}
if ( first == -1 )
first = v[i];
last = v[i];
}
}
if ( first != last ) {
b[first][last] = 1;
b[last][first] = 1;
}
for ( i = 0; i < v.size(); i++ ) {
for ( j = i + 1; j < v.size(); j++ ) {
if ( forest2.find( v[i] ) == forest2.find( v[j] ) ) {
if ( p[v[i]][v[j]] != 1 )
return 0;
} else {
if ( p[v[i]][v[j]] != 2 )
return 0;
}
}
}
} else {
for ( i = 0; i < v.size() - 1; i++ ) {
b[v[i]][v[i + 1]] = 1;
b[v[i + 1]][v[i]] = 1;
}
}
}
}
build( b );
return 1;
}
Compilation message
supertrees.cpp: In function 'int construct(std::vector<std::vector<int> >)':
supertrees.cpp:69:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for ( i = 0; i < v.size(); i++ ) {
| ~~^~~~~~~~~~
supertrees.cpp:70:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for ( j = i + 1; j < v.size(); j++ ) {
| ~~^~~~~~~~~~
supertrees.cpp:81:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for ( i = 0; i < v.size(); i++ ) {
| ~~^~~~~~~~~~
supertrees.cpp:93:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for ( i = 0; i < v.size(); i++ ) {
| ~~^~~~~~~~~~
supertrees.cpp:111:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for ( i = 0; i < v.size(); i++ ) {
| ~~^~~~~~~~~~
supertrees.cpp:112:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for ( j = i + 1; j < v.size(); j++ ) {
| ~~^~~~~~~~~~
supertrees.cpp:123:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for ( i = 0; i < v.size() - 1; i++ ) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
9 ms |
1232 KB |
Output is correct |
7 |
Correct |
187 ms |
22148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
9 ms |
1232 KB |
Output is correct |
7 |
Correct |
187 ms |
22148 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
8 ms |
1244 KB |
Output is correct |
13 |
Correct |
175 ms |
22224 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
116 ms |
12288 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
45 ms |
5736 KB |
Output is correct |
21 |
Correct |
191 ms |
22100 KB |
Output is correct |
22 |
Correct |
165 ms |
22268 KB |
Output is correct |
23 |
Correct |
197 ms |
22140 KB |
Output is correct |
24 |
Correct |
174 ms |
22308 KB |
Output is correct |
25 |
Correct |
120 ms |
12284 KB |
Output is correct |
26 |
Correct |
74 ms |
12284 KB |
Output is correct |
27 |
Correct |
186 ms |
22152 KB |
Output is correct |
28 |
Correct |
181 ms |
22140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
8 ms |
1228 KB |
Output is correct |
9 |
Correct |
181 ms |
22212 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
8 ms |
1228 KB |
Output is correct |
13 |
Correct |
188 ms |
22136 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
844 KB |
Output is correct |
17 |
Correct |
88 ms |
12296 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
64 ms |
5760 KB |
Output is correct |
22 |
Correct |
172 ms |
22340 KB |
Output is correct |
23 |
Correct |
190 ms |
22156 KB |
Output is correct |
24 |
Correct |
186 ms |
22244 KB |
Output is correct |
25 |
Correct |
111 ms |
12292 KB |
Output is correct |
26 |
Correct |
74 ms |
12300 KB |
Output is correct |
27 |
Correct |
170 ms |
22408 KB |
Output is correct |
28 |
Correct |
197 ms |
22156 KB |
Output is correct |
29 |
Correct |
71 ms |
12288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
54 ms |
5724 KB |
Output is correct |
5 |
Correct |
181 ms |
22152 KB |
Output is correct |
6 |
Correct |
167 ms |
22168 KB |
Output is correct |
7 |
Correct |
240 ms |
22140 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
41 ms |
5828 KB |
Output is correct |
10 |
Correct |
181 ms |
22232 KB |
Output is correct |
11 |
Correct |
162 ms |
22152 KB |
Output is correct |
12 |
Correct |
188 ms |
22144 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Incorrect |
18 ms |
3332 KB |
Answer gives possible 0 while actual possible 1 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
9 ms |
1232 KB |
Output is correct |
7 |
Correct |
187 ms |
22148 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
8 ms |
1244 KB |
Output is correct |
13 |
Correct |
175 ms |
22224 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
116 ms |
12288 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
45 ms |
5736 KB |
Output is correct |
21 |
Correct |
191 ms |
22100 KB |
Output is correct |
22 |
Correct |
165 ms |
22268 KB |
Output is correct |
23 |
Correct |
197 ms |
22140 KB |
Output is correct |
24 |
Correct |
174 ms |
22308 KB |
Output is correct |
25 |
Correct |
120 ms |
12284 KB |
Output is correct |
26 |
Correct |
74 ms |
12284 KB |
Output is correct |
27 |
Correct |
186 ms |
22152 KB |
Output is correct |
28 |
Correct |
181 ms |
22140 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
8 ms |
1228 KB |
Output is correct |
37 |
Correct |
181 ms |
22212 KB |
Output is correct |
38 |
Correct |
0 ms |
332 KB |
Output is correct |
39 |
Correct |
0 ms |
332 KB |
Output is correct |
40 |
Correct |
8 ms |
1228 KB |
Output is correct |
41 |
Correct |
188 ms |
22136 KB |
Output is correct |
42 |
Correct |
0 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
4 ms |
844 KB |
Output is correct |
45 |
Correct |
88 ms |
12296 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
64 ms |
5760 KB |
Output is correct |
50 |
Correct |
172 ms |
22340 KB |
Output is correct |
51 |
Correct |
190 ms |
22156 KB |
Output is correct |
52 |
Correct |
186 ms |
22244 KB |
Output is correct |
53 |
Correct |
111 ms |
12292 KB |
Output is correct |
54 |
Correct |
74 ms |
12300 KB |
Output is correct |
55 |
Correct |
170 ms |
22408 KB |
Output is correct |
56 |
Correct |
197 ms |
22156 KB |
Output is correct |
57 |
Correct |
71 ms |
12288 KB |
Output is correct |
58 |
Correct |
0 ms |
332 KB |
Output is correct |
59 |
Correct |
0 ms |
332 KB |
Output is correct |
60 |
Correct |
4 ms |
844 KB |
Output is correct |
61 |
Correct |
93 ms |
12288 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Incorrect |
18 ms |
3352 KB |
Answer gives possible 0 while actual possible 1 |
66 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
9 ms |
1232 KB |
Output is correct |
7 |
Correct |
187 ms |
22148 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
8 ms |
1244 KB |
Output is correct |
13 |
Correct |
175 ms |
22224 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
116 ms |
12288 KB |
Output is correct |
18 |
Correct |
0 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
45 ms |
5736 KB |
Output is correct |
21 |
Correct |
191 ms |
22100 KB |
Output is correct |
22 |
Correct |
165 ms |
22268 KB |
Output is correct |
23 |
Correct |
197 ms |
22140 KB |
Output is correct |
24 |
Correct |
174 ms |
22308 KB |
Output is correct |
25 |
Correct |
120 ms |
12284 KB |
Output is correct |
26 |
Correct |
74 ms |
12284 KB |
Output is correct |
27 |
Correct |
186 ms |
22152 KB |
Output is correct |
28 |
Correct |
181 ms |
22140 KB |
Output is correct |
29 |
Correct |
0 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
0 ms |
332 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
8 ms |
1228 KB |
Output is correct |
37 |
Correct |
181 ms |
22212 KB |
Output is correct |
38 |
Correct |
0 ms |
332 KB |
Output is correct |
39 |
Correct |
0 ms |
332 KB |
Output is correct |
40 |
Correct |
8 ms |
1228 KB |
Output is correct |
41 |
Correct |
188 ms |
22136 KB |
Output is correct |
42 |
Correct |
0 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
4 ms |
844 KB |
Output is correct |
45 |
Correct |
88 ms |
12296 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
64 ms |
5760 KB |
Output is correct |
50 |
Correct |
172 ms |
22340 KB |
Output is correct |
51 |
Correct |
190 ms |
22156 KB |
Output is correct |
52 |
Correct |
186 ms |
22244 KB |
Output is correct |
53 |
Correct |
111 ms |
12292 KB |
Output is correct |
54 |
Correct |
74 ms |
12300 KB |
Output is correct |
55 |
Correct |
170 ms |
22408 KB |
Output is correct |
56 |
Correct |
197 ms |
22156 KB |
Output is correct |
57 |
Correct |
71 ms |
12288 KB |
Output is correct |
58 |
Correct |
0 ms |
332 KB |
Output is correct |
59 |
Correct |
0 ms |
332 KB |
Output is correct |
60 |
Correct |
0 ms |
332 KB |
Output is correct |
61 |
Correct |
54 ms |
5724 KB |
Output is correct |
62 |
Correct |
181 ms |
22152 KB |
Output is correct |
63 |
Correct |
167 ms |
22168 KB |
Output is correct |
64 |
Correct |
240 ms |
22140 KB |
Output is correct |
65 |
Correct |
1 ms |
332 KB |
Output is correct |
66 |
Correct |
41 ms |
5828 KB |
Output is correct |
67 |
Correct |
181 ms |
22232 KB |
Output is correct |
68 |
Correct |
162 ms |
22152 KB |
Output is correct |
69 |
Correct |
188 ms |
22144 KB |
Output is correct |
70 |
Correct |
1 ms |
332 KB |
Output is correct |
71 |
Correct |
1 ms |
332 KB |
Output is correct |
72 |
Correct |
1 ms |
332 KB |
Output is correct |
73 |
Incorrect |
18 ms |
3332 KB |
Answer gives possible 0 while actual possible 1 |
74 |
Halted |
0 ms |
0 KB |
- |