Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvs(vector <string> v){
- for (auto a: v){
- cout<<a<<"\n";
- }
- }
- void cvp(vector <pii> a){
- for (auto p: a){
- cout<<p.first<<' '<<p.second<<"\n";
- }
- cout<<"\n";
- }
- int n,m,A,B;
- struct ed{
- int a,b,l;
- };
- vector <ed> E;
- vector <int> pr,sz;
- bool sh = 1;
- int getp(int v){
- if (pr[v] == v){
- return v;
- }
- int p = getp(v);
- pr[v] = p;
- return p;
- }
- void un(int a, int b){
- a = getp(a);
- b = getp(b);
- if (sh){
- cout<<"PAR a b = "<<a<<' '<<b<<"\n";
- }
- if (a == b)return;
- //sz[b] > sz[a]
- if (sz[a] > sz[b]) swap(a,b);
- sz[b] += sz[a];
- pr[a] = b;
- }
- int main()
- {
- /*ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- cin>>n>>m;
- map <pii, int> mtr;
- for (int i = 0; i < m; ++i){
- int a,b,l;
- cin>>a>>b>>l;
- a--;
- b--;
- if (a > b) swap(a,b);
- if (mtr.find({a,b}) == mtr.end()){
- mtr[{a,b}] = l;
- }
- else{
- mtr[{a,b}] = max(mtr[{a,b}], l);
- }
- }
- cin>>A>>B;
- for (auto p: mtr){
- //cout<<p.first.first<<' '<<p.first.second<<' '<<p.second<<"\n";
- E.push_back({p.first.first, p.first.second, p.second});
- }
- pr.resize(n);
- sz.assign(n,1);
- for (int i = 0; i < n; ++i){
- pr[i] = i;
- }
- /*if (sh){
- cout<<"sz\n";
- cv(sz);
- cout<<"pr\n";
- cv(pr);
- }*/
- for (ed e: E){
- if (sh){
- cout<<"a b = "<<e.a<<' '<<e.b<<"\n";
- }
- un(e.a, e.b);
- }
- /*if (sh){
- cout<<"sz\n";
- cv(sz);
- cout<<"pr\n";
- cv(pr);
- }*/
- }
- /*
- 6 8
- 1 2 3
- 1 3 2
- 1 4 4
- 1 6 5
- 2 3 15
- 3 6 8
- 3 5 7
- 4 5 1
- 1 5
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement