Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <conio.h>
- #include <string>
- #include <set>
- #include <map>
- #include <algorithm>
- #include <string>
- #include <math.h>
- #include <vector>
- #include <cmath>
- #include <sstream>
- #include <queue>
- using namespace std;
- const int MAX = 50001;
- vector<int> lst[MAX];
- int parent[MAX];
- void make_set(int v) {
- lst[v] = vector<int>(1, v);
- parent[v] = v;
- }
- int find_set(int v) {
- return parent[v];
- }
- void union_sets(int a, int b) {
- a = find_set(a);
- b = find_set(b);
- if (a != b) {
- if (lst[a].size() < lst[b].size())
- swap(a, b);
- while (!lst[b].empty()) {
- int v = lst[b].back();
- lst[b].pop_back();
- parent[v] = a;
- lst[a].push_back(v);
- }
- }
- }
- int main()
- {
- vector<pair<pair<long long,long long>, pair<long long, long long>>> ter;
- long long n, m, s;
- cin >> n >> m >> s;
- long long a, b, si;
- for (int i = 0; i < m; i++)
- {
- cin >> a >> b >> si;
- ter.push_back({ {si,i+1},{a,b} });
- }
- sort(ter.rbegin(), ter.rend());
- for (int i = 1; i <= n; i++)
- make_set(i);
- vector<pair<int, int>> used;
- for (int i = 0; i < m; i++)
- {
- long long prom1 = find_set(ter[i].second.first);
- long long prom2 = find_set(ter[i].second.second);
- if (prom1==prom2)
- {
- used.push_back(ter[i].first);
- }
- else
- {
- union_sets(prom1, prom2);
- }
- }
- vector<int>bax;
- for (int i = used.size()-1; i >=0; i--)
- {
- if (used[i].first <= s)
- {
- bax.push_back(used[i].second);
- s -= used[i].first;
- }
- }
- cout << bax.size() << endl;
- for (int i = 0; i < bax.size(); i++)
- cout << bax[i] << " ";
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment