이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <map>
typedef long long llong;
const llong MAXN = 100000 + 10;
const llong MOD = 1'000'000'000;
llong n;
llong ans;
llong sz[MAXN];
llong par[MAXN];
llong dp[MAXN];
llong chain[MAXN];
llong inChain[MAXN];
llong x[MAXN], y[MAXN];
std::vector <llong> out[MAXN];
std::pair <llong,llong> delta[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
std::map <std::pair <llong,llong>, llong> map;
std::vector <llong> g[MAXN];
std::vector <llong> t[MAXN];
std::queue <llong> q;
void dfsDown(llong node)
{
dp[node] = 0;
sz[node] = 1;
for (const llong &u : t[node])
{
if (u == par[node])
{
continue;
}
dfsDown(u);
sz[node] += sz[u];
dp[node] += dp[u] + sz[u];
dp[node] %= MOD;
}
}
void dfsUp(llong node)
{
if (par[node] != 0)
{
dp[node] = (dp[par[node]] + n + 2 * (MOD - chain[inChain[node]])) % MOD;
}
for (const llong &u : t[node])
{
if (u != par[node])
{
dfsUp(u);
}
}
}
llong timer;
void dfsChain(llong node)
{
inChain[node] = timer;
chain[inChain[node]] += sz[node];
chain[inChain[node]] %= MOD;
for (const llong &u : out[node])
{
if (inChain[u] == 0)
{
dfsChain(u);
}
}
}
int DistanceSum(int N, int *X, int *Y)
{
n = N;
for (llong i = 1 ; i <= n ; ++i)
{
x[i] = X[i - 1];
y[i] = Y[i - 1];
map[{x[i], y[i]}] = i;
}
for (llong i = 1 ; i <= n ; ++i)
{
for (const auto &[dx, dy] : delta)
{
if (map.count({x[i] + dx, y[i] + dy}))
{
g[i].push_back(map[{x[i] + dx, y[i] + dy}]);
}
}
}
q.push(n/2 + 1);
std::fill(par + 1, par + n + 1, -1);
par[n/2 + 1] = 0;
while (!q.empty())
{
llong top = q.front();
q.pop();
for (const llong &i : g[top])
{
if (par[i] == -1)
{
par[i] = top;
t[top].push_back(i);
q.push(i);
} else if (par[i] != top && par[top] != i)
{
out[top].push_back(i);
}
}
}
dfsDown(n/2 + 1);
for (llong i = 1 ; i <= n ; ++i)
{
if (inChain[i] == 0)
{
timer++;
dfsChain(i);
}
}
dfsUp(n/2 + 1);
for (llong i = 1 ; i <= n ; ++i)
{
ans += dp[i];
ans %= MOD;
}
return ans / 2;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |