fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int t;
  9. cin >> t;
  10.  
  11. while (t--) {
  12. int n;
  13. cin >> n;
  14.  
  15. vector<vector<int>> edges(n + 1);
  16. for (int i = 0; i < n - 1; i++) {
  17. int u, v;
  18. cin >> u >> v;
  19. edges[u].push_back(v);
  20. edges[v].push_back(u);
  21. }
  22.  
  23. vector<int> ks(n + 1);
  24. long long ans = 0;
  25.  
  26. for (int x = 1; x <= n; x++) {
  27. if (edges[x].size() == 1) {
  28. ans += n - 2;
  29. int y = edges[x][0];
  30. ks[y]++;
  31. }
  32. }
  33.  
  34. for (int x = 0; x <= n; x++) {
  35. ans -= 1LL * ks[x] * (ks[x] - 1) / 2;
  36. if (edges[x].size() == 2 && ks[x] == 0) ans++;
  37. }
  38.  
  39. cout << 1LL * n * (n - 1) * (n - 2) / 6 - ans << '\n';
  40. }
  41. }
  42.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
0
0