이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
const int maxn = 1e5 + 7, mod = 1e9;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int n;
int siz[maxn], bio[maxn];
vector <int> adj[maxn];
int add(int x, int y) {return (x+y < mod) ? x+y : x+y-mod;}
int mul(int x, int y) {return (ll)x*y%mod;}
struct UF {
vector <int> pa, si;
void init(int n) {
pa.clear(); pa.resize(n);
si.clear(); si.resize(n, 1);
for (int i = 0; i < n; i++) pa[i] = i;
}
int find(int x) {return (x == pa[x]) ? x : (pa[x] = find(pa[x]));}
void unite(int x, int y) {
x = find(x); y = find(y);
if (x == y) return;
if (si[x] < si[y]) swap(x, y);
si[x] += si[y];
pa[y] = x;
}
} u;
int dfs(int x) {
bio[x] = 1;
siz[x] = u.si[x];
int res = 0;
for (auto y : adj[x]) {
if (bio[y]) continue;
res = add(res, dfs(y));
siz[x] += siz[y];
}
return add(res, mul(siz[x], n-siz[x]));
}
int solve(vector <pii> v) {
map <pii, int> m;
u.init(n);
for (int i = 0; i < n; i++) {
m[v[i]] = i;
siz[i] = bio[i] = 0;
adj[i].clear();
}
for (int i = 0; i < n; i++) if (m.find({v[i].F+1, v[i].S}) != m.end()) u.unite(i, m[{v[i].F+1, v[i].S}]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
int nx = v[i].F+dx[j], ny = v[i].S+dy[j];
if (m.find({nx, ny}) != m.end()) adj[u.find(i)].pb(u.find(m[{nx, ny}]));
}
}
return dfs(u.find(0));
}
int DistanceSum(int N, int *X, int *Y) {
n = N;
vector <pii> v;
for (int i = 0; i < n; i++) {
cin >> X[i] >> Y[i];
v.pb({X[i], Y[i]});
}
int res = solve(v);
for (int i = 0; i < n; i++) swap(v[i].F, v[i].S);
return add(res, solve(v));
}
# | 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... |