//Challenge: Accepted
#include "parks.h"
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define pii pair<ll, ll>
#define ff first
#define ss second
vector<pii> xv[maxn], yv[maxn];
unordered_map<ll, vector<pii> > adj;
unordered_map<ll, bool> vis;
ll h(int x, int y) {
return (ll)x * maxn + y;
}
struct DSU{
int par[maxn];
int merges = 0;
void init(int n) {
for (int i = 0;i < n;i++) par[i] = i;
}
int find(int a) {
return a == par[a] ? a : (par[a] = find(par[a]));
}
void Union(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
merges++;
par[a] = b;
}
} dsu;
vector<int> u, v, a, b;
void dfs(ll n, ll prv) {
vis[n] = 1;
for (auto p:adj[n]) {
if (p.ff != prv) {
u.push_back(p.ss / maxn);
v.push_back(p.ss % maxn);
a.push_back(n / maxn);
b.push_back(n % maxn);
if (!vis[p.ff]) dfs(p.ff, n);
}
}
}
int construct_roads(std::vector<int> x, std::vector<int> y) {
int n = x.size();
dsu.init(n);
for (int i = 0;i < n;i++) {
xv[x[i]].push_back({y[i], i});
yv[y[i]].push_back({x[i], i});
}
for (int i = 0;i < maxn;i += 2) {
sort(xv[i].begin(), xv[i].end());
sort(yv[i].begin(), yv[i].end());
for (int j = 0;j < (int)xv[i].size() - 1;j++) {
if (xv[i][j].ff + 2 == xv[i][j+1].ff) {
dsu.Union(xv[i][j].ss, xv[i][j+1].ss);
u.push_back(xv[i][j].ss);
v.push_back(xv[i][j+1].ss);
b.push_back(xv[i][j].ff + 1);
if (i == 2) a.push_back(1);
else a.push_back(7);
/*
ll ind = h(xv[i][j].ss, xv[i][j+1].ss), v1 = h(i-1, xv[i][j].ff + 1), v2 = h(i+1, xv[i][j].ff + 1);
debug(v1, v2);
adj[v1].push_back({v2, ind});
adj[v2].push_back({v1, ind});
*/
}
}
for (int j = 0;j < (int)yv[i].size() - 1;j++) {
if (yv[i][j].ff + 2 == yv[i][j+1].ff) {
dsu.Union(yv[i][j].ss, yv[i][j+1].ss);
ll ind = h(yv[i][j].ss, yv[i][j+1].ss), v1 = h(yv[i][j].ff + 1, i-1), v2 = h(yv[i][j].ff + 1, i+1);
adj[v1].push_back({v2, ind});
adj[v2].push_back({v1, ind});
}
}
}
{
int i = 4;
for (int j = 0;j < (int)xv[i].size() - 1;j++) {
if (xv[i][j].ff + 2 == xv[i][j+1].ff) {
if (dsu.find(xv[i][j].ss) == dsu.find(xv[i][j+1].ss)) continue;
dsu.Union(xv[i][j].ss, xv[i][j+1].ss);
ll ind = h(xv[i][j].ss, xv[i][j+1].ss), v1 = h(i-1, xv[i][j].ff + 1), v2 = h(i+1, xv[i][j].ff + 1);
adj[v1].push_back({v2, ind});
adj[v2].push_back({v1, ind});
}
}
}
if (dsu.merges != n - 1) return 0;
for (auto i:adj) {
if (i.ss.size() == 1 && !vis[i.ff]) dfs(i.ff, -1);
}
for (auto i:adj) {
if (!vis[i.ff]) dfs(i.ff, i.ss[0].ff);
}
build(u, v, a, b);
return 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9700 KB |
Output is correct |
7 |
Correct |
6 ms |
9700 KB |
Output is correct |
8 |
Correct |
7 ms |
9704 KB |
Output is correct |
9 |
Correct |
103 ms |
22672 KB |
Output is correct |
10 |
Correct |
14 ms |
11080 KB |
Output is correct |
11 |
Correct |
38 ms |
16608 KB |
Output is correct |
12 |
Correct |
15 ms |
11728 KB |
Output is correct |
13 |
Correct |
18 ms |
14036 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
80 ms |
22700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9700 KB |
Output is correct |
7 |
Correct |
6 ms |
9700 KB |
Output is correct |
8 |
Correct |
7 ms |
9704 KB |
Output is correct |
9 |
Correct |
103 ms |
22672 KB |
Output is correct |
10 |
Correct |
14 ms |
11080 KB |
Output is correct |
11 |
Correct |
38 ms |
16608 KB |
Output is correct |
12 |
Correct |
15 ms |
11728 KB |
Output is correct |
13 |
Correct |
18 ms |
14036 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
80 ms |
22700 KB |
Output is correct |
17 |
Incorrect |
8 ms |
9692 KB |
Tree (a[1], b[1]) = (7, 3) is not adjacent to edge between u[1]=2 @(4, 2) and v[1]=0 @(4, 4) |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9700 KB |
Output is correct |
7 |
Correct |
6 ms |
9700 KB |
Output is correct |
8 |
Correct |
7 ms |
9704 KB |
Output is correct |
9 |
Correct |
103 ms |
22672 KB |
Output is correct |
10 |
Correct |
14 ms |
11080 KB |
Output is correct |
11 |
Correct |
38 ms |
16608 KB |
Output is correct |
12 |
Correct |
15 ms |
11728 KB |
Output is correct |
13 |
Correct |
18 ms |
14036 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
80 ms |
22700 KB |
Output is correct |
17 |
Incorrect |
8 ms |
9692 KB |
Tree (a[1], b[1]) = (7, 3) is not adjacent to edge between u[1]=2 @(4, 2) and v[1]=0 @(4, 4) |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9700 KB |
Output is correct |
7 |
Correct |
6 ms |
9700 KB |
Output is correct |
8 |
Correct |
7 ms |
9704 KB |
Output is correct |
9 |
Correct |
103 ms |
22672 KB |
Output is correct |
10 |
Correct |
14 ms |
11080 KB |
Output is correct |
11 |
Correct |
38 ms |
16608 KB |
Output is correct |
12 |
Correct |
15 ms |
11728 KB |
Output is correct |
13 |
Correct |
18 ms |
14036 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
80 ms |
22700 KB |
Output is correct |
17 |
Incorrect |
5 ms |
9684 KB |
Tree (a[0], b[0]) = (7, 3) is not adjacent to edge between u[0]=0 @(200000, 2) and v[0]=1 @(200000, 4) |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9700 KB |
Output is correct |
7 |
Correct |
6 ms |
9700 KB |
Output is correct |
8 |
Correct |
7 ms |
9704 KB |
Output is correct |
9 |
Correct |
103 ms |
22672 KB |
Output is correct |
10 |
Correct |
14 ms |
11080 KB |
Output is correct |
11 |
Correct |
38 ms |
16608 KB |
Output is correct |
12 |
Correct |
15 ms |
11728 KB |
Output is correct |
13 |
Correct |
18 ms |
14036 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
80 ms |
22700 KB |
Output is correct |
17 |
Incorrect |
292 ms |
63748 KB |
Tree (a[50000], b[50000]) = (7, 3) is not adjacent to edge between u[50000]=138413 @(100002, 2) and v[50000]=87394 @(100002, 4) |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
7 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
6 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9700 KB |
Output is correct |
7 |
Correct |
6 ms |
9700 KB |
Output is correct |
8 |
Correct |
7 ms |
9704 KB |
Output is correct |
9 |
Correct |
103 ms |
22672 KB |
Output is correct |
10 |
Correct |
14 ms |
11080 KB |
Output is correct |
11 |
Correct |
38 ms |
16608 KB |
Output is correct |
12 |
Correct |
15 ms |
11728 KB |
Output is correct |
13 |
Correct |
18 ms |
14036 KB |
Output is correct |
14 |
Correct |
6 ms |
9684 KB |
Output is correct |
15 |
Correct |
7 ms |
9812 KB |
Output is correct |
16 |
Correct |
80 ms |
22700 KB |
Output is correct |
17 |
Incorrect |
8 ms |
9692 KB |
Tree (a[1], b[1]) = (7, 3) is not adjacent to edge between u[1]=2 @(4, 2) and v[1]=0 @(4, 4) |
18 |
Halted |
0 ms |
0 KB |
- |