답안 #363081

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
363081 2021-02-05T04:25:20 Z 8e7 이상적인 도시 (IOI12_city) C++14
0 / 100
37 ms 7400 KB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <set>
#include <queue>
#define ll long long
#define maxn 100005
#define mod 1000000000
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector<int> adj[maxn], tree[maxn];
vector<pii> a;
int tot;
int dis[maxn], par[maxn], siz[maxn];
ll pref[maxn], dsum[maxn];
bool found[maxn];
ll ans = 0;

void dfs(int n, int p) {
	siz[n] = 1;
	for (int v:tree[n]) {
		if (v != p) {
			dfs(v, n);
			siz[n] += siz[v];
			dsum[n] += dsum[v] + siz[v];
		}
	}

}

void reroot(int u, int v) {
	int st, ed;
	if (a[u].ff == a[v].ff) { //vertical
		st = 0, ed = 2;
	} else {
		st = 2, ed = 4;
	}
	//cout << u << ' ' << v << " " << siz[u] << " " << dsum[u] << " " << siz[v] << " " << dsum[v] << endl;
	siz[u] -= siz[v];
	dsum[u] -= dsum[v] + siz[v];
	for (int k = st;k < ed;k++) {
		pii xp = make_pair(a[u].ff + dir[k][0], a[u].ss + dir[k][1]), yp = make_pair(a[v].ff + dir[k][0], a[v].ss + dir[k][1]);
		int x = lower_bound(a.begin(), a.end(), xp) - a.begin();
		int y = lower_bound(a.begin(), a.end(), yp) - a.begin();
		//cout << x << " " << y << endl;
		if (x < tot && y < tot && a[x] == xp && a[y] == yp && par[y] == x) {
			//cout << "edge\n";
			siz[u] -= siz[x];
			dsum[u] -= siz[x] + dsum[x];

			int tmp = siz[x];
			siz[x] -= siz[y], dsum[x] -= dsum[y] + siz[y];
			siz[y] = tmp, dsum[y] += dsum[x] + siz[x];

			par[x] = y, par[y] = v;
			siz[v] += siz[y], dsum[v] += siz[y] + dsum[y];
		}
	}
	siz[v] = tot;
	dsum[v] += dsum[u] + siz[u];
	par[v] = -1, par[u] = v;
	//cout << u << ' ' << v << " " << siz[u] << " " << dsum[u] << " " << siz[v] << " " << dsum[v] << endl;
	//cout << endl;
}
void move(int n, int par) {
	//cout << n << ' ' << dsum[n] << endl;
	ans += dsum[n];
	for (int v:tree[n]) {
		if (v != par) {
			reroot(n, v);
			move(v, n);
			reroot(v, n);
		}
	}
}
int DistanceSum(int N, int *X, int *Y) {
	tot = N;
	for (int i = 0;i < N;i++) {
		a.push_back(make_pair(X[i], Y[i]));
	}
	sort(a.begin(), a.end());
	for (int i = 0;i < N;i++) {
		for (int k = 0;k < 4;k++) {
			pii ne = make_pair(a[i].ff + dir[k][0], a[i].ss + dir[k][1]);
			int id = lower_bound(a.begin(), a.end(), ne) - a.begin();
			if (id < N && a[id] == ne) {
				adj[i].push_back(id);
				adj[id].push_back(i);
			}
		}
	}
	for (int j = 0;j < N;j++) found[j] = 0, dis[j] = 0;
	queue<int> que;
	que.push(0);
	found[0] = 1, par[0] = -1;
	while (que.size()) {
		int cur = que.front();
		que.pop();
		for (int v:adj[cur]) {
			if (!found[v]) {
				dis[v] = dis[cur] + 1;
				tree[cur].push_back(v);
				tree[v].push_back(cur);
				par[v] = cur;
				que.push(v);
				found[v] = 1;
			}
		}
	}
	dfs(0, -1);
	//for (int i = 0;i < N;i++) cout << par[i] << endl;
	move(0, -1);
	return (ans / 2) % mod;
}
/*
int main() {
	io
	int n;
	cin >> n;
	int x[n], y[n];
	for (int i = 0;i < n;i++) cin >> x[i];
	for (int i = 0;i <n ;i++) cin >> y[i];
	cout << DistanceSum(n, x, y);
}
/*
11
2 2 3 3 4 4 4 4 5 5 5
5 6 3 6 3 4 5 6 3 4 6

6
0 0 1 1 1 1
0 1 1 2 3 4
 */

Compilation message

city.cpp:132:1: warning: "/*" within comment [-Wcomment]
  132 | /*
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5120 KB Output is correct
2 Incorrect 3 ms 5100 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 5100 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 37 ms 7400 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 35 ms 7292 KB Output isn't correct
2 Halted 0 ms 0 KB -