artemgf

Теракты на нефтепроводах

Nov 7th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <string>
  5. #include <set>
  6. #include <map>
  7. #include <algorithm>
  8. #include <string>
  9. #include <math.h>
  10. #include <vector>
  11. #include <cmath>
  12. #include <sstream>
  13. #include <queue>
  14.  
  15. using namespace std;
  16.  
  17. const int MAX = 50001;
  18. vector<int> lst[MAX];
  19. int parent[MAX];
  20.  
  21. void make_set(int v) {
  22.     lst[v] = vector<int>(1, v);
  23.     parent[v] = v;
  24. }
  25.  
  26. int find_set(int v) {
  27.     return parent[v];
  28. }
  29.  
  30. void union_sets(int a, int b) {
  31.     a = find_set(a);
  32.     b = find_set(b);
  33.     if (a != b) {
  34.         if (lst[a].size() < lst[b].size())
  35.             swap(a, b);
  36.         while (!lst[b].empty()) {
  37.             int v = lst[b].back();
  38.             lst[b].pop_back();
  39.             parent[v] = a;
  40.             lst[a].push_back(v);
  41.         }
  42.     }
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48.     vector<pair<pair<long long,long long>, pair<long long, long long>>> ter;
  49.     long long n, m, s;
  50.     cin >> n >> m >> s;
  51.  
  52.     long long a, b, si;
  53.     for (int i = 0; i < m; i++)
  54.     {
  55.         cin >> a >> b >> si;
  56.         ter.push_back({ {si,i+1},{a,b} });
  57.     }
  58.    
  59.     sort(ter.rbegin(), ter.rend());
  60.      
  61.     for (int i = 1; i <= n; i++)
  62.         make_set(i);
  63.  
  64.     vector<pair<int, int>> used;
  65.     for (int i = 0; i < m; i++)
  66.     {
  67.         long long prom1 = find_set(ter[i].second.first);
  68.         long long prom2 = find_set(ter[i].second.second);
  69.         if (prom1==prom2)
  70.         {
  71.             used.push_back(ter[i].first);
  72.         }
  73.         else
  74.         {
  75.             union_sets(prom1, prom2);
  76.         }
  77.     }
  78.  
  79.     vector<int>bax;
  80.     for (int i = used.size()-1; i >=0; i--)
  81.     {
  82.         if (used[i].first <= s)
  83.         {
  84.             bax.push_back(used[i].second);
  85.             s -= used[i].first;
  86.         }
  87.     }
  88.  
  89.     cout << bax.size() << endl;
  90.     for (int i = 0; i < bax.size(); i++)
  91.         cout << bax[i] << " ";
  92.     system("pause");
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment