SHARE
TWEET

Untitled

a guest Oct 15th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8.  
  9. int n, a, b, dp[200000], ds[200000], a[200000];
  10. vector<int> g[200000];
  11.  
  12. int c(int p, int pr) {
  13.     dp[p] = 1;
  14.     for(int i: g[p])
  15.         if(pr != i)
  16.             dp[p] += c(i, p);
  17. }
  18.  
  19. void f(int p, int pr) {
  20.     for(int i: g[p])
  21.         if(pr != i) {
  22.             ds[i] += ds[p] + 1;
  23.             f(i, p);
  24.         }
  25. }
  26.  
  27. void dfs(int p, int pr) {
  28.     for(int i: g[p])
  29.         if(pr != i) {
  30.             a = (sum = sum - dp[i] + (n - dp[i]));
  31.         }
  32. }
  33.  
  34. int main()
  35. {
  36.     cin >> n;
  37.     for(int i = 0; i < n - 1; ++i)
  38.         cin >> a >> b, g[a - 1].push_back(b - 1), g[b - 1].push_back(a - 1);
  39.     ds[0] = 0;
  40.     c(0, -1);
  41.     f(0, -1);
  42.     int sum = 0;
  43.     for(int i = 0; i < n; ++i)
  44.         sum += ds[i];
  45.     cout << sum << " ";
  46.     for(int i: g[0])
  47.         dfs(i, 0);
  48.     for(int i = 1; i < n; ++i)
  49.         cout << (sum = sum - dp[i] + (n - dp[i])) << " ";
  50.     cout << endl;
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top