Advertisement
tanasaradu

Untitled

Oct 22nd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("apm.in");
  4. ofstream fout("apm.out");
  5. const int NMAX=200005;
  6. int n,muchii,t[NMAX];
  7. struct T
  8. {
  9. int nod,nod1,cost;
  10. bool ok;
  11. };
  12. T a[NMAX];
  13. inline void UNION(int x,int y)
  14. {
  15. t[y]=x;
  16. }
  17. inline int Find(int x)
  18. {
  19. int rad,y;
  20. rad=x;
  21. while(t[rad])
  22. rad=t[rad];
  23. while(x!=rad)
  24. {
  25. y=t[x];
  26. t[x]=rad;
  27. x=y;
  28. }
  29. return x;
  30. }
  31. inline bool SORT(const T A,const T B)
  32. {
  33. return A.cost<B.cost;
  34. }
  35. inline void READ()
  36. {
  37. fin>>n>>muchii;
  38. for(int i=1;i<=muchii;i++)
  39. fin>>a[i].nod>>a[i].nod1>>a[i].cost;
  40. }
  41. inline void SOLVE()
  42. {
  43. long long solcost=0;
  44. sort(a+1,a+muchii+1,SORT);
  45. for(int i=1;i<=muchii;i++)
  46. {
  47. int x,y;
  48. x=a[i].nod;
  49. y=a[i].nod1;
  50. x=Find(x);
  51. y=Find(y);
  52. if(x!=y)
  53. {
  54. UNION(x,y);
  55. solcost+=a[i].cost;
  56. a[i].ok=true;
  57. }
  58. }
  59. fout<<solcost<<"\n"<<(n-1)<<"\n";
  60. for(int i=1;i<=muchii;i++)
  61. if(a[i].ok)
  62. fout<<a[i].nod<<" "<<a[i].nod1<<"\n";
  63. }
  64. int main()
  65. {
  66. READ();
  67. SOLVE();
  68. fin.close();
  69. fout.close();
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement