#include <bits/stdc++.h> using namespace std; #define int long long #define ls(p) (p) << 1 #define rs(p) (p) << 1 | 1 inline int rd() { int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();} return x*f; } const int N = 3e5 + 5; const int P = 1e9 + 7;//998244353 const int INF = 1e18; int n, f[N], ans = 0; vector <int> G[N]; void solve () { cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } for (int u = 1; u <= n; u++) { for (auto v : G[u]) if (u > v) f[u] += f[v] + 1; for (auto v : G[u]) if (u > v) ans += (f[u] - f[v] - 1) * (f[v] + 1); } cout << ans; } signed main () { // freopen("ride.in", "r", stdin); // freopen("ride.out", "w", stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T = 1; // cin >> T; while (T--) solve (); return 0; }
Note.ms
/liuhaosenday1T2