-
Notifications
You must be signed in to change notification settings - Fork 3
/
D.cpp
85 lines (64 loc) · 1.68 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <int,pii> piii;
#define forr(_a,_b,_c) for(int _a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
const int N = 5e5 + 5;
const ll oo = 1e9;
const ll mod = 1e9 + 7; // 998244353;
vector <int> a[N];
ll res, sz[N], n;
void dfs (int u, int p){
sz[u] = 1;
ll x = 0, z = 0;
for (int v : a[u])
if (v != p){
dfs(v, u);
res += z * sz[v] * (sz[v] - 1) / 2;
x += sz[u] * sz[v];
z += sz[v] * (sz[v] - 1) / 2;
sz[u] += sz[v];
}
reverse(all(a[u]));
ll tmp = sz[u];
sz[u] = 1;
for (int v : a[u])
if (v != p){
res += (x - sz[v] * (tmp - sz[v])) * sz[v] * (sz[v] - 1) / 2;
res += (tmp - sz[v]) * (n - tmp) * sz[v] * (sz[v] - 1) / 2;
sz[u] += sz[v];
}
// cout << u << " " << sz[u] << " " << res << "\n";
}
ll u, v;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef hqm
freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
#endif
cin >> n;
forf (i, 1, n){
cin >> u >> v;
a[u].pb(v);
a[v].pb(u);
}
dfs(1, 1);
cout << res * 2;
return 0;
}
/*
*/