Guest User

Untitled

a guest
Apr 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <unordered_map>
  7. #include <set>
  8. #include <utility>
  9. using namespace std;
  10.  
  11. #define M 1000000007
  12.  
  13. struct DisjointUnionSets {
  14. vector<int> rank;
  15. vector<int> parent;
  16.  
  17. DisjointUnionSets(int n) {
  18. rank.resize(n);
  19. parent.resize(n);;
  20. makeSet();
  21. }
  22.  
  23. void makeSet() {
  24. for (int i=0; i<parent.size(); i++) {
  25. parent[i] = i;
  26. }
  27. }
  28.  
  29. int find(int x) {
  30. if (parent[x]!=x) {
  31. parent[x] = find(parent[x]);
  32. }
  33.  
  34. return parent[x];
  35. }
  36.  
  37. void union_(int x, int y) {
  38. int xRoot = find(x);
  39. int yRoot = find(y);
  40.  
  41. if (xRoot == yRoot) {
  42. return;
  43. }
  44.  
  45. if (rank[xRoot] < rank[yRoot]) {
  46. parent[xRoot] = yRoot;
  47. } else if (rank[yRoot] < rank[xRoot]) {
  48. parent[yRoot] = xRoot;
  49. } else {
  50. parent[yRoot] = xRoot;
  51. rank[xRoot] = rank[xRoot] + 1;
  52. }
  53. }
  54. };
  55.  
  56. struct Node {
  57. int id;
  58. int parent{-1};
  59. int cnt{0};
  60. vector<int> children;
  61. };
  62.  
  63. vector<Node> nodes;
  64. set<pair<int,int>> red_edge_set;
  65.  
  66. bool is_red(int i, int j) {
  67. auto it = red_edge_set.find(make_pair(min(i,j), max(i,j)));
  68.  
  69. return it != red_edge_set.end();
  70. }
  71.  
  72. int build_tree(int n) {
  73. nodes.reserve(n);
  74.  
  75. for (int i = 0; i < n; i++) {
  76. Node node;
  77. node.id = i;
  78. nodes.push_back(node);
  79. }
  80.  
  81. for (int i = 0; i < n-1; i++) {
  82. int a, b;
  83. char c;
  84. cin >> a >> b >> c;
  85.  
  86. int xid = a - 1;
  87. int yid = b - 1;
  88. if (c == 'r') {
  89. red_edge_set.insert(make_pair(min(xid,yid), max(xid,yid)));
  90. }
  91.  
  92. if (nodes[yid].parent != -1) {
  93. vector<int> stack;
  94.  
  95. int p = yid;
  96. while (p != -1) {
  97. stack.push_back(p);
  98. p = nodes[p].parent;
  99. }
  100.  
  101. while (stack.size() > 1) {
  102. p = stack.back();
  103. stack.pop_back();
  104. nodes[p].parent = stack.back();
  105. }
  106.  
  107. }
  108.  
  109. nodes[yid].parent = xid;
  110. }
  111.  
  112. int root = -1;
  113. for (auto &n : nodes) {
  114. if (n.parent != -1) {
  115. nodes[n.parent].children.push_back(n.id);
  116. } else {
  117. root = n.id;
  118. }
  119. }
  120.  
  121. return root;
  122. }
  123.  
  124. int count(int root) {
  125. int c = 1;
  126.  
  127. for (auto &child : nodes[root].children) {
  128. c += count(child);
  129. }
  130.  
  131. nodes[root].cnt = c;
  132. return c;
  133. }
  134.  
  135.  
  136. int main() {
  137. int n;
  138. cin >> n;
  139.  
  140. auto root = build_tree(n);
  141. count(root);
  142.  
  143. long result = 0;
  144. cout << result << endl;
  145. }
Add Comment
Please, Sign In to add comment