#include "parks.h"
#include <map>
#include <utility>
#include <queue>
#include <set>
using namespace std;
map < pair < int , int > , int > all,con;
map < pair < int , int > , pair < int , int > > road;
vector < int > u,v,a,b;
queue < pair < int , int > > BFS;
set < pair < int , int > > still;
int Father[200005];
int Find(int here)
{
if(Father[here]==here) return here;
Father[here]=Find(Father[here]);
return Father[here];
}
int construct_roads(vector < int > x, vector < int > y)
{
int N=x.size(),i,t,aa,bb,ok=1,xx,yy;
for(i=0;i<N;i++)
{
all[make_pair(x[i],y[i])]=i;
Father[i]=i;
}
for(i=0;i<N;i++)
{
if(all.find(make_pair(x[i],y[i]+2))!=all.end())
{
t=all[make_pair(x[i],y[i]+2)];
road[make_pair(x[i],y[i]+1)]=make_pair(i,t);
con[make_pair(x[i]+1,y[i]+1)]++;
con[make_pair(x[i]-1,y[i]+1)]++;
aa=Find(i);
bb=Find(t);
if(aa!=bb) Father[aa]=bb;
}
if(all.find(make_pair(x[i]+2,y[i]))!=all.end())
{
t=all[make_pair(x[i]+2,y[i])];
road[make_pair(x[i]+1,y[i])]=make_pair(i,t);
con[make_pair(x[i]+1,y[i]+1)]++;
con[make_pair(x[i]+1,y[i]-1)]++;
aa=Find(i);
bb=Find(t);
if(aa!=bb) Father[aa]=bb;
}
}
t=Find(0);
for(i=0;i<N;i++) if(t!=Find(i)) ok=0;
if(ok==0) return ok;
for(auto i:con)
{
still.insert(i.first);
if(i.second==1) BFS.push(i.first);
}
while(!BFS.empty())
{
xx=BFS.front().first;
yy=BFS.front().second;
BFS.pop();
if(con[make_pair(xx,yy)]<=0) continue;
if(road.find(make_pair(xx+1,yy))!=road.end()) aa=xx+1,bb=yy;
if(road.find(make_pair(xx-1,yy))!=road.end()) aa=xx-1,bb=yy;
if(road.find(make_pair(xx,yy+1))!=road.end()) aa=xx,bb=yy+1;
if(road.find(make_pair(xx,yy-1))!=road.end()) aa=xx,bb=yy-1;
u.push_back(road[make_pair(aa,bb)].first);
v.push_back(road[make_pair(aa,bb)].second);
a.push_back(xx);
b.push_back(yy);
road.erase(make_pair(aa,bb));
con[make_pair(xx,yy)]=0;
still.erase(make_pair(xx,yy));
if(aa%2==0)
{
con[make_pair(aa-1,bb)]--;
con[make_pair(aa+1,bb)]--;
if(con[make_pair(aa-1,bb)]==1) BFS.push(make_pair(aa-1,bb));
if(con[make_pair(aa+1,bb)]==1) BFS.push(make_pair(aa+1,bb));
}
else
{
con[make_pair(aa,bb-1)]--;
con[make_pair(aa,bb+1)]--;
if(con[make_pair(aa,bb-1)]==1) BFS.push(make_pair(aa,bb-1));
if(con[make_pair(aa,bb+1)]==1) BFS.push(make_pair(aa,bb+1));
}
}
while(!road.empty())
{
BFS.push(*still.begin());
while(!BFS.empty())
{
xx=BFS.front().first;
yy=BFS.front().second;
BFS.pop();
if(con[make_pair(xx,yy)]<=0) continue;
if(road.find(make_pair(xx+1,yy))!=road.end()) aa=xx+1,bb=yy;
if(road.find(make_pair(xx-1,yy))!=road.end()) aa=xx-1,bb=yy;
if(road.find(make_pair(xx,yy+1))!=road.end()) aa=xx,bb=yy+1;
if(road.find(make_pair(xx,yy-1))!=road.end()) aa=xx,bb=yy-1;
u.push_back(road[make_pair(aa,bb)].first);
v.push_back(road[make_pair(aa,bb)].second);
a.push_back(xx);
b.push_back(yy);
road.erase(make_pair(aa,bb));
con[make_pair(xx,yy)]=0;
still.erase(make_pair(xx,yy));
if(aa%2==0)
{
con[make_pair(aa-1,bb)]--;
con[make_pair(aa+1,bb)]--;
if(con[make_pair(aa-1,bb)]==1) BFS.push(make_pair(aa-1,bb));
if(con[make_pair(aa+1,bb)]==1) BFS.push(make_pair(aa+1,bb));
}
else
{
con[make_pair(aa,bb-1)]--;
con[make_pair(aa,bb+1)]--;
if(con[make_pair(aa,bb-1)]==1) BFS.push(make_pair(aa,bb-1));
if(con[make_pair(aa,bb+1)]==1) BFS.push(make_pair(aa,bb+1));
}
}
}
if(ok) build(u,v,a,b);
return ok;
}
Compilation message
parks.cpp: In function 'int construct_roads(std::vector<int>, std::vector<int>)':
parks.cpp:88:32: warning: 'bb' may be used uninitialized in this function [-Wmaybe-uninitialized]
88 | con[make_pair(aa,bb+1)]--;
| ~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
492 ms |
39940 KB |
Output is correct |
10 |
Correct |
32 ms |
4428 KB |
Output is correct |
11 |
Correct |
208 ms |
22556 KB |
Output is correct |
12 |
Correct |
51 ms |
6428 KB |
Output is correct |
13 |
Correct |
77 ms |
12564 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
504 ms |
39860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
492 ms |
39940 KB |
Output is correct |
10 |
Correct |
32 ms |
4428 KB |
Output is correct |
11 |
Correct |
208 ms |
22556 KB |
Output is correct |
12 |
Correct |
51 ms |
6428 KB |
Output is correct |
13 |
Correct |
77 ms |
12564 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
504 ms |
39860 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1545 ms |
81856 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
5 ms |
716 KB |
Output is correct |
26 |
Correct |
6 ms |
972 KB |
Output is correct |
27 |
Correct |
8 ms |
1356 KB |
Output is correct |
28 |
Correct |
505 ms |
30872 KB |
Output is correct |
29 |
Correct |
829 ms |
49228 KB |
Output is correct |
30 |
Correct |
1188 ms |
65680 KB |
Output is correct |
31 |
Correct |
1531 ms |
81948 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
3 ms |
716 KB |
Output is correct |
44 |
Correct |
5 ms |
844 KB |
Output is correct |
45 |
Correct |
528 ms |
34640 KB |
Output is correct |
46 |
Correct |
851 ms |
49720 KB |
Output is correct |
47 |
Correct |
844 ms |
49740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
492 ms |
39940 KB |
Output is correct |
10 |
Correct |
32 ms |
4428 KB |
Output is correct |
11 |
Correct |
208 ms |
22556 KB |
Output is correct |
12 |
Correct |
51 ms |
6428 KB |
Output is correct |
13 |
Correct |
77 ms |
12564 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
504 ms |
39860 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1545 ms |
81856 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
5 ms |
716 KB |
Output is correct |
26 |
Correct |
6 ms |
972 KB |
Output is correct |
27 |
Correct |
8 ms |
1356 KB |
Output is correct |
28 |
Correct |
505 ms |
30872 KB |
Output is correct |
29 |
Correct |
829 ms |
49228 KB |
Output is correct |
30 |
Correct |
1188 ms |
65680 KB |
Output is correct |
31 |
Correct |
1531 ms |
81948 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
3 ms |
716 KB |
Output is correct |
44 |
Correct |
5 ms |
844 KB |
Output is correct |
45 |
Correct |
528 ms |
34640 KB |
Output is correct |
46 |
Correct |
851 ms |
49720 KB |
Output is correct |
47 |
Correct |
844 ms |
49740 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
0 ms |
204 KB |
Output is correct |
50 |
Correct |
0 ms |
204 KB |
Output is correct |
51 |
Correct |
0 ms |
204 KB |
Output is correct |
52 |
Correct |
0 ms |
204 KB |
Output is correct |
53 |
Correct |
0 ms |
204 KB |
Output is correct |
54 |
Correct |
0 ms |
204 KB |
Output is correct |
55 |
Execution timed out |
3574 ms |
72572 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
492 ms |
39940 KB |
Output is correct |
10 |
Correct |
32 ms |
4428 KB |
Output is correct |
11 |
Correct |
208 ms |
22556 KB |
Output is correct |
12 |
Correct |
51 ms |
6428 KB |
Output is correct |
13 |
Correct |
77 ms |
12564 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
504 ms |
39860 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1072 ms |
57852 KB |
Output is correct |
21 |
Correct |
1268 ms |
56856 KB |
Output is correct |
22 |
Correct |
1264 ms |
57664 KB |
Output is correct |
23 |
Correct |
1069 ms |
72988 KB |
Output is correct |
24 |
Correct |
325 ms |
16700 KB |
Output is correct |
25 |
Correct |
945 ms |
54072 KB |
Output is correct |
26 |
Correct |
902 ms |
54124 KB |
Output is correct |
27 |
Correct |
1297 ms |
85692 KB |
Output is correct |
28 |
Correct |
1304 ms |
85804 KB |
Output is correct |
29 |
Correct |
1341 ms |
85876 KB |
Output is correct |
30 |
Correct |
1273 ms |
85932 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Execution timed out |
3579 ms |
4712 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
492 ms |
39940 KB |
Output is correct |
10 |
Correct |
32 ms |
4428 KB |
Output is correct |
11 |
Correct |
208 ms |
22556 KB |
Output is correct |
12 |
Correct |
51 ms |
6428 KB |
Output is correct |
13 |
Correct |
77 ms |
12564 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
504 ms |
39860 KB |
Output is correct |
17 |
Correct |
1184 ms |
85792 KB |
Output is correct |
18 |
Execution timed out |
3580 ms |
79036 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
492 ms |
39940 KB |
Output is correct |
10 |
Correct |
32 ms |
4428 KB |
Output is correct |
11 |
Correct |
208 ms |
22556 KB |
Output is correct |
12 |
Correct |
51 ms |
6428 KB |
Output is correct |
13 |
Correct |
77 ms |
12564 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
504 ms |
39860 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1545 ms |
81856 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
5 ms |
716 KB |
Output is correct |
26 |
Correct |
6 ms |
972 KB |
Output is correct |
27 |
Correct |
8 ms |
1356 KB |
Output is correct |
28 |
Correct |
505 ms |
30872 KB |
Output is correct |
29 |
Correct |
829 ms |
49228 KB |
Output is correct |
30 |
Correct |
1188 ms |
65680 KB |
Output is correct |
31 |
Correct |
1531 ms |
81948 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
3 ms |
716 KB |
Output is correct |
44 |
Correct |
5 ms |
844 KB |
Output is correct |
45 |
Correct |
528 ms |
34640 KB |
Output is correct |
46 |
Correct |
851 ms |
49720 KB |
Output is correct |
47 |
Correct |
844 ms |
49740 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
0 ms |
204 KB |
Output is correct |
50 |
Correct |
0 ms |
204 KB |
Output is correct |
51 |
Correct |
0 ms |
204 KB |
Output is correct |
52 |
Correct |
0 ms |
204 KB |
Output is correct |
53 |
Correct |
0 ms |
204 KB |
Output is correct |
54 |
Correct |
0 ms |
204 KB |
Output is correct |
55 |
Execution timed out |
3574 ms |
72572 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |