Raslboyy

1376

Nov 21st, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <algorithm>
  5. #include <string>
  6. #include <math.h>
  7. #include <climits>
  8.  
  9. //#include <chrono>
  10. //#include <fstream>
  11. //#include <bits/stdc++.h>
  12.  
  13. #define mp make_pair
  14. #define pb push_back
  15. #define eb emplace_back
  16. #define x first
  17. #define y second
  18. #define sz(x) (int)x.size()
  19. #define all(x) begin(x), end(x)
  20. #define rall(x) rbegin(x), rend(x)
  21. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  22. #define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
  23.  
  24. using namespace std;
  25.  
  26. typedef unsigned long long ull;
  27. typedef long long ll;
  28. typedef long double ld;
  29. typedef pair<int, int> pi;
  30. typedef pair<ll, ll> pl;
  31. typedef vector<int> veci;
  32. typedef vector<bool> vecb;
  33. typedef vector<vector<int>> vvi;
  34. typedef vector<vector<bool>> vvb;
  35.  
  36. const int INF_I = 1e9;
  37. const ll INF_LL = 1e18;
  38.  
  39. int n;
  40. veci p, sz, cost;
  41.  
  42. int find_set(int v) {
  43.     return (v == p[v] ? v : p[v] = find_set(p[v]));
  44. }
  45.  
  46. void union_set(int a, int b, int w) {
  47.     a = find_set(a);
  48.     b = find_set(b);
  49.     if (sz[a] < sz[b])
  50.         swap(a, b);
  51.     if (a != b) {
  52.         p[b] = a;
  53.         sz[a] += sz[b];
  54.         cost[a] += w + cost[b];
  55.     }
  56.     else
  57.         cost[a] += w;
  58.    
  59. }
  60.  
  61. int get_sum(int a) {
  62.     return cost[find_set(a)];
  63. }
  64.  
  65. int main() {
  66.     /*ifstream cin("in.txt");
  67.     ofstream cout("out.txt");
  68.     auto start_time = chrono::high_resolution_clock::now();*/
  69.  
  70.     ios::sync_with_stdio(false);
  71.     cin.tie(0);
  72.     cout.tie(0);
  73.  
  74.     int m;
  75.     cin >> n >> m;
  76.     p.assign(n, 0);
  77.     sz.assign(n, 1);
  78.     cost.assign(n, 0);
  79.     FOR(i, 0, n)
  80.         p[i] = i;
  81.     for (int i = 0; i < m; i++) {
  82.         int cl;
  83.         cin >> cl;
  84.         if (cl == 1) {
  85.             int a, b, w;
  86.             cin >> a >> b >> w;
  87.             union_set(--a, --b, w);
  88.         }
  89.         else {
  90.             int a;
  91.             cin >> a;
  92.             cout << get_sum(--a) << "\n";
  93.         }
  94.     }
  95.    
  96.  
  97.    
  98.     /*auto end_time = chrono::high_resolution_clock::now();
  99.     chrono::duration<double> duration = end_time - start_time;
  100.     cout << duration.count() << "\n";
  101.     cin.close();
  102.     cout.close();*/
  103.     return 0;
  104. }
Add Comment
Please, Sign In to add comment