Submission #1012048

#TimeUsernameProblemLanguageResultExecution timeMemory
1012048AmirAli_H1Ideal city (IOI12_city)C++17
100 / 100
297 ms19540 KiB
// In the name of Allah #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef complex<ld> cld; #define all(x) (x).begin(),(x).end() #define len(x) ((ll) (x).size()) #define F first #define S second #define pb push_back #define sep ' ' #define endl '\n' #define Mp make_pair #define kill(x) cout << x << '\n', exit(0) #define set_dec(x) cout << fixed << setprecision(x); #define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int maxn = 1e5 + 7; const int mod = 1e9; int n; set<int> adj[maxn]; pii A[maxn]; map<pii, int> ind; int p[maxn], sz[maxn]; ll res; int get(int a) { return (p[a] == a) ? a : p[a] = get(p[a]); } void merge(int a, int b) { a = get(a); b = get(b); if (a == b) return ; if (sz[a] > sz[b]) swap(a, b); p[a] = b; sz[b] += sz[a]; sz[a] = 0; } void dfs(int v, int p = -1) { for (int u : adj[v]) { if (u == p) continue; dfs(u, v); sz[v] += sz[u]; res += 1ll * sz[u] * (n - sz[u]) % mod; res %= mod; } } int DistanceSum(int N, int X[], int Y[]) { n = N; ind.clear(); for (int i = 0; i < n; i++) { A[i] = Mp(X[i], Y[i]); ind[A[i]] = i; } res = 0; iota(p, p + n, 0); fill(sz, sz + n, 1); for (int i = 0; i < n; i++) adj[i].clear(); for (int i = 0; i < n; i++) { int x = A[i].F, y = A[i].S; for (int x1 : {x - 1, x + 1}) { if (ind.find(Mp(x1, y)) == ind.end()) continue; int j = ind[Mp(x1, y)]; merge(i, j); } } for (int i = 0; i < n; i++) { int x = A[i].F, y = A[i].S; for (int y1 : {y - 1, y + 1}) { if (ind.find(Mp(x, y1)) == ind.end()) continue; int j = ind[Mp(x, y1)]; int u = get(i), v = get(j); adj[u].insert(v); adj[v].insert(u); } } dfs(get(0)); iota(p, p + n, 0); fill(sz, sz + n, 1); for (int i = 0; i < n; i++) adj[i].clear(); for (int i = 0; i < n; i++) { int x = A[i].F, y = A[i].S; for (int y1 : {y - 1, y + 1}) { if (ind.find(Mp(x, y1)) == ind.end()) continue; int j = ind[Mp(x, y1)]; merge(i, j); } } for (int i = 0; i < n; i++) { int x = A[i].F, y = A[i].S; for (int x1 : {x - 1, x + 1}) { if (ind.find(Mp(x1, y)) == ind.end()) continue; int j = ind[Mp(x1, y)]; int u = get(i), v = get(j); adj[u].insert(v); adj[v].insert(u); } } dfs(get(0)); return res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...