Advertisement
pdpd123

Problem 8

Feb 17th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.84 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. // nichijou
  4. #define REP(i,a,b) for (int i(a); i < (b); ++i)
  5. #define RP(i,n) REP(i,0,n)
  6. #define PER(i,a,b) for(int i((a)-1); i >= (b); --i)
  7. #define PR(i,n) PER(i,n,0)
  8. #define REP1(i,a,b) REP(i,a,(b)+1)
  9. #define RP1(i,n) REP1(i,1,n)
  10. #define PER1(i,a,b) PER(i,(a)+1,b)
  11. #define PR1(i,n) PER1(i,n,1)
  12. #define DO(n) RP(__i,n)
  13. template<class T,class U>
  14. bool cmax(T & a, const U & b) {return a < b ? a = b, 1 : 0;}
  15. template<class T,class U>
  16. bool cmin(T & a, const U & b) {return b < a ? a = b, 1 : 0;}
  17.  
  18. // data type
  19. typedef long long ll;
  20. typedef pair<int,int> pii;
  21. typedef pair<ll,ll> pll;
  22. #define F first
  23. #define S second
  24.  
  25. // STL container
  26. typedef vector<int> vi;
  27. typedef vector<ll> vll;
  28. #define SZ(a) ((int)(a).size())
  29. #define ALL(a) begin(a), end(a)
  30. #define CLR(a) (a).clear()
  31. #define BK(a) ((a).back())
  32. #define FT(a) ((a).front())
  33. #define DB(a) (a).pop_back()
  34. #define DF(a) (a).pop_front()
  35. #define PB push_back
  36. #define EB emplace_back
  37.  
  38. /* I gave you my heart and then you turned around. */
  39. void _BG(const char * s) {cerr<<s<<endl;}
  40. template<class T, class ... TT>
  41. void _BG(const char * s,T a, TT...b)
  42. {
  43.     for (size_t c = 0; *s && (c || *s != ','); cerr << *s++)
  44.         c += count(ALL("([{"),*s) - count(ALL(")]}"),*s);
  45.     cerr << " = " << a;
  46.     if (*s) {
  47.         cerr << ", ";
  48.         ++s;
  49.     }
  50.     _BG(s,b...);
  51. }
  52. #ifdef PR3PONY
  53. #define BG(...) do { \
  54.     cerr << __PRETTY_FUNCTION__ << ':' << __LINE__ << ": "; \
  55.     _BG(#__VA_ARGS__,__VA_ARGS__); \
  56. } while(0)
  57. #else
  58. #define BG(...)
  59. #endif
  60.  
  61. /* Reading input is now 20% cooler! */
  62. bool RD() {return 1;}
  63. bool RD(char & a) {return scanf(" %c", &a) == 1;}
  64. bool RD(char * a) {return scanf("%s", a) == 1;}
  65. bool RD(double & a) {return scanf("%lf", &a) == 1;}
  66. bool RD(int & a) {return scanf("%d", &a) == 1;}
  67. bool RD(ll & a) {return scanf("%lld", &a) == 1;}
  68.  
  69. template<class T, class ... TT>
  70. bool RD(T & a, TT & ... b) {return RD(a) && RD(b...);}
  71.  
  72. /* Do princesses dream of magic sheep? */
  73. #define DR(T,...) T __VA_ARGS__; RD(__VA_ARGS__)
  74. #define RI(...) DR(int,__VA_ARGS__)
  75.  
  76. /* For it's time for you to fulfill your output. */
  77. void PT(const char & a) {putchar(a);}
  78. void PT(char const * const & a) {fputs(a, stdout);}
  79. void PT(const double & a) {printf("%.16f", a);}
  80. void PT(const int & a) {printf("%d", a);}
  81. void PT(const ll & a) {printf("%lld", a);}
  82.  
  83. /* The line will last forever! */
  84. template<char s = ' ', char e = '\n'>
  85. void PL() {if (e) PT(e);}
  86. template<char s = ' ', char e = '\n', class T, class ... TT>
  87. void PL(const T & a, const TT & ... b)
  88. {PT(a); if (sizeof...(b) && s) PT(s); PL<s,e>(b...);}
  89.  
  90. /* Good Luck && Have Fun ! */
  91. const int N = 2e5 + 98;
  92. vector<int> g[N];
  93. int sz[N];
  94. void getsz(int u,int p)
  95. {
  96.     sz[u] = 1;
  97.     for (int v : g[u])
  98.         if (v != p) {
  99.             getsz(v,u);
  100.             sz[u] += sz[v];
  101.         }
  102. }
  103. int fct(int n,int u,int p)
  104. {
  105.     for (int v : g[u])
  106.         if (v != p && sz[v] > n/2)
  107.             return fct(n,v,u);
  108.     return u;
  109. }
  110. ll to[2],cn[2],nu[2],su[2];
  111. void dfs(int u,int p,int d,bool fi=true)
  112. {
  113.     if (fi) {
  114.         for (int i = 0; i < 2; ++i) {
  115.             cn[(d + i) & 1] += nu[i];
  116.             to[(d + i) & 1] += su[i] + nu[i] * d;
  117.         }
  118.     } else {
  119.         ++nu[d&1];
  120.         su[d&1] += d;
  121.     }
  122.     for (int v : g[u])
  123.         if (v != p)
  124.             dfs(v,u,d+1,fi);
  125. }
  126. void DC(int u)
  127. {
  128.     getsz(u,u);
  129.     int n = sz[u];
  130.     u = fct(n,u,u);
  131.     fill_n(nu,2,0);
  132.     fill_n(su,2,0);
  133.     ++nu[0];
  134.     for (int v : g[u]) {
  135.         dfs(v,u,1);
  136.         dfs(v,u,1,false);
  137.     }
  138.     for (int v : g[u]) {
  139.         g[v].erase(find(ALL(g[v]),u));
  140.         DC(v);
  141.     }
  142. }
  143. int main()
  144. {
  145.     RI(n);
  146.     RP(i,n-1) {
  147.         RI(u,v);
  148.         --u,--v;
  149.         g[u].PB(v);
  150.         g[v].PB(u);
  151.     }
  152.     DC(0);
  153.     PL((to[0] + to[1] - cn[1]) / 2 + cn[1]);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement