Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ss second
  6. #define ff first
  7. #define p_b push_back
  8. #define endl "\n"
  9.  
  10. typedef long long ll;
  11. typedef long double ld;
  12.  
  13. const ll INF = 2e18;
  14. const ll INFINT = 2e9;
  15. const int N = 1000006;
  16. const int NN = 1006;
  17.  
  18. ll ans = 0;
  19. int n;
  20. int CR = 10;
  21. int sz[N];
  22. bool wasCentroid[N];
  23. int startbal, last[2][N];
  24. vector < int > g[N];
  25. string s;
  26. vector < int > kek[2];
  27.  
  28. void Calc_Size (int v, int p = -1) {
  29. sz[v] = 1;
  30. for (int to : g[v]) {
  31. if (to == p || wasCentroid[to]) continue;
  32. Calc_Size(to, v);
  33. sz[v] += sz[to];
  34. }
  35. }
  36.  
  37. int Find_Centroid (int v, int p = -1, int SZ = -1) {
  38. if (p == -1) SZ = sz[v];
  39. for (int to : g[v]) {
  40. if (to == p || wasCentroid[to]) continue;
  41. if (sz[to] < SZ / 2) continue;
  42. return Find_Centroid(to, v, SZ);
  43. }
  44. return v;
  45. }
  46.  
  47. void dfs_fnd_pre (int v, int p, int bal) {
  48. //cerr << "FND PRE " << bal << endl;
  49. if (s[v] == '(') bal++;
  50. else bal--;
  51. if (bal >= 0 && bal + startbal >= 0 && s[v] == '(') {
  52. // cout << "PRE " << v << ' ' << bal << ' ' << kek[1][bal + startbal] << endl;
  53. if (last[1][bal + startbal] != CR) {
  54. last[1][bal + startbal] = CR;
  55. kek[1][bal + startbal] = 0;
  56. }
  57. // cout << "PREF " << v + 1 << ' ' << bal << ' ' << kek[1][bal + startbal] << endl;
  58. ans += kek[1][bal + startbal];
  59. }
  60. for (int to : g[v]) {
  61. if (to == p || wasCentroid[to]) continue;
  62. dfs_fnd_pre(to, v, bal);
  63. }
  64. }
  65.  
  66. void dfs_fnd_suff (int v, int p, int bal) {
  67. //cerr << "FND SUFF " << bal << endl;
  68. if (s[v] == '(') bal++;
  69. else bal--;
  70. if (bal <= 0 && bal + startbal <= 0 && s[v] == ')') {
  71. if (last[0][-(bal + startbal)] != CR) {
  72. last[0][-(bal + startbal)] = CR;
  73. kek[0][-(bal + startbal)] = 0;
  74. }
  75. // cout << "SUFF " << v << ' ' << bal << ' ' << kek[0][bal + startbal] << endl;
  76. // cout << "SUFF " << v + 1 << ' ' << bal << ' ' << kek[0][-(bal + startbal)] << endl;
  77. ans += kek[0][-(bal + startbal)];
  78. }
  79. for (int to : g[v]) {
  80. if (to == p || wasCentroid[to]) continue;
  81. dfs_fnd_suff(to, v, bal);
  82. }
  83. }
  84.  
  85. void dfs_pls_pre (int v, int p, int bal) {
  86. //cerr << "PLS PRE " << bal << endl;
  87. if (s[v] == '(') bal++;
  88. else bal--;
  89. if (bal + startbal >= 0 && s[v] == '(') {
  90. if (last[0][bal] != CR) {
  91. last[0][bal] = CR;
  92. kek[0][bal] = 0;
  93. }
  94. kek[0][bal]++;
  95. }
  96. for (int to : g[v]) {
  97. if (to == p || wasCentroid[to]) continue;
  98. dfs_pls_pre(to, v, bal);
  99. }
  100. }
  101.  
  102. void dfs_pls_suff (int v, int p, int bal) {
  103. //cerr << "PLS SUFF " << bal << endl;
  104. if (s[v] == '(') bal++;
  105. else bal--;
  106. if (bal + startbal <= 0 && s[v] == ')') {
  107. if (last[1][-bal] != CR) {
  108. last[1][-bal] = CR;
  109. kek[1][-bal] = 0;
  110. }
  111. kek[1][-bal]++;
  112. }
  113. for (int to : g[v]) {
  114. if (to == p || wasCentroid[to]) continue;
  115. dfs_pls_suff(to, v, bal);
  116. }
  117. }
  118.  
  119. void go (int v) {
  120. Calc_Size(v);
  121. v = Find_Centroid(v);
  122. CR++;
  123. //cout << "CENTR " << 1 + v << endl;
  124. //cerr << v << endl;//
  125.  
  126. //cout << "CENTROID " << v << endl;
  127.  
  128.  
  129. //kek[0].assign (kek[0].size(), 0);
  130. //kek[1].assign (kek[1].size(), 0);
  131. kek[0][0] = kek[1][0] = 1;
  132. last[0][0] = last[1][0] = CR;
  133.  
  134. if (s[v] == '(') startbal = 1;
  135. else startbal = -1;
  136.  
  137. for (int to : g[v]) {
  138. //cerr << to << endl;
  139. if (wasCentroid[to]) continue;
  140.  
  141. dfs_fnd_pre(to, v, 0);
  142. dfs_fnd_suff(to, v, 0);
  143.  
  144. dfs_pls_pre(to, v, 0);
  145. dfs_pls_suff(to, v, 0);
  146. //cout << to + 1 << endl;
  147. // cout << ans << endl;
  148. //for (int i = 0; i < n; i++) cout << kek[0][i] << ' ' << kek[1][i] << endl;
  149. // cout << endl;
  150. }
  151. //exit(0);
  152. wasCentroid[v] = 1;
  153. for (int i = 0; i < g[v].size(); i++) {
  154. if (wasCentroid[g[v][i]]) continue;
  155. go(g[v][i]);
  156. }
  157. }
  158.  
  159. int main () {
  160. ios_base::sync_with_stdio(0);
  161. #ifdef LOCAL
  162. freopen ("input.txt", "r", stdin);
  163. freopen ("output.txt", "w", stdout);
  164. #else
  165. //freopen ("input.txt", "r", stdin);freopen ("output.txt", "w", stdout);
  166. //freopen ("dynamic.in", "r", stdin);freopen ("dynamic.out", "w", stdout);
  167. #endif // LOCAL
  168. cin >> n;
  169. cin >> s;
  170. for (int i = 0; i < n - 1; i++) {
  171. int x, y;
  172. cin >> x >> y;
  173. x--, y--;
  174. g[x].p_b(y);
  175. g[y].p_b(x);
  176. }
  177. kek[0].resize(n + 10);
  178. kek[1].resize(n + 10);
  179. go(0);
  180. cout << ans << endl;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement