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
| #include <bits/stdc++.h>
#define ll long long using namespace std;
const int N = 5005; int n; ll num[N], f[N], g[N], maxd, ans;
struct node { int to, nxt; } e[N << 1]; int head[N], tmp;
void add(int x, int y) { e[++tmp].to = y; e[tmp].nxt = head[x]; head[x] = tmp; }
void dfs(int x, int fa, ll dep) { maxd = max(maxd, dep); num[dep]++; for (int i = head[x]; i; i = e[i].nxt) { int v = e[i].to; if (v == fa) continue; dfs(v, x, dep + 1); } }
void sol() { for (int i = 1; i <= n; i++) { memset(g, 0, sizeof(g)); memset(f, 0, sizeof(f)); for (int j = head[i]; j; j = e[j].nxt) { int v = e[j].to; maxd = 0; memset(num, 0, sizeof(num)); dfs(v, i, 1); for (int k = 1; k <= maxd; k++) { ans += f[k] * num[k]; f[k] += g[k] * num[k]; g[k] += num[k]; } } } }
int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1, x, y; i < n; i++) { cin >> x >> y; add(x, y); add(y, x); } sol(); cout << ans << endl; return 0; }
|