#include <bits/stdc++.h>
#define vi vector<int>
#define ve vector
#define ll long long
#define vf vector<float>
#define vll vector<pair<ll,ll>>
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 2e9+5
#define eps 1e-7
#define eps1 1e-25
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
#define V 450
using namespace std;
const int MOD = 1e9;
const int nax = 1e5+5;
int mnx = INF,mny = INF, mxx = 0, mxy = 0, nb;
int d[nax], ans[nax], sz[nax], sm[nax];
bool vis[nax];
vi adj[nax], tree[nax];
ve<pair<ii,int>> ed1, ed2;
void dfs(int u, int p, int dep){
sm[u] = dep;
sz[u] = 1;
for(auto x : tree[u]){
if(x == p) continue;
dfs(x,u,dep+1);
sz[u] += sz[x];
sm[u] += sm[x];
}
}
void dfs2(int u, int p){
for(auto x : tree[u]){
if(x == p) continue;
ans[x] = ans[u]-sz[x] + nb - sz[x];
dfs2(x,u);
}
}
int DistanceSum(int n, int *x, int *y) {
nb = n;
for (int i = 0; i < n; ++i)
{
mnx = min(mnx,x[i]), mny = min(mny,y[i]);
mxx = max(mxx,x[i]), mxy = max(mxy, y[i]);
}
mxx-=mnx,mxy-=mny;
mxx++,mxy++;
for (int i = 0; i < n; ++i)
{
x[i]-=mnx,y[i]-=mny;
ed1.pb({{x[i],y[i]},i});
ed2.pb({{y[i],x[i]},i});
}
sort(ed1.begin(), ed1.end());
sort(ed2.begin(), ed2.end());
for (int i = 1; i < n; ++i)
{
if(ed1[i].fi.fi == ed1[i-1].fi.fi && ed1[i].fi.se - ed1[i-1].fi.se == 1){
adj[ed1[i].se].pb(ed1[i-1].se);
adj[ed1[i-1].se].pb(ed1[i].se);
}
if(ed2[i].fi.fi == ed2[i-1].fi.fi&& ed2[i].fi.se - ed2[i-1].fi.se == 1){
adj[ed2[i].se].pb(ed2[i-1].se);
adj[ed2[i-1].se].pb(ed2[i].se);
}
}
queue<int> q;
q.push(0);
memset(d,0,sizeof d);
vis[0] = 1;
int cnt = 0;
while(!q.empty()){
int cur = q.front();
q.pop();
for (auto j : adj[cur])
{
if(d[j] || vis[j]) continue;
d[j] = d[cur] + 1;
tree[j].pb(cur);
tree[cur].pb(j);
q.push(j);
}
cnt++;
}
dfs(0,0,0);
ll res = 0;
ans[0] = sm[0];
dfs2(0,0);
for (int i = 0; i < n; ++i)
{
res += ans[i];
res %= MOD;
}
return res/2;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
5332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
7588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
7576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |