#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
int n;
vector<int> x,y;
map<ii,int> M;
bool isVertical(ii u) {
return x[u.first]==x[u.second];
}
bool cmp(ii u,ii v) {
if(x[u.first]==x[v.first]) {
if(isVertical(u)==isVertical(v))
return y[u.first]<y[v.first];
return isVertical(u)>isVertical(v);
}
return x[u.first]<x[v.first];
}
vector<ii> constructBenches(vector<ii> &paths) {
map<ii,int> deg;
map<ii,bool> mark;
vector<ii> benchesPos;
for(int i=0;i<paths.size();i++) {
ii path=paths[i];
if(x[path.first]==x[path.second]) {
int bx=x[path.first],by=y[path.first]+1;
deg[ii(bx-1,by)]++;
deg[ii(bx+1,by)]++;
benchesPos.push_back({bx-1,by});
benchesPos.push_back({bx+1,by});
} else {
int bx=x[path.first]+1,by=y[path.first];
deg[ii(bx,by-1)]++;
deg[ii(bx,by+1)]++;
benchesPos.push_back({bx,by-1});
benchesPos.push_back({bx,by+1});
}
}
vector<ii> benches;
for(int i=0;i<paths.size();i++) {
if(deg[benchesPos[2*i+1]]==1||mark[benchesPos[2*i]]) {
mark[benchesPos[2*i+1]]=true;
deg[benchesPos[2*i+1]]--;
benches.push_back(benchesPos[2*i+1]);
} else {
mark[benchesPos[2*i]]=true;
deg[benchesPos[2*i]]--;
benches.push_back(benchesPos[2*i]);
}
}
return benches;
}
int construct_roads(std::vector<int> _x, std::vector<int> _y) {
n=_x.size();
x=_x,y=_y;
for(int i=0;i<n;i++)
M[ii(x[i],y[i])]=i;
vector<ii> paths;
for(int i=0;i<n;i++) {
auto it=M.find({x[i]+2,y[i]});
if(it!=M.end())
paths.push_back({i,it->second});
it=M.find({x[i],y[i]+2});
if(it!=M.end())
paths.push_back({i,it->second});
}
if(paths.size()<n-1)
return 0;
sort(paths.begin(),paths.end(),cmp);
//for(ii path:paths)
// cerr<<x[path.first]<<' '<<y[path.first]<<' '<<x[path.second]<<' '<<y[path.second]<<'\n';
vector<ii> benches=constructBenches(paths);
vector<int> U(n-1),V(n-1),A(n-1),B(n-1);
for(int i=0;i<n-1;i++) {
U[i]=paths[i].first,V[i]=paths[i].second;
A[i]=benches[i].first,B[i]=benches[i].second;
}
build(U,V,A,B);
return 1;
}
Compilation message
parks.cpp: In function 'std::vector<std::pair<int, int> > constructBenches(std::vector<std::pair<int, int> >&)':
parks.cpp:28:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i=0;i<paths.size();i++) {
| ~^~~~~~~~~~~~~
parks.cpp:45:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=0;i<paths.size();i++) {
| ~^~~~~~~~~~~~~
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:73:17: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
73 | if(paths.size()<n-1)
| ~~~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
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 |
392 ms |
30824 KB |
Output is correct |
10 |
Correct |
25 ms |
3280 KB |
Output is correct |
11 |
Correct |
149 ms |
16720 KB |
Output is correct |
12 |
Correct |
34 ms |
4800 KB |
Output is correct |
13 |
Correct |
30 ms |
4736 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
344 ms |
30748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
392 ms |
30824 KB |
Output is correct |
10 |
Correct |
25 ms |
3280 KB |
Output is correct |
11 |
Correct |
149 ms |
16720 KB |
Output is correct |
12 |
Correct |
34 ms |
4800 KB |
Output is correct |
13 |
Correct |
30 ms |
4736 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
344 ms |
30748 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 |
1 ms |
204 KB |
Output is correct |
21 |
Incorrect |
1 ms |
204 KB |
Given structure is not connected: There is no path between vertices 0 and 1 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
392 ms |
30824 KB |
Output is correct |
10 |
Correct |
25 ms |
3280 KB |
Output is correct |
11 |
Correct |
149 ms |
16720 KB |
Output is correct |
12 |
Correct |
34 ms |
4800 KB |
Output is correct |
13 |
Correct |
30 ms |
4736 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
344 ms |
30748 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 |
1 ms |
204 KB |
Output is correct |
21 |
Incorrect |
1 ms |
204 KB |
Given structure is not connected: There is no path between vertices 0 and 1 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
392 ms |
30824 KB |
Output is correct |
10 |
Correct |
25 ms |
3280 KB |
Output is correct |
11 |
Correct |
149 ms |
16720 KB |
Output is correct |
12 |
Correct |
34 ms |
4800 KB |
Output is correct |
13 |
Correct |
30 ms |
4736 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
344 ms |
30748 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 |
Incorrect |
774 ms |
45768 KB |
Tree @(7, 199997) appears more than once: for edges on positions 3 and 4 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
392 ms |
30824 KB |
Output is correct |
10 |
Correct |
25 ms |
3280 KB |
Output is correct |
11 |
Correct |
149 ms |
16720 KB |
Output is correct |
12 |
Correct |
34 ms |
4800 KB |
Output is correct |
13 |
Correct |
30 ms |
4736 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
344 ms |
30748 KB |
Output is correct |
17 |
Correct |
793 ms |
61408 KB |
Output is correct |
18 |
Incorrect |
678 ms |
61316 KB |
Given structure is not connected: There is no path between vertices 0 and 105803 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
392 ms |
30824 KB |
Output is correct |
10 |
Correct |
25 ms |
3280 KB |
Output is correct |
11 |
Correct |
149 ms |
16720 KB |
Output is correct |
12 |
Correct |
34 ms |
4800 KB |
Output is correct |
13 |
Correct |
30 ms |
4736 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
344 ms |
30748 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 |
1 ms |
204 KB |
Output is correct |
21 |
Incorrect |
1 ms |
204 KB |
Given structure is not connected: There is no path between vertices 0 and 1 |
22 |
Halted |
0 ms |
0 KB |
- |