Submission #1012048

# Submission time Handle Problem Language Result Execution time Memory
1012048 2024-07-01T15:05:49 Z AmirAli_H1 Ideal city (IOI12_city) C++17
100 / 100
297 ms 19540 KB
// 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 time Memory Grader output
1 Correct 1 ms 6744 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6492 KB Output is correct
4 Correct 2 ms 6492 KB Output is correct
5 Correct 2 ms 6540 KB Output is correct
6 Correct 2 ms 6492 KB Output is correct
7 Correct 2 ms 6492 KB Output is correct
8 Correct 2 ms 6492 KB Output is correct
9 Correct 2 ms 6556 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 2 ms 6504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6488 KB Output is correct
2 Correct 3 ms 6488 KB Output is correct
3 Correct 4 ms 6492 KB Output is correct
4 Correct 4 ms 6492 KB Output is correct
5 Correct 5 ms 6784 KB Output is correct
6 Correct 4 ms 6748 KB Output is correct
7 Correct 5 ms 6748 KB Output is correct
8 Correct 5 ms 6492 KB Output is correct
9 Correct 5 ms 6492 KB Output is correct
10 Correct 5 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 8024 KB Output is correct
2 Correct 48 ms 8276 KB Output is correct
3 Correct 121 ms 10580 KB Output is correct
4 Correct 123 ms 10584 KB Output is correct
5 Correct 253 ms 14420 KB Output is correct
6 Correct 259 ms 14676 KB Output is correct
7 Correct 285 ms 14736 KB Output is correct
8 Correct 273 ms 14416 KB Output is correct
9 Correct 267 ms 15288 KB Output is correct
10 Correct 231 ms 19540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 8796 KB Output is correct
2 Correct 46 ms 8540 KB Output is correct
3 Correct 125 ms 12408 KB Output is correct
4 Correct 133 ms 11940 KB Output is correct
5 Correct 277 ms 18256 KB Output is correct
6 Correct 292 ms 16216 KB Output is correct
7 Correct 275 ms 18352 KB Output is correct
8 Correct 291 ms 16208 KB Output is correct
9 Correct 297 ms 15600 KB Output is correct
10 Correct 290 ms 15856 KB Output is correct