#include <bits/stdc++.h>
#include "parks.h"
using namespace std;
typedef long long ll;
struct Edge{
int u, v, idx;
Edge(){}
Edge(int u, int v, int idx): u(u), v(v), idx(idx){}
bool operator==(const Edge &r)const{
return idx==r.idx;
}
};
struct dat{
int x, y;
vector<Edge> vec;
dat(){}
dat(int x, int y, vector<Edge> vec): x(x), y(y), vec(vec){}
bool operator<(const dat &r)const{
if(vec.size() != r.vec.size()) return vec.size() < r.vec.size();
return make_pair(x, y) < make_pair(r.x, r.y);
}
};
int xx[]={1, -1, 0, 0}, yy[]={0, 0, 1, -1};
int n;
pair<int, int> arr[200002];
map<pair<int, int>, int> idx;
int par[200002];
int edgeU[200002], edgeV[200002], edgeCnt;
int edgeA[200002], edgeB[200002];
vector<pair<int, int> > link[200002];
map<pair<int, int>, vector<Edge> > mp;
set<dat> st;
int find(int x){
if(x==par[x]) return x;
return par[x] = find(par[x]);
}
void merge(int x, int y){
x = find(x), y = find(y);
par[y] = x;
}
void dfs(int u){
int x = arr[u].first, y = arr[u].second;
for(int d=0; d<4; d++){
int tx = x+xx[d]+xx[d], ty = y+yy[d]+yy[d];
if(idx.find(make_pair(tx, ty)) == idx.end()) continue;
int tmp = idx[make_pair(tx, ty)];
if(find(u) == find(tmp)) continue;
edgeU[edgeCnt] = u, edgeV[edgeCnt] = tmp;
if(x == tx){
mp[make_pair(x+1, y+yy[d])].push_back(Edge(u, tmp, edgeCnt));
mp[make_pair(x-1, y+yy[d])].push_back(Edge(u, tmp, edgeCnt));
link[edgeCnt].push_back(make_pair(x+1, y+yy[d]));
link[edgeCnt].push_back(make_pair(x-1, y+yy[d]));
}
else{
mp[make_pair(x+xx[d], y+1)].push_back(Edge(u, tmp, edgeCnt));
mp[make_pair(x+xx[d], y-1)].push_back(Edge(u, tmp, edgeCnt));
link[edgeCnt].push_back(make_pair(x+xx[d], y-1));
link[edgeCnt].push_back(make_pair(x+xx[d], y+1));
}
edgeCnt++;
merge(u, tmp);
dfs(tmp);
}
}
int construct_roads(vector<int> x, vector<int> y) {
n = (int)x.size();
for(int i=0; i<n; i++){
arr[i] = make_pair(x[i], y[i]);
par[i] = i;
idx[arr[i]] = i;
}
int start = min_element(arr, arr+n) - arr;
dfs(start);
for(int i=0; i<n; i++){
if(find(i) != find(0)) return 0;
}
for(auto p: mp){
st.insert(dat(p.first.first, p.first.second, p.second));
}
while(!st.empty()){
dat tmp = *st.begin(); st.erase(st.begin());
if(tmp.vec.empty()) continue;
Edge edge = tmp.vec[0];
edgeA[edge.idx] = tmp.x;
edgeB[edge.idx] = tmp.y;
for(auto p: link[edge.idx]){
if(p.first == tmp.x && p.second == tmp.y) continue;
st.erase(dat(p.first, p.second, mp[p]));
auto it = find(mp[p].begin(), mp[p].end(), Edge(edgeU[edge.idx], edgeV[edge.idx], edge.idx));
assert(it != mp[p].end());
mp[p].erase(it);
st.insert(dat(p.first, p.second, mp[p]));
}
}
build(vector<int>(edgeU, edgeU+n-1), vector<int>(edgeV, edgeV+n-1), vector<int>(edgeA, edgeA+n-1), vector<int>(edgeB, edgeB+n-1));
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
566 ms |
76232 KB |
Output is correct |
10 |
Correct |
52 ms |
12100 KB |
Output is correct |
11 |
Correct |
269 ms |
43352 KB |
Output is correct |
12 |
Correct |
69 ms |
15736 KB |
Output is correct |
13 |
Correct |
82 ms |
20588 KB |
Output is correct |
14 |
Correct |
5 ms |
5196 KB |
Output is correct |
15 |
Correct |
6 ms |
5324 KB |
Output is correct |
16 |
Correct |
589 ms |
76200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
566 ms |
76232 KB |
Output is correct |
10 |
Correct |
52 ms |
12100 KB |
Output is correct |
11 |
Correct |
269 ms |
43352 KB |
Output is correct |
12 |
Correct |
69 ms |
15736 KB |
Output is correct |
13 |
Correct |
82 ms |
20588 KB |
Output is correct |
14 |
Correct |
5 ms |
5196 KB |
Output is correct |
15 |
Correct |
6 ms |
5324 KB |
Output is correct |
16 |
Correct |
589 ms |
76200 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
5 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
5004 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
4940 KB |
Output is correct |
23 |
Correct |
1092 ms |
108652 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
25 |
Correct |
8 ms |
5560 KB |
Output is correct |
26 |
Correct |
8 ms |
5708 KB |
Output is correct |
27 |
Correct |
7 ms |
5452 KB |
Output is correct |
28 |
Correct |
433 ms |
46312 KB |
Output is correct |
29 |
Correct |
594 ms |
67092 KB |
Output is correct |
30 |
Correct |
809 ms |
87868 KB |
Output is correct |
31 |
Correct |
1092 ms |
108688 KB |
Output is correct |
32 |
Correct |
4 ms |
4940 KB |
Output is correct |
33 |
Correct |
4 ms |
4940 KB |
Output is correct |
34 |
Correct |
4 ms |
4940 KB |
Output is correct |
35 |
Correct |
4 ms |
4940 KB |
Output is correct |
36 |
Correct |
4 ms |
4940 KB |
Output is correct |
37 |
Correct |
4 ms |
4940 KB |
Output is correct |
38 |
Correct |
4 ms |
4940 KB |
Output is correct |
39 |
Correct |
5 ms |
5028 KB |
Output is correct |
40 |
Correct |
4 ms |
4940 KB |
Output is correct |
41 |
Correct |
5 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
4940 KB |
Output is correct |
43 |
Correct |
7 ms |
5452 KB |
Output is correct |
44 |
Correct |
5 ms |
5452 KB |
Output is correct |
45 |
Correct |
499 ms |
60196 KB |
Output is correct |
46 |
Correct |
728 ms |
84956 KB |
Output is correct |
47 |
Correct |
832 ms |
85076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
566 ms |
76232 KB |
Output is correct |
10 |
Correct |
52 ms |
12100 KB |
Output is correct |
11 |
Correct |
269 ms |
43352 KB |
Output is correct |
12 |
Correct |
69 ms |
15736 KB |
Output is correct |
13 |
Correct |
82 ms |
20588 KB |
Output is correct |
14 |
Correct |
5 ms |
5196 KB |
Output is correct |
15 |
Correct |
6 ms |
5324 KB |
Output is correct |
16 |
Correct |
589 ms |
76200 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
5 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
5004 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
4940 KB |
Output is correct |
23 |
Correct |
1092 ms |
108652 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
25 |
Correct |
8 ms |
5560 KB |
Output is correct |
26 |
Correct |
8 ms |
5708 KB |
Output is correct |
27 |
Correct |
7 ms |
5452 KB |
Output is correct |
28 |
Correct |
433 ms |
46312 KB |
Output is correct |
29 |
Correct |
594 ms |
67092 KB |
Output is correct |
30 |
Correct |
809 ms |
87868 KB |
Output is correct |
31 |
Correct |
1092 ms |
108688 KB |
Output is correct |
32 |
Correct |
4 ms |
4940 KB |
Output is correct |
33 |
Correct |
4 ms |
4940 KB |
Output is correct |
34 |
Correct |
4 ms |
4940 KB |
Output is correct |
35 |
Correct |
4 ms |
4940 KB |
Output is correct |
36 |
Correct |
4 ms |
4940 KB |
Output is correct |
37 |
Correct |
4 ms |
4940 KB |
Output is correct |
38 |
Correct |
4 ms |
4940 KB |
Output is correct |
39 |
Correct |
5 ms |
5028 KB |
Output is correct |
40 |
Correct |
4 ms |
4940 KB |
Output is correct |
41 |
Correct |
5 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
4940 KB |
Output is correct |
43 |
Correct |
7 ms |
5452 KB |
Output is correct |
44 |
Correct |
5 ms |
5452 KB |
Output is correct |
45 |
Correct |
499 ms |
60196 KB |
Output is correct |
46 |
Correct |
728 ms |
84956 KB |
Output is correct |
47 |
Correct |
832 ms |
85076 KB |
Output is correct |
48 |
Correct |
4 ms |
4940 KB |
Output is correct |
49 |
Correct |
4 ms |
4940 KB |
Output is correct |
50 |
Correct |
4 ms |
4940 KB |
Output is correct |
51 |
Correct |
4 ms |
4940 KB |
Output is correct |
52 |
Correct |
4 ms |
4940 KB |
Output is correct |
53 |
Correct |
4 ms |
4940 KB |
Output is correct |
54 |
Correct |
4 ms |
4940 KB |
Output is correct |
55 |
Correct |
1210 ms |
107064 KB |
Output is correct |
56 |
Correct |
5 ms |
4940 KB |
Output is correct |
57 |
Correct |
12 ms |
5888 KB |
Output is correct |
58 |
Correct |
23 ms |
7992 KB |
Output is correct |
59 |
Correct |
11 ms |
6348 KB |
Output is correct |
60 |
Correct |
517 ms |
55852 KB |
Output is correct |
61 |
Correct |
722 ms |
73736 KB |
Output is correct |
62 |
Correct |
971 ms |
89464 KB |
Output is correct |
63 |
Correct |
1157 ms |
107052 KB |
Output is correct |
64 |
Correct |
4 ms |
4940 KB |
Output is correct |
65 |
Correct |
4 ms |
4940 KB |
Output is correct |
66 |
Correct |
4 ms |
4940 KB |
Output is correct |
67 |
Correct |
1250 ms |
147796 KB |
Output is correct |
68 |
Correct |
1321 ms |
135364 KB |
Output is correct |
69 |
Correct |
1241 ms |
140836 KB |
Output is correct |
70 |
Correct |
8 ms |
5836 KB |
Output is correct |
71 |
Correct |
10 ms |
6220 KB |
Output is correct |
72 |
Runtime error |
481 ms |
112144 KB |
Execution killed with signal 6 |
73 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
566 ms |
76232 KB |
Output is correct |
10 |
Correct |
52 ms |
12100 KB |
Output is correct |
11 |
Correct |
269 ms |
43352 KB |
Output is correct |
12 |
Correct |
69 ms |
15736 KB |
Output is correct |
13 |
Correct |
82 ms |
20588 KB |
Output is correct |
14 |
Correct |
5 ms |
5196 KB |
Output is correct |
15 |
Correct |
6 ms |
5324 KB |
Output is correct |
16 |
Correct |
589 ms |
76200 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
990 ms |
104852 KB |
Output is correct |
21 |
Correct |
1014 ms |
92016 KB |
Output is correct |
22 |
Correct |
999 ms |
104636 KB |
Output is correct |
23 |
Correct |
953 ms |
121704 KB |
Output is correct |
24 |
Correct |
214 ms |
23004 KB |
Output is correct |
25 |
Correct |
231 ms |
23204 KB |
Output is correct |
26 |
Correct |
200 ms |
23140 KB |
Output is correct |
27 |
Correct |
1004 ms |
122700 KB |
Output is correct |
28 |
Correct |
973 ms |
122648 KB |
Output is correct |
29 |
Correct |
1247 ms |
122676 KB |
Output is correct |
30 |
Correct |
1304 ms |
122756 KB |
Output is correct |
31 |
Correct |
4 ms |
4940 KB |
Output is correct |
32 |
Runtime error |
71 ms |
23336 KB |
Execution killed with signal 6 |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
566 ms |
76232 KB |
Output is correct |
10 |
Correct |
52 ms |
12100 KB |
Output is correct |
11 |
Correct |
269 ms |
43352 KB |
Output is correct |
12 |
Correct |
69 ms |
15736 KB |
Output is correct |
13 |
Correct |
82 ms |
20588 KB |
Output is correct |
14 |
Correct |
5 ms |
5196 KB |
Output is correct |
15 |
Correct |
6 ms |
5324 KB |
Output is correct |
16 |
Correct |
589 ms |
76200 KB |
Output is correct |
17 |
Correct |
1189 ms |
148248 KB |
Output is correct |
18 |
Correct |
1266 ms |
141980 KB |
Output is correct |
19 |
Correct |
1027 ms |
104592 KB |
Output is correct |
20 |
Correct |
1119 ms |
108144 KB |
Output is correct |
21 |
Correct |
1075 ms |
111968 KB |
Output is correct |
22 |
Correct |
4 ms |
4940 KB |
Output is correct |
23 |
Runtime error |
140 ms |
37628 KB |
Execution killed with signal 6 |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
4 ms |
4940 KB |
Output is correct |
8 |
Correct |
4 ms |
4940 KB |
Output is correct |
9 |
Correct |
566 ms |
76232 KB |
Output is correct |
10 |
Correct |
52 ms |
12100 KB |
Output is correct |
11 |
Correct |
269 ms |
43352 KB |
Output is correct |
12 |
Correct |
69 ms |
15736 KB |
Output is correct |
13 |
Correct |
82 ms |
20588 KB |
Output is correct |
14 |
Correct |
5 ms |
5196 KB |
Output is correct |
15 |
Correct |
6 ms |
5324 KB |
Output is correct |
16 |
Correct |
589 ms |
76200 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
4 ms |
4940 KB |
Output is correct |
19 |
Correct |
5 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
5004 KB |
Output is correct |
21 |
Correct |
4 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
4940 KB |
Output is correct |
23 |
Correct |
1092 ms |
108652 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
25 |
Correct |
8 ms |
5560 KB |
Output is correct |
26 |
Correct |
8 ms |
5708 KB |
Output is correct |
27 |
Correct |
7 ms |
5452 KB |
Output is correct |
28 |
Correct |
433 ms |
46312 KB |
Output is correct |
29 |
Correct |
594 ms |
67092 KB |
Output is correct |
30 |
Correct |
809 ms |
87868 KB |
Output is correct |
31 |
Correct |
1092 ms |
108688 KB |
Output is correct |
32 |
Correct |
4 ms |
4940 KB |
Output is correct |
33 |
Correct |
4 ms |
4940 KB |
Output is correct |
34 |
Correct |
4 ms |
4940 KB |
Output is correct |
35 |
Correct |
4 ms |
4940 KB |
Output is correct |
36 |
Correct |
4 ms |
4940 KB |
Output is correct |
37 |
Correct |
4 ms |
4940 KB |
Output is correct |
38 |
Correct |
4 ms |
4940 KB |
Output is correct |
39 |
Correct |
5 ms |
5028 KB |
Output is correct |
40 |
Correct |
4 ms |
4940 KB |
Output is correct |
41 |
Correct |
5 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
4940 KB |
Output is correct |
43 |
Correct |
7 ms |
5452 KB |
Output is correct |
44 |
Correct |
5 ms |
5452 KB |
Output is correct |
45 |
Correct |
499 ms |
60196 KB |
Output is correct |
46 |
Correct |
728 ms |
84956 KB |
Output is correct |
47 |
Correct |
832 ms |
85076 KB |
Output is correct |
48 |
Correct |
4 ms |
4940 KB |
Output is correct |
49 |
Correct |
4 ms |
4940 KB |
Output is correct |
50 |
Correct |
4 ms |
4940 KB |
Output is correct |
51 |
Correct |
4 ms |
4940 KB |
Output is correct |
52 |
Correct |
4 ms |
4940 KB |
Output is correct |
53 |
Correct |
4 ms |
4940 KB |
Output is correct |
54 |
Correct |
4 ms |
4940 KB |
Output is correct |
55 |
Correct |
1210 ms |
107064 KB |
Output is correct |
56 |
Correct |
5 ms |
4940 KB |
Output is correct |
57 |
Correct |
12 ms |
5888 KB |
Output is correct |
58 |
Correct |
23 ms |
7992 KB |
Output is correct |
59 |
Correct |
11 ms |
6348 KB |
Output is correct |
60 |
Correct |
517 ms |
55852 KB |
Output is correct |
61 |
Correct |
722 ms |
73736 KB |
Output is correct |
62 |
Correct |
971 ms |
89464 KB |
Output is correct |
63 |
Correct |
1157 ms |
107052 KB |
Output is correct |
64 |
Correct |
4 ms |
4940 KB |
Output is correct |
65 |
Correct |
4 ms |
4940 KB |
Output is correct |
66 |
Correct |
4 ms |
4940 KB |
Output is correct |
67 |
Correct |
1250 ms |
147796 KB |
Output is correct |
68 |
Correct |
1321 ms |
135364 KB |
Output is correct |
69 |
Correct |
1241 ms |
140836 KB |
Output is correct |
70 |
Correct |
8 ms |
5836 KB |
Output is correct |
71 |
Correct |
10 ms |
6220 KB |
Output is correct |
72 |
Runtime error |
481 ms |
112144 KB |
Execution killed with signal 6 |
73 |
Halted |
0 ms |
0 KB |
- |