#include "parks.h"
#include <bits/stdc++.h>
//#include "grader.cpp"
using namespace std;
#define pb push_back
#define all(x) x.begin(), x.end()
#define fr first
#define sc second
const int N=2e5+5;
vector<array<int,3>> g[N];
int p[N];
vector<int> sz(N,1);
int f(int x){
if(p[x]==x) return x;
return p[x]=f(p[x]);
}
bool add(int a, int b){
a=f(a);
b=f(b);
if(a==b) return 0;
if(rand()%2) swap(a,b);//<-может лагать
p[a]=b;
sz[b]+=sz[a];
return 1;
}
void add_edge(int a, int b, int u, int v){
g[a].pb({b,u,v});
g[b].pb({a,u,v});
}
int construct_roads(vector<int> x, vector<int> y) {
int n=x.size();
map<pair<int,int>,int> X,Y;
for(int i=0;i<n;i++){
p[i]=i;
X[{x[i],y[i]}]=i;
}
vector<int> u,v,a,b;
for(int i=0;i<n;i++){
if(X.count({x[i]-2,y[i]})){
int j=X[{x[i]-2,y[i]}];
if(add(i,j)){
u.pb(i);v.pb(j);
a.pb(-1);b.pb(-1);
int A=u.size()-1;
if(Y.count({x[i]-1,y[i]-1})){
int B=Y[{x[i]-1,y[i]-1}];
add_edge(A,B,x[i]-1,y[i]-1);
Y.erase({x[i]-1,y[i]-1});
}
else{
Y[{x[i]-1,y[i]-1}]=A;
}
if(Y.count({x[i]-1,y[i]+1})){
int B=Y[{x[i]-1,y[i]+1}];
add_edge(A,B,x[i]-1,y[i]+1);
Y.erase({x[i]-1,y[i]+1});
}
else{
Y[{x[i]-1,y[i]+1}]=A;
}
}
}
if(X.count({x[i],y[i]-2})){
int j=X[{x[i],y[i]-2}];
if(add(i,j)){
u.pb(i);v.pb(j);
a.pb(-1);b.pb(-1);
int A=u.size()-1;
if(Y.count({x[i]-1,y[i]-1})){
int B=Y[{x[i]-1,y[i]-1}];
add_edge(A,B,x[i]-1,y[i]-1);
Y.erase({x[i]-1,y[i]-1});
}
else{
Y[{x[i]-1,y[i]-1}]=A;
}
if(Y.count({x[i]+1,y[i]-1})){
int B=Y[{x[i]+1,y[i]-1}];
add_edge(A,B,x[i]+1,y[i]-1);
Y.erase({x[i]+1,y[i]-1});
}
else{
Y[{x[i]+1,y[i]-1}]=A;
}
}
}
}
if(sz[f(0)]<n) return 0;
int m=u.size();
vector<int> used(m);
function<void(int)> dfs=[&](int x){
used[x]=1;
for(auto [it,A,B]:g[x]){
if(used[it]) continue;
a[it]=A;
b[it]=B;
dfs(it);
}
};
for(auto [it,A]: Y){
if(used[A]) continue;
a[A]=it.fr;
b[A]=it.sc;
dfs(A);
}
for(int i=0;i<m;i++){
if(used[i]) continue;
a[i]=g[i].back()[1];
b[i]=g[i].back()[2];
g[i].pop_back();
dfs(i);
}
build(u,v,a,b);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
2 ms |
5716 KB |
Output is correct |
3 |
Correct |
2 ms |
5716 KB |
Output is correct |
4 |
Correct |
2 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
271 ms |
31556 KB |
Output is correct |
10 |
Correct |
13 ms |
8532 KB |
Output is correct |
11 |
Correct |
79 ms |
20064 KB |
Output is correct |
12 |
Correct |
20 ms |
9996 KB |
Output is correct |
13 |
Correct |
54 ms |
15816 KB |
Output is correct |
14 |
Correct |
6 ms |
5932 KB |
Output is correct |
15 |
Correct |
5 ms |
6228 KB |
Output is correct |
16 |
Correct |
267 ms |
31716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
2 ms |
5716 KB |
Output is correct |
3 |
Correct |
2 ms |
5716 KB |
Output is correct |
4 |
Correct |
2 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
271 ms |
31556 KB |
Output is correct |
10 |
Correct |
13 ms |
8532 KB |
Output is correct |
11 |
Correct |
79 ms |
20064 KB |
Output is correct |
12 |
Correct |
20 ms |
9996 KB |
Output is correct |
13 |
Correct |
54 ms |
15816 KB |
Output is correct |
14 |
Correct |
6 ms |
5932 KB |
Output is correct |
15 |
Correct |
5 ms |
6228 KB |
Output is correct |
16 |
Correct |
267 ms |
31716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Incorrect |
2 ms |
5716 KB |
Tree @(3, 5) appears more than once: for edges on positions 0 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
2 ms |
5716 KB |
Output is correct |
3 |
Correct |
2 ms |
5716 KB |
Output is correct |
4 |
Correct |
2 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
271 ms |
31556 KB |
Output is correct |
10 |
Correct |
13 ms |
8532 KB |
Output is correct |
11 |
Correct |
79 ms |
20064 KB |
Output is correct |
12 |
Correct |
20 ms |
9996 KB |
Output is correct |
13 |
Correct |
54 ms |
15816 KB |
Output is correct |
14 |
Correct |
6 ms |
5932 KB |
Output is correct |
15 |
Correct |
5 ms |
6228 KB |
Output is correct |
16 |
Correct |
267 ms |
31716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Incorrect |
2 ms |
5716 KB |
Tree @(3, 5) appears more than once: for edges on positions 0 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
2 ms |
5716 KB |
Output is correct |
3 |
Correct |
2 ms |
5716 KB |
Output is correct |
4 |
Correct |
2 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
271 ms |
31556 KB |
Output is correct |
10 |
Correct |
13 ms |
8532 KB |
Output is correct |
11 |
Correct |
79 ms |
20064 KB |
Output is correct |
12 |
Correct |
20 ms |
9996 KB |
Output is correct |
13 |
Correct |
54 ms |
15816 KB |
Output is correct |
14 |
Correct |
6 ms |
5932 KB |
Output is correct |
15 |
Correct |
5 ms |
6228 KB |
Output is correct |
16 |
Correct |
267 ms |
31716 KB |
Output is correct |
17 |
Correct |
4 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5792 KB |
Output is correct |
19 |
Correct |
2 ms |
5716 KB |
Output is correct |
20 |
Correct |
569 ms |
52104 KB |
Output is correct |
21 |
Correct |
676 ms |
48080 KB |
Output is correct |
22 |
Correct |
582 ms |
48120 KB |
Output is correct |
23 |
Correct |
510 ms |
51488 KB |
Output is correct |
24 |
Correct |
200 ms |
23876 KB |
Output is correct |
25 |
Correct |
683 ms |
51852 KB |
Output is correct |
26 |
Correct |
620 ms |
51916 KB |
Output is correct |
27 |
Correct |
732 ms |
58976 KB |
Output is correct |
28 |
Correct |
702 ms |
58912 KB |
Output is correct |
29 |
Correct |
741 ms |
59024 KB |
Output is correct |
30 |
Correct |
738 ms |
59008 KB |
Output is correct |
31 |
Correct |
3 ms |
5716 KB |
Output is correct |
32 |
Correct |
30 ms |
9036 KB |
Output is correct |
33 |
Correct |
82 ms |
15148 KB |
Output is correct |
34 |
Correct |
680 ms |
56816 KB |
Output is correct |
35 |
Correct |
17 ms |
7508 KB |
Output is correct |
36 |
Correct |
95 ms |
13956 KB |
Output is correct |
37 |
Correct |
225 ms |
22392 KB |
Output is correct |
38 |
Correct |
211 ms |
21848 KB |
Output is correct |
39 |
Correct |
315 ms |
27796 KB |
Output is correct |
40 |
Correct |
452 ms |
33836 KB |
Output is correct |
41 |
Correct |
573 ms |
39992 KB |
Output is correct |
42 |
Correct |
744 ms |
46044 KB |
Output is correct |
43 |
Correct |
3 ms |
5716 KB |
Output is correct |
44 |
Correct |
3 ms |
5808 KB |
Output is correct |
45 |
Correct |
3 ms |
5808 KB |
Output is correct |
46 |
Correct |
3 ms |
5716 KB |
Output is correct |
47 |
Correct |
3 ms |
5748 KB |
Output is correct |
48 |
Correct |
2 ms |
5716 KB |
Output is correct |
49 |
Correct |
3 ms |
5716 KB |
Output is correct |
50 |
Correct |
3 ms |
5716 KB |
Output is correct |
51 |
Correct |
3 ms |
5804 KB |
Output is correct |
52 |
Correct |
2 ms |
5716 KB |
Output is correct |
53 |
Correct |
4 ms |
5716 KB |
Output is correct |
54 |
Correct |
5 ms |
6164 KB |
Output is correct |
55 |
Correct |
6 ms |
6356 KB |
Output is correct |
56 |
Correct |
244 ms |
27640 KB |
Output is correct |
57 |
Correct |
508 ms |
37416 KB |
Output is correct |
58 |
Correct |
432 ms |
37560 KB |
Output is correct |
59 |
Correct |
3 ms |
5800 KB |
Output is correct |
60 |
Correct |
3 ms |
5800 KB |
Output is correct |
61 |
Correct |
3 ms |
5716 KB |
Output is correct |
62 |
Correct |
776 ms |
59264 KB |
Output is correct |
63 |
Correct |
725 ms |
59136 KB |
Output is correct |
64 |
Correct |
765 ms |
58876 KB |
Output is correct |
65 |
Correct |
8 ms |
6484 KB |
Output is correct |
66 |
Correct |
15 ms |
7220 KB |
Output is correct |
67 |
Correct |
306 ms |
26748 KB |
Output is correct |
68 |
Correct |
484 ms |
37420 KB |
Output is correct |
69 |
Correct |
727 ms |
47876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
2 ms |
5716 KB |
Output is correct |
3 |
Correct |
2 ms |
5716 KB |
Output is correct |
4 |
Correct |
2 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
271 ms |
31556 KB |
Output is correct |
10 |
Correct |
13 ms |
8532 KB |
Output is correct |
11 |
Correct |
79 ms |
20064 KB |
Output is correct |
12 |
Correct |
20 ms |
9996 KB |
Output is correct |
13 |
Correct |
54 ms |
15816 KB |
Output is correct |
14 |
Correct |
6 ms |
5932 KB |
Output is correct |
15 |
Correct |
5 ms |
6228 KB |
Output is correct |
16 |
Correct |
267 ms |
31716 KB |
Output is correct |
17 |
Correct |
712 ms |
57452 KB |
Output is correct |
18 |
Correct |
644 ms |
57476 KB |
Output is correct |
19 |
Correct |
628 ms |
55476 KB |
Output is correct |
20 |
Correct |
784 ms |
44824 KB |
Output is correct |
21 |
Correct |
625 ms |
46628 KB |
Output is correct |
22 |
Correct |
3 ms |
5716 KB |
Output is correct |
23 |
Correct |
67 ms |
12664 KB |
Output is correct |
24 |
Correct |
39 ms |
9312 KB |
Output is correct |
25 |
Correct |
171 ms |
17756 KB |
Output is correct |
26 |
Correct |
312 ms |
26032 KB |
Output is correct |
27 |
Correct |
253 ms |
26272 KB |
Output is correct |
28 |
Correct |
368 ms |
31516 KB |
Output is correct |
29 |
Correct |
486 ms |
36732 KB |
Output is correct |
30 |
Correct |
626 ms |
41392 KB |
Output is correct |
31 |
Correct |
738 ms |
46764 KB |
Output is correct |
32 |
Correct |
704 ms |
50664 KB |
Output is correct |
33 |
Correct |
656 ms |
59080 KB |
Output is correct |
34 |
Correct |
9 ms |
6720 KB |
Output is correct |
35 |
Correct |
17 ms |
7456 KB |
Output is correct |
36 |
Correct |
242 ms |
27044 KB |
Output is correct |
37 |
Correct |
528 ms |
37728 KB |
Output is correct |
38 |
Correct |
713 ms |
48384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5716 KB |
Output is correct |
2 |
Correct |
2 ms |
5716 KB |
Output is correct |
3 |
Correct |
2 ms |
5716 KB |
Output is correct |
4 |
Correct |
2 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
2 ms |
5716 KB |
Output is correct |
7 |
Correct |
3 ms |
5716 KB |
Output is correct |
8 |
Correct |
2 ms |
5716 KB |
Output is correct |
9 |
Correct |
271 ms |
31556 KB |
Output is correct |
10 |
Correct |
13 ms |
8532 KB |
Output is correct |
11 |
Correct |
79 ms |
20064 KB |
Output is correct |
12 |
Correct |
20 ms |
9996 KB |
Output is correct |
13 |
Correct |
54 ms |
15816 KB |
Output is correct |
14 |
Correct |
6 ms |
5932 KB |
Output is correct |
15 |
Correct |
5 ms |
6228 KB |
Output is correct |
16 |
Correct |
267 ms |
31716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Incorrect |
2 ms |
5716 KB |
Tree @(3, 5) appears more than once: for edges on positions 0 and 2 |
19 |
Halted |
0 ms |
0 KB |
- |