Advertisement
nicuvlad76

Untitled

Jan 16th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define Nmax 1000005
  5.  
  6. using namespace std;
  7.  
  8. class InParser {
  9. private:
  10. FILE *fin;
  11. char *buff;
  12. int sp;
  13.  
  14. char read_ch() {
  15. ++sp;
  16. if (sp == 4096) {
  17. sp = 0;
  18. fread(buff, 1, 4096, fin);
  19. }
  20. return buff[sp];
  21. }
  22.  
  23. public:
  24. InParser(const char* nume) {
  25. fin = fopen(nume, "r");
  26. buff = new char[4096]();
  27. sp = 4095;
  28. }
  29.  
  30. InParser& operator >> (int &n) {
  31. char c;
  32. while (!isdigit(c = read_ch()) && c != '-');
  33. int sgn = 1;
  34. if (c == '-') {
  35. n = 0;
  36. sgn = -1;
  37. } else {
  38. n = c - '0';
  39. }
  40. while (isdigit(c = read_ch())) {
  41. n = 10 * n + c - '0';
  42. }
  43. n *= sgn;
  44. return *this;
  45. }
  46.  
  47. InParser& operator >> (long long &n) {
  48. char c;
  49. n = 0;
  50. while (!isdigit(c = read_ch()) && c != '-');
  51. long long sgn = 1;
  52. if (c == '-') {
  53. n = 0;
  54. sgn = -1;
  55. } else {
  56. n = c - '0';
  57. }
  58. while (isdigit(c = read_ch())) {
  59. n = 10 * n + c - '0';
  60. }
  61. n *= sgn;
  62. return *this;
  63. }
  64. };
  65.  
  66. class OutParser {
  67. private:
  68. FILE *fout;
  69. char *buff;
  70. int sp;
  71.  
  72. void write_ch(char ch) {
  73. if (sp == 50000) {
  74. fwrite(buff, 1, 50000, fout);
  75. sp = 0;
  76. buff[sp++] = ch;
  77. } else {
  78. buff[sp++] = ch;
  79. }
  80. }
  81.  
  82.  
  83. public:
  84. OutParser(const char* name) {
  85. fout = fopen(name, "w");
  86. buff = new char[50000]();
  87. sp = 0;
  88. }
  89. ~OutParser() {
  90. fwrite(buff, 1, sp, fout);
  91. fclose(fout);
  92. }
  93.  
  94. OutParser& operator << (int vu32) {
  95. if (vu32 <= 9) {
  96. write_ch(vu32 + '0');
  97. } else {
  98. (*this) << (vu32 / 10);
  99. write_ch(vu32 % 10 + '0');
  100. }
  101. return *this;
  102. }
  103.  
  104. OutParser& operator << (long long vu64) {
  105. if (vu64 <= 9) {
  106. write_ch(vu64 + '0');
  107. } else {
  108. (*this) << (vu64 / 10);
  109. write_ch(vu64 % 10 + '0');
  110. }
  111. return *this;
  112. }
  113.  
  114. OutParser& operator << (char ch) {
  115. write_ch(ch);
  116. return *this;
  117. }
  118. OutParser& operator << (const char *ch) {
  119. while (*ch) {
  120. write_ch(*ch);
  121. ++ch;
  122. }
  123. return *this;
  124. }
  125. };
  126.  
  127. InParser fin("episodul1.in");
  128. OutParser fout("episodul1.out");
  129.  
  130. int M, N;
  131. int t;
  132. int T[Nmax];
  133. int L[Nmax];
  134. ll APM[Nmax];
  135.  
  136. int find(int node)
  137. {
  138. if(node != T[node])
  139. T[node] = find(T[node]);
  140. return T[node];
  141. }
  142.  
  143. void unite(int a, int b, int cost)
  144. {
  145. if(L[a] < L[b])
  146. swap(a, b);
  147. T[b] = a;
  148. if(L[a] == L[b])
  149. L[a]++;
  150. APM[a] += APM[b] + cost;
  151. }
  152.  
  153. void insertEdge(int x, int y, int c)
  154. {
  155. int tx = find(x);
  156. int ty = find(y);
  157. if(tx != ty)
  158. unite(tx, ty, c);
  159. }
  160.  
  161. int main()
  162. {
  163. for(fin >> M; M; M--)
  164. {
  165. int t;
  166. fin >> t;
  167. if(t == 1)
  168. {
  169. ++N;
  170. T[N] = N;
  171. L[N] = 1;
  172. APM[N] = 0;
  173. }
  174. if(t == 2)
  175. {
  176. int x, y, c;
  177. fin >> x >> y >> c;
  178. insertEdge(x, y, c);
  179. }
  180. if(t == 3)
  181. {
  182. int x;
  183. fin >> x;
  184. fout << APM[find(x)] << "\n";
  185. }
  186. }
  187. return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement