fork(1) download
  1. #include <cstdio>
  2. #include <vector>
  3.  
  4. static const int N = 4000;
  5.  
  6. int n;
  7. std::vector<int> g[N];
  8. int cur;
  9. int fs[2][N * 2], f[2][N * 2];
  10. int ans[N];
  11.  
  12. void dfs(int a, int p, int x, int y) {
  13. y += a < cur ? -1 : 1;
  14.  
  15. f[x][n + y]++;
  16.  
  17. for (std::size_t i = 0; i < g[a].size(); i++) {
  18. int b = g[a][i];
  19.  
  20. if (b == p)
  21. continue;
  22.  
  23. dfs(b, a, 1 - x, y);
  24. }
  25. }
  26.  
  27. void solve(int a) {
  28. cur = a;
  29.  
  30. for (int i = 0; i < 2; i++)
  31. for (int j = 0; j < 2 * n; j++)
  32. fs[i][j] = 0;
  33.  
  34. for (std::size_t i = 0; i < g[a].size(); i++)
  35. {
  36. for (int j = 0; j < 2; j++)
  37. for (int k = 0; k < 2 * n; k++)
  38. f[j][k] = 0;
  39.  
  40. dfs(g[a][i], a, 1, 0);
  41.  
  42. for (int j = 0; j < 2; j++)
  43. for (int k = 0; k < 2 * n; k++)
  44. ans[cur] += fs[j][k] * f[j][2 * n - k] * 2;
  45.  
  46. ans[cur] += f[0][n] * 2;
  47.  
  48. for (int j = 0; j < 2; j++)
  49. for (int k = 0; k < 2 * n; k++)
  50. fs[j][k] += f[j][k];
  51. }
  52. }
  53.  
  54. int main() {
  55. freopen("input.txt", "rt", stdin);
  56. freopen("output.txt", "wt", stdout);
  57.  
  58. scanf("%d", &n);
  59.  
  60. for (int i = 1; i < n; i++) {
  61. int x, y;
  62. scanf("%d%d", &x, &y);
  63. x--, y--;
  64. g[x].push_back(y);
  65. g[y].push_back(x);
  66. }
  67.  
  68. for (int i = 0; i < n; i++)
  69. solve(i);
  70.  
  71. for (int i = 0; i < n; i++)
  72. printf("%d\n", ans[i]);
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0s 3652KB
stdin
Standard input is empty
stdout
Standard output is empty