Advertisement
aurko96

Codeforce- 609E

Nov 24th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define LL long long
  3. using namespace std;
  4. struct edge
  5. {
  6.     int u,v,w;
  7.     bool operator < ( const edge& p ) const
  8.     {
  9.         return w < p.w;
  10.     }
  11. };
  12. int pr[100005];
  13. vector<edge>e;
  14. vector<pair<int,int> >edge2[100005];
  15. pair<int,int>val[100005];
  16. int find(int r)
  17. {
  18.     return (pr[r]==r) ? r : find(pr[r]);
  19. }
  20.  
  21. int mst(int n)
  22. {
  23.     sort(e.begin(),e.end());
  24.     for(int i=0;i<=n;i++)pr[i]=i;
  25.  
  26.     int count=0;
  27.     int s=0;
  28.  
  29.     for(int i=0;i<(int)e.size();i++)
  30.     {
  31.         int u=find(e[i].u);
  32.         int v=find(e[i].v);
  33.  
  34.         if(u!=v)
  35.         {
  36.             pr[u]=v;
  37.             count++;
  38.             s+=e[i].w;
  39.             val[v].first=u;
  40.             val[v].second=e[i].w;
  41.             edge2[u].push_back(make_pair(v,e[i].w));
  42.             edge2[v].push_back(make_pair(u,e[i].w));
  43.             cout<<"u = "<<u<<" v = "<<v<<" ara = "<<val[v].second<<endl;
  44.             if(count==n-1) break;
  45.         }
  46.  
  47.     }
  48.     return s;
  49. }
  50. int main()
  51. {
  52.     int ara[100005],ara2[100005],i,j,k,n,m,x,y,z;
  53.     cin>>m>>n;
  54.     for(i=0;i<n;i++)
  55.     {
  56.         cin>>x>>y>>z;
  57.         ara[i]=z;
  58.         ara2[i]=y;
  59.         edge get;
  60.         get.u=x;
  61.         get.v=y;
  62.         get.w=z;
  63.         e.push_back(get);
  64.     }
  65.     int ans=mst(m);
  66.     cout<<ans<<endl;
  67.     for(i=0;i<n;i++)
  68.     {
  69.         //k=(ans+ara[i])-val[ara2[i]].second;
  70.         k=(ans+ara[i])-edge2[ara2[i]][0].second;
  71.        // cout<<"ara = "<<val[ara2[i]].second<<endl;
  72.         cout<<k<<endl;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement