#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
#include "parks.h"
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]);
}
}
}
/*
void RunDFS(int s) {
if(visited[s] == 1) return;
visited[s]=1;
if(north[s] != -1) RunDFS(north[s]);
if(west[s] != -1) RunDFS(west[s]);
if(south[s] != -1) RunDFS(south[s]);
if(east[s] != -1) RunDFS(east[s]);
}
*/
// 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);
// cout << "merge " << i << "," << j << endl;
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);
}
}
}
}
/*
void build(std::vector<int> u, std::vector<int> v, std::vector<int> a, std::vector<int> b) {
return 0;
}
*/
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 );
}
/*
cout << "The vector before applying sort is:\n" ;
for (int i=0; i<n; i++) {
cout << idx[i] << ": " << x[idx[i]] << " " << y[idx[i]] << endl;
}
*/
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]]) {
add_edge(idx[i], idx[i+1]);
north[idx[i]] = idx[i+1];
south[idx[i+1]] = idx[i];
}
}
// 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]]) {
add_edge(idx[i], idx[i+1]);
east[idx[i]] = idx[i+1];
west[idx[i+1]] = idx[i];
}
}
// 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 == false) {
// cout << "It is not connected" << endl;
return 0;
}
// cout << "It is connected" << endl;
build(u, v, a, b);
return 1;
}
# |
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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
45 ms |
10424 KB |
Output is correct |
10 |
Correct |
4 ms |
1500 KB |
Output is correct |
11 |
Correct |
18 ms |
5524 KB |
Output is correct |
12 |
Correct |
10 ms |
2140 KB |
Output is correct |
13 |
Correct |
9 ms |
3668 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
44 ms |
10416 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
45 ms |
10424 KB |
Output is correct |
10 |
Correct |
4 ms |
1500 KB |
Output is correct |
11 |
Correct |
18 ms |
5524 KB |
Output is correct |
12 |
Correct |
10 ms |
2140 KB |
Output is correct |
13 |
Correct |
9 ms |
3668 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
44 ms |
10416 KB |
Output is correct |
17 |
Incorrect |
1 ms |
344 KB |
Tree @(3, 3) appears more than once: for edges on positions 2 and 3 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
45 ms |
10424 KB |
Output is correct |
10 |
Correct |
4 ms |
1500 KB |
Output is correct |
11 |
Correct |
18 ms |
5524 KB |
Output is correct |
12 |
Correct |
10 ms |
2140 KB |
Output is correct |
13 |
Correct |
9 ms |
3668 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
44 ms |
10416 KB |
Output is correct |
17 |
Incorrect |
1 ms |
344 KB |
Tree @(3, 3) appears more than once: for edges on positions 2 and 3 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
45 ms |
10424 KB |
Output is correct |
10 |
Correct |
4 ms |
1500 KB |
Output is correct |
11 |
Correct |
18 ms |
5524 KB |
Output is correct |
12 |
Correct |
10 ms |
2140 KB |
Output is correct |
13 |
Correct |
9 ms |
3668 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
44 ms |
10416 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
102 ms |
21100 KB |
Output is correct |
21 |
Correct |
108 ms |
20844 KB |
Output is correct |
22 |
Correct |
95 ms |
20984 KB |
Output is correct |
23 |
Correct |
93 ms |
18068 KB |
Output is correct |
24 |
Correct |
73 ms |
11584 KB |
Output is correct |
25 |
Correct |
78 ms |
14684 KB |
Output is correct |
26 |
Correct |
80 ms |
14788 KB |
Output is correct |
27 |
Correct |
107 ms |
20164 KB |
Output is correct |
28 |
Correct |
111 ms |
20156 KB |
Output is correct |
29 |
Correct |
113 ms |
20164 KB |
Output is correct |
30 |
Correct |
106 ms |
20216 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
8 ms |
2140 KB |
Output is correct |
33 |
Correct |
26 ms |
5968 KB |
Output is correct |
34 |
Correct |
136 ms |
21240 KB |
Output is correct |
35 |
Correct |
4 ms |
1112 KB |
Output is correct |
36 |
Correct |
18 ms |
4012 KB |
Output is correct |
37 |
Correct |
38 ms |
7832 KB |
Output is correct |
38 |
Correct |
44 ms |
9064 KB |
Output is correct |
39 |
Correct |
59 ms |
11484 KB |
Output is correct |
40 |
Correct |
82 ms |
16032 KB |
Output is correct |
41 |
Correct |
101 ms |
18492 KB |
Output is correct |
42 |
Correct |
115 ms |
21112 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
1 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
51 ms |
10784 KB |
Output is correct |
57 |
Correct |
78 ms |
15872 KB |
Output is correct |
58 |
Correct |
79 ms |
15660 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
114 ms |
20324 KB |
Output is correct |
63 |
Correct |
113 ms |
20316 KB |
Output is correct |
64 |
Correct |
109 ms |
20164 KB |
Output is correct |
65 |
Correct |
2 ms |
600 KB |
Output is correct |
66 |
Correct |
3 ms |
856 KB |
Output is correct |
67 |
Correct |
47 ms |
10108 KB |
Output is correct |
68 |
Correct |
74 ms |
16072 KB |
Output is correct |
69 |
Correct |
100 ms |
20212 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
45 ms |
10424 KB |
Output is correct |
10 |
Correct |
4 ms |
1500 KB |
Output is correct |
11 |
Correct |
18 ms |
5524 KB |
Output is correct |
12 |
Correct |
10 ms |
2140 KB |
Output is correct |
13 |
Correct |
9 ms |
3668 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
44 ms |
10416 KB |
Output is correct |
17 |
Correct |
124 ms |
20724 KB |
Output is correct |
18 |
Correct |
117 ms |
20856 KB |
Output is correct |
19 |
Correct |
115 ms |
20980 KB |
Output is correct |
20 |
Correct |
116 ms |
25284 KB |
Output is correct |
21 |
Correct |
98 ms |
19228 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
16 ms |
3788 KB |
Output is correct |
24 |
Correct |
7 ms |
1880 KB |
Output is correct |
25 |
Correct |
26 ms |
6156 KB |
Output is correct |
26 |
Correct |
46 ms |
9036 KB |
Output is correct |
27 |
Correct |
57 ms |
11648 KB |
Output is correct |
28 |
Correct |
74 ms |
14788 KB |
Output is correct |
29 |
Correct |
94 ms |
18660 KB |
Output is correct |
30 |
Correct |
117 ms |
20696 KB |
Output is correct |
31 |
Correct |
121 ms |
23324 KB |
Output is correct |
32 |
Correct |
110 ms |
21956 KB |
Output is correct |
33 |
Correct |
123 ms |
20164 KB |
Output is correct |
34 |
Correct |
1 ms |
856 KB |
Output is correct |
35 |
Correct |
3 ms |
1116 KB |
Output is correct |
36 |
Correct |
50 ms |
10740 KB |
Output is correct |
37 |
Correct |
85 ms |
16972 KB |
Output is correct |
38 |
Correct |
104 ms |
21188 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
45 ms |
10424 KB |
Output is correct |
10 |
Correct |
4 ms |
1500 KB |
Output is correct |
11 |
Correct |
18 ms |
5524 KB |
Output is correct |
12 |
Correct |
10 ms |
2140 KB |
Output is correct |
13 |
Correct |
9 ms |
3668 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
44 ms |
10416 KB |
Output is correct |
17 |
Incorrect |
1 ms |
344 KB |
Tree @(3, 3) appears more than once: for edges on positions 2 and 3 |
18 |
Halted |
0 ms |
0 KB |
- |