답안 #60678

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
60678 2018-07-24T14:05:00 Z radoslav11 이상적인 도시 (IOI12_city) C++14
32 / 100
1000 ms 52588 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include "Lgrader.cpp"

using namespace std;
using namespace __gnu_pbds;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);
const int mod = (int)1e9;

struct chash { int operator()(pair<int, int> x) const { return x.first * 42 + x.second; } };

int answer;
gp_hash_table<pair<int, int>, int, chash> S;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};

int cnt1[MAXN], cnt2[MAXN];
set<int> adj[MAXN];

int par[MAXN], sz[MAXN];
int root(int u) { return u == par[u] ? u : par[u] = root(par[u]); }
int root(pair<int, int> x) { return root(S[x]); }

void unite(pair<int, int> a, pair<int, int> b)
{
	int u = root(S[a]), v = root(S[b]);
	if(u == v) return;
	
	if(sz[u] > sz[v]) 
		swap(u, v);

	sz[v] += sz[u];
	par[u] = v;
}

int dp[MAXN];

void pre_dfs(int u, int pr)
{
	dp[u] = sz[u];
	for(int v: adj[u])
		if(v != pr)
		{
			pre_dfs(v, u);
			dp[u] = (dp[u] + dp[v]) % mod;
		}
}

int n;

void dfs(int u, int pr)
{
	if(u != pr)
	{
		int curr = 0;
		curr = (dp[u] * 1ll * (n - dp[u])) % mod;
		answer = (answer + curr) % mod;
	}

	for(int v: adj[u])
		if(v != pr)
			dfs(v, u);
}

int DistanceSum(int N, int *X, int *Y) 
{
	::n = N;
	answer = 0;
	vector<pair<int, int> > li;
	for(int i = 0; i < N; i++)
		li.push_back({X[i], Y[i]});

	int r, idx = 0;
	for(auto it: li)
		S[it] = ++idx;

	for(int i = 1; i <= idx; i++) adj[i].clear();
	for(int i = 1; i <= idx; i++)
		par[i] = i, sz[i] = 1;

	for(auto it: li)
		for(int d = 0; d < 2; d++)
		{
			pair<int, int> nxt = {dx[d] + it.first, dy[d] + it.second};
			if(S.find(nxt) != S.end())
				unite(nxt, it);
		}

	for(auto it: li)
		for(int d = 2; d < 4; d++)
		{
			pair<int, int> nxt = {dx[d] + it.first, dy[d] + it.second};
			if(S.find(nxt) != S.end())
				adj[root(nxt)].insert(root(S[it]));
		}

	r = root(1);
	pre_dfs(r, r);
	dfs(r, r);

	for(int i = 1; i <= idx; i++) adj[i].clear();
	for(int i = 1; i <= idx; i++)
		par[i] = i, sz[i] = 1;

	for(auto it: li)
		for(int d = 2; d < 4; d++)
		{
			pair<int, int> nxt = {dx[d] + it.first, dy[d] + it.second};
			if(S.find(nxt) != S.end())
				unite(nxt, it);
		}

	for(auto it: li)
		for(int d = 0; d < 2; d++)
		{
			pair<int, int> nxt = {dx[d] + it.first, dy[d] + it.second};
			if(S.find(nxt) != S.end())
				adj[root(nxt)].insert(root(S[it]));
		}

	r = root(1);
	pre_dfs(r, r);
	dfs(r, r);

	return answer;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 49528 KB Output is correct
2 Correct 58 ms 49920 KB Output is correct
3 Correct 50 ms 49920 KB Output is correct
4 Correct 60 ms 49920 KB Output is correct
5 Correct 47 ms 49920 KB Output is correct
6 Correct 55 ms 49960 KB Output is correct
7 Correct 52 ms 49960 KB Output is correct
8 Correct 54 ms 49964 KB Output is correct
9 Correct 45 ms 49968 KB Output is correct
10 Correct 45 ms 49968 KB Output is correct
11 Correct 53 ms 49976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 49980 KB Output is correct
2 Correct 48 ms 49988 KB Output is correct
3 Correct 55 ms 50136 KB Output is correct
4 Correct 54 ms 50168 KB Output is correct
5 Correct 53 ms 50324 KB Output is correct
6 Correct 74 ms 50324 KB Output is correct
7 Correct 50 ms 50324 KB Output is correct
8 Correct 48 ms 50324 KB Output is correct
9 Correct 56 ms 50324 KB Output is correct
10 Correct 61 ms 50324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1076 ms 52372 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1069 ms 52588 KB Time limit exceeded
2 Halted 0 ms 0 KB -