#include <iostream>
#include <vector>
#include <set>
#include <cassert>
#include <queue>
#include <map>
#include <algorithm>
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(xs) xs.begin(), xs.end()
#define pb push_back
#define _1 first
#define _2 second
#define INF 1145141919
#define MOD 1000000000
using namespace std;
int DX[4] = {-1, 0, 1, 0};
int DY[4] = {0, -1, 0, 1};
typedef pair<int, int> P;
map<P, int> mp;
vector<int> G[100000];
int par[100000], ord[100000], low[100000], sz[100000];
int W[100000];
vector<P> bridges, els;
void dfs(int x, int p, int r) {
par[x] = p;
sz[x] = 1;
ord[x] = low[x] = r;
for (int t : G[x]) if (t != p) {
if (ord[t] != -1) {
if (ord[x] < ord[t]) els.pb(P(x, t));
low[x] = min(low[x], ord[t]);
continue;
}
dfs(t, x, r+1);
sz[x] += sz[t];
low[x] = min(low[x], low[t]);
if (ord[x] < low[t]) bridges.pb(P(x, t));
else els.pb(P(x, t));
}
}
int U[100000], R[100000];
vector<int> G2[100000];
int find(int x) {
if (U[x] == x) return x;
return U[x] = find(U[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) return;
if (R[x] < R[y]) swap(x, y);
U[y] = x;
R[x] += R[y];
}
int count(vector<P> seq) {
sort(all(seq));
int all = 0, sum = 0, ret = 0, last = seq[0]._1;
for (P p : seq) all += p._2;
for (P p : seq) {
if (last != p._1) {
ret = (ret + 1LL*(p._1-last)*((1LL*sum*(all-sum)) % MOD)) % MOD;
last = p._1;
}
sum += p._2;
}
return ret;
}
int DistanceSum(int N, int *X, int *Y) {
rep(i, N) mp[P(X[i], Y[i])] = i;
rep(i, N) {
rep(k, 2) {
P np = P(X[i]+DX[k], Y[i]+DY[k]);
if (mp.find(np) != mp.end()) {
int t = mp[np];
G[i].pb(t);
G[t].pb(i);
}
}
}
rep(i, N) ord[i] = low[i] = -1;
dfs(0, -1, 0);
rep(i, N) W[i] = 1;
int s = 0;
for (P p : bridges) {
int u = p._1, v = p._2;
W[u] += sz[v];
W[v] += N-sz[v];
s = (s + 1LL*sz[v]*(N-sz[v])) % MOD;
}
rep(i, N) U[i] = i, R[i] = 1;
for (P p : els) unite(p._1, p._2);
rep(i, N) G2[find(i)].pb(i);
rep(c, N) {
vector<P> xs, ys;
for (int x : G2[c]) if (G2[c].size() >= 2) {
assert(G2[c].size() >= 4);
/*
int w = 1;
for (int t : G[x]) if (find(t) != c) {
if (par[x] == t) w += N-sz[x];
else w += sz[t];
}
*/
xs.pb(P(X[x], W[x]));
ys.pb(P(Y[x], W[x]));
}
s = (s+count(xs)) % MOD;
s = (s+count(ys)) % MOD;
}
return s;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
9584 KB |
Output is correct |
2 |
Correct |
0 ms |
9584 KB |
Output is correct |
3 |
Correct |
0 ms |
9584 KB |
Output is correct |
4 |
Correct |
0 ms |
9584 KB |
Output is correct |
5 |
Incorrect |
0 ms |
9584 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
9716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
13924 KB |
Output is correct |
2 |
Correct |
43 ms |
14112 KB |
Output is correct |
3 |
Correct |
133 ms |
19172 KB |
Output is correct |
4 |
Correct |
126 ms |
19784 KB |
Output is correct |
5 |
Correct |
323 ms |
30280 KB |
Output is correct |
6 |
Correct |
393 ms |
29604 KB |
Output is correct |
7 |
Correct |
396 ms |
28680 KB |
Output is correct |
8 |
Correct |
349 ms |
29764 KB |
Output is correct |
9 |
Correct |
383 ms |
30588 KB |
Output is correct |
10 |
Correct |
299 ms |
33400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
12288 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |