#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())
{
ok=0;
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,ok=1;
if(road.find(make_pair(xx-1,yy))!=road.end()) aa=xx-1,bb=yy,ok=1;
if(road.find(make_pair(xx,yy+1))!=road.end()) aa=xx,bb=yy+1,ok=1;
if(road.find(make_pair(xx,yy-1))!=road.end()) aa=xx,bb=yy-1,ok=1;
con[make_pair(xx,yy)]=0;
still.erase(make_pair(xx,yy));
if(ok==0) continue;
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));
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 |
501 ms |
39968 KB |
Output is correct |
10 |
Correct |
36 ms |
4548 KB |
Output is correct |
11 |
Correct |
206 ms |
22540 KB |
Output is correct |
12 |
Correct |
49 ms |
6468 KB |
Output is correct |
13 |
Correct |
73 ms |
12612 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
495 ms |
39896 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 |
501 ms |
39968 KB |
Output is correct |
10 |
Correct |
36 ms |
4548 KB |
Output is correct |
11 |
Correct |
206 ms |
22540 KB |
Output is correct |
12 |
Correct |
49 ms |
6468 KB |
Output is correct |
13 |
Correct |
73 ms |
12612 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
495 ms |
39896 KB |
Output is correct |
17 |
Correct |
0 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 |
1580 ms |
82144 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
4 ms |
716 KB |
Output is correct |
26 |
Correct |
6 ms |
972 KB |
Output is correct |
27 |
Correct |
8 ms |
1340 KB |
Output is correct |
28 |
Correct |
507 ms |
30776 KB |
Output is correct |
29 |
Correct |
855 ms |
49280 KB |
Output is correct |
30 |
Correct |
1202 ms |
65792 KB |
Output is correct |
31 |
Correct |
1532 ms |
82064 KB |
Output is correct |
32 |
Correct |
2 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
1 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 |
972 KB |
Output is correct |
45 |
Correct |
565 ms |
34640 KB |
Output is correct |
46 |
Correct |
841 ms |
49788 KB |
Output is correct |
47 |
Correct |
865 ms |
49624 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 |
501 ms |
39968 KB |
Output is correct |
10 |
Correct |
36 ms |
4548 KB |
Output is correct |
11 |
Correct |
206 ms |
22540 KB |
Output is correct |
12 |
Correct |
49 ms |
6468 KB |
Output is correct |
13 |
Correct |
73 ms |
12612 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
495 ms |
39896 KB |
Output is correct |
17 |
Correct |
0 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 |
1580 ms |
82144 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
4 ms |
716 KB |
Output is correct |
26 |
Correct |
6 ms |
972 KB |
Output is correct |
27 |
Correct |
8 ms |
1340 KB |
Output is correct |
28 |
Correct |
507 ms |
30776 KB |
Output is correct |
29 |
Correct |
855 ms |
49280 KB |
Output is correct |
30 |
Correct |
1202 ms |
65792 KB |
Output is correct |
31 |
Correct |
1532 ms |
82064 KB |
Output is correct |
32 |
Correct |
2 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
1 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 |
972 KB |
Output is correct |
45 |
Correct |
565 ms |
34640 KB |
Output is correct |
46 |
Correct |
841 ms |
49788 KB |
Output is correct |
47 |
Correct |
865 ms |
49624 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
1 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 |
3563 ms |
72128 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 |
501 ms |
39968 KB |
Output is correct |
10 |
Correct |
36 ms |
4548 KB |
Output is correct |
11 |
Correct |
206 ms |
22540 KB |
Output is correct |
12 |
Correct |
49 ms |
6468 KB |
Output is correct |
13 |
Correct |
73 ms |
12612 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
495 ms |
39896 KB |
Output is correct |
17 |
Correct |
0 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 |
1091 ms |
57916 KB |
Output is correct |
21 |
Correct |
1233 ms |
57024 KB |
Output is correct |
22 |
Correct |
1163 ms |
57828 KB |
Output is correct |
23 |
Correct |
999 ms |
72976 KB |
Output is correct |
24 |
Correct |
299 ms |
16708 KB |
Output is correct |
25 |
Correct |
855 ms |
54204 KB |
Output is correct |
26 |
Correct |
804 ms |
54328 KB |
Output is correct |
27 |
Correct |
1249 ms |
85728 KB |
Output is correct |
28 |
Correct |
1298 ms |
85924 KB |
Output is correct |
29 |
Correct |
1385 ms |
85928 KB |
Output is correct |
30 |
Correct |
1419 ms |
85764 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Execution timed out |
3533 ms |
4716 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 |
501 ms |
39968 KB |
Output is correct |
10 |
Correct |
36 ms |
4548 KB |
Output is correct |
11 |
Correct |
206 ms |
22540 KB |
Output is correct |
12 |
Correct |
49 ms |
6468 KB |
Output is correct |
13 |
Correct |
73 ms |
12612 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
495 ms |
39896 KB |
Output is correct |
17 |
Correct |
1280 ms |
85768 KB |
Output is correct |
18 |
Execution timed out |
3548 ms |
78912 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 |
501 ms |
39968 KB |
Output is correct |
10 |
Correct |
36 ms |
4548 KB |
Output is correct |
11 |
Correct |
206 ms |
22540 KB |
Output is correct |
12 |
Correct |
49 ms |
6468 KB |
Output is correct |
13 |
Correct |
73 ms |
12612 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
716 KB |
Output is correct |
16 |
Correct |
495 ms |
39896 KB |
Output is correct |
17 |
Correct |
0 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 |
1580 ms |
82144 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
4 ms |
716 KB |
Output is correct |
26 |
Correct |
6 ms |
972 KB |
Output is correct |
27 |
Correct |
8 ms |
1340 KB |
Output is correct |
28 |
Correct |
507 ms |
30776 KB |
Output is correct |
29 |
Correct |
855 ms |
49280 KB |
Output is correct |
30 |
Correct |
1202 ms |
65792 KB |
Output is correct |
31 |
Correct |
1532 ms |
82064 KB |
Output is correct |
32 |
Correct |
2 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
1 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 |
972 KB |
Output is correct |
45 |
Correct |
565 ms |
34640 KB |
Output is correct |
46 |
Correct |
841 ms |
49788 KB |
Output is correct |
47 |
Correct |
865 ms |
49624 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
1 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 |
3563 ms |
72128 KB |
Time limit exceeded |
56 |
Halted |
0 ms |
0 KB |
- |