#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
#include "parks.h"
// All edges in column 2, face the tree on column 1
// All edges in column 4, face the tree on column 5
// All horizontal edges, face the tree just above it on column 3
using namespace std;
vector<int> x, y;
vector<int> north, south, east, west;
vector<int> visited;
bool cmpx(int i, int j) {
if (x[i] == x[j])
return (y[i] < y[j]);
return (x[i] < x[j]);
}
bool cmpy(int i, int j) {
if (y[i] == y[j])
return (x[i] < x[j]);
return (y[i] < y[j]);
}
void RunDFS(int s) {
vector<int> stack;
// cout << s << visited[s] << " north: " << north[s] << " east: " << east[s] << " south: " << south[s] << " west: " << west[s] << endl;
stack.push_back(s);
while (stack.size() > 0) {
int t = stack.back();
stack.pop_back();
if (visited[t] != 1) {
visited[t]=1;
if(north[t] != -1) stack.push_back(north[t]);
if(west[t] != -1) stack.push_back(west[t]);
if(south[t] != -1) stack.push_back(south[t]);
if(east[t] != -1) stack.push_back(east[t]);
}
}
}
// Edge set and tree set
std::vector<int> u, v, a, b;
void add_edge(int i, int j) {
u.push_back(i);
v.push_back(j);
if (((x[i]+y[i])/2) % 2 == 0) {
// insert tree on left
if (x[i] == x[j]) {
b.push_back( (y[i]+y[j])/2 );
if (y[j] > y[i]) {
a.push_back(x[i]-1);
} else {
a.push_back(x[i]+1);
}
} else {
a.push_back( (x[i]+x[j])/2 );
if (x[j] > x[i]) {
b.push_back(y[i]+1);
} else {
b.push_back(y[i]-1);
}
}
} else {
// insert tree on right
if (x[i] == x[j]) {
b.push_back( (y[i]+y[j])/2 );
if (y[j] > y[i]) {
a.push_back(x[i]+1);
} else {
a.push_back(x[i]-1);
}
} else {
a.push_back( (x[i]+x[j])/2 );
if (x[j] > x[i]) {
b.push_back(y[i]-1);
} else {
b.push_back(y[i]+1);
}
}
}
}
int construct_roads(std::vector<int> _x, std::vector<int> _y) {
x = _x;
y = _y;
std::vector<int> idx;
int n = x.size();
for(int i=0; i<n; i++) {
idx.push_back( i );
}
for (int i=0; i<n; i++) {
north.push_back(-1);
south.push_back(-1);
east.push_back(-1);
west.push_back(-1);
}
// Assign north & south
std::sort(idx.begin(), idx.end(), cmpx);
for (int i=0; i<n-1; i++) {
if (x[idx[i]]==x[idx[i+1]] && y[idx[i]]+2 == y[idx[i+1]]) {
north[idx[i]] = idx[i+1];
south[idx[i+1]] = idx[i];
add_edge(idx[i], idx[i+1]);
}
}
// Assign east & west
std::sort(idx.begin(), idx.end(), cmpy);
for (int i=0; i<n-1; i++) {
if (y[idx[i]]==y[idx[i+1]] && x[idx[i]]+2 == x[idx[i+1]]) {
east[idx[i]] = idx[i+1];
west[idx[i+1]] = idx[i];
add_edge(idx[i], idx[i+1]);
}
}
// cout << "Finish assigning directions\n";
for(int i=0; i<n; i++) {
visited.push_back(-1);
}
RunDFS(0);
bool connected = true;
for(int i=0; i<n; i++) {
if (visited[i] == -1) {
connected = false;
break;
}
}
if (connected == true) {
build(u, v, a, b);
return 1;
} else {
return 0;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
120 ms |
10300 KB |
Output is correct |
10 |
Correct |
8 ms |
1484 KB |
Output is correct |
11 |
Correct |
40 ms |
5524 KB |
Output is correct |
12 |
Correct |
11 ms |
2116 KB |
Output is correct |
13 |
Correct |
19 ms |
3664 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
102 ms |
10316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
120 ms |
10300 KB |
Output is correct |
10 |
Correct |
8 ms |
1484 KB |
Output is correct |
11 |
Correct |
40 ms |
5524 KB |
Output is correct |
12 |
Correct |
11 ms |
2116 KB |
Output is correct |
13 |
Correct |
19 ms |
3664 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
102 ms |
10316 KB |
Output is correct |
17 |
Incorrect |
1 ms |
204 KB |
Tree @(3, 3) appears more than once: for edges on positions 2 and 3 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
120 ms |
10300 KB |
Output is correct |
10 |
Correct |
8 ms |
1484 KB |
Output is correct |
11 |
Correct |
40 ms |
5524 KB |
Output is correct |
12 |
Correct |
11 ms |
2116 KB |
Output is correct |
13 |
Correct |
19 ms |
3664 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
102 ms |
10316 KB |
Output is correct |
17 |
Incorrect |
1 ms |
204 KB |
Tree @(3, 3) appears more than once: for edges on positions 2 and 3 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
120 ms |
10300 KB |
Output is correct |
10 |
Correct |
8 ms |
1484 KB |
Output is correct |
11 |
Correct |
40 ms |
5524 KB |
Output is correct |
12 |
Correct |
11 ms |
2116 KB |
Output is correct |
13 |
Correct |
19 ms |
3664 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
102 ms |
10316 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
229 ms |
21124 KB |
Output is correct |
21 |
Correct |
213 ms |
20840 KB |
Output is correct |
22 |
Correct |
260 ms |
20836 KB |
Output is correct |
23 |
Correct |
180 ms |
17844 KB |
Output is correct |
24 |
Correct |
128 ms |
11468 KB |
Output is correct |
25 |
Correct |
138 ms |
14668 KB |
Output is correct |
26 |
Correct |
139 ms |
14664 KB |
Output is correct |
27 |
Correct |
239 ms |
20164 KB |
Output is correct |
28 |
Correct |
256 ms |
20140 KB |
Output is correct |
29 |
Correct |
219 ms |
20184 KB |
Output is correct |
30 |
Correct |
216 ms |
20100 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
15 ms |
2064 KB |
Output is correct |
33 |
Correct |
52 ms |
5924 KB |
Output is correct |
34 |
Correct |
220 ms |
21104 KB |
Output is correct |
35 |
Correct |
7 ms |
1100 KB |
Output is correct |
36 |
Correct |
33 ms |
3920 KB |
Output is correct |
37 |
Correct |
70 ms |
7584 KB |
Output is correct |
38 |
Correct |
85 ms |
8936 KB |
Output is correct |
39 |
Correct |
125 ms |
11468 KB |
Output is correct |
40 |
Correct |
166 ms |
15920 KB |
Output is correct |
41 |
Correct |
193 ms |
18476 KB |
Output is correct |
42 |
Correct |
235 ms |
21136 KB |
Output is correct |
43 |
Correct |
1 ms |
204 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
1 ms |
204 KB |
Output is correct |
53 |
Correct |
1 ms |
204 KB |
Output is correct |
54 |
Correct |
2 ms |
332 KB |
Output is correct |
55 |
Correct |
3 ms |
460 KB |
Output is correct |
56 |
Correct |
105 ms |
10716 KB |
Output is correct |
57 |
Correct |
167 ms |
15712 KB |
Output is correct |
58 |
Correct |
167 ms |
15708 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
204 KB |
Output is correct |
61 |
Correct |
1 ms |
204 KB |
Output is correct |
62 |
Correct |
248 ms |
20352 KB |
Output is correct |
63 |
Correct |
250 ms |
20248 KB |
Output is correct |
64 |
Correct |
242 ms |
20100 KB |
Output is correct |
65 |
Correct |
3 ms |
460 KB |
Output is correct |
66 |
Correct |
5 ms |
844 KB |
Output is correct |
67 |
Correct |
113 ms |
10024 KB |
Output is correct |
68 |
Correct |
158 ms |
16168 KB |
Output is correct |
69 |
Correct |
222 ms |
20180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
120 ms |
10300 KB |
Output is correct |
10 |
Correct |
8 ms |
1484 KB |
Output is correct |
11 |
Correct |
40 ms |
5524 KB |
Output is correct |
12 |
Correct |
11 ms |
2116 KB |
Output is correct |
13 |
Correct |
19 ms |
3664 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
102 ms |
10316 KB |
Output is correct |
17 |
Correct |
247 ms |
20712 KB |
Output is correct |
18 |
Correct |
240 ms |
20736 KB |
Output is correct |
19 |
Correct |
213 ms |
20840 KB |
Output is correct |
20 |
Correct |
254 ms |
25252 KB |
Output is correct |
21 |
Correct |
201 ms |
19080 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
33 ms |
3648 KB |
Output is correct |
24 |
Correct |
14 ms |
1848 KB |
Output is correct |
25 |
Correct |
66 ms |
6108 KB |
Output is correct |
26 |
Correct |
86 ms |
9060 KB |
Output is correct |
27 |
Correct |
120 ms |
11576 KB |
Output is correct |
28 |
Correct |
156 ms |
14852 KB |
Output is correct |
29 |
Correct |
189 ms |
18512 KB |
Output is correct |
30 |
Correct |
214 ms |
20620 KB |
Output is correct |
31 |
Correct |
249 ms |
23176 KB |
Output is correct |
32 |
Correct |
229 ms |
21932 KB |
Output is correct |
33 |
Correct |
245 ms |
20140 KB |
Output is correct |
34 |
Correct |
3 ms |
716 KB |
Output is correct |
35 |
Correct |
5 ms |
1100 KB |
Output is correct |
36 |
Correct |
103 ms |
10748 KB |
Output is correct |
37 |
Correct |
165 ms |
16888 KB |
Output is correct |
38 |
Correct |
224 ms |
21052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
120 ms |
10300 KB |
Output is correct |
10 |
Correct |
8 ms |
1484 KB |
Output is correct |
11 |
Correct |
40 ms |
5524 KB |
Output is correct |
12 |
Correct |
11 ms |
2116 KB |
Output is correct |
13 |
Correct |
19 ms |
3664 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
102 ms |
10316 KB |
Output is correct |
17 |
Incorrect |
1 ms |
204 KB |
Tree @(3, 3) appears more than once: for edges on positions 2 and 3 |
18 |
Halted |
0 ms |
0 KB |
- |