Saleh127

UVA 10369 / MST

Sep 25th, 2021 (edited)
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  5.  
  6. vector<ll>parent,rankk;
  7. vector<pair<double,pair<ll,ll>>>edge;
  8.  
  9. void make_set(ll n)
  10. {
  11. for(ll i=0; i<n; i++)
  12. {
  13. parent[i] = i;
  14. //rankk[i] = 0;
  15. }
  16. }
  17.  
  18. ll find_set(ll v)
  19. {
  20. if (v == parent[v])
  21. {
  22. return v;
  23. }
  24. return parent[v] = find_set(parent[v]);
  25. }
  26.  
  27. void union_sets(ll a, ll b)
  28. {
  29. a = find_set(a);
  30. b = find_set(b);
  31.  
  32. if (a != b)
  33. {
  34. if(rand()%2)
  35. {
  36. swap(a,b);
  37. }
  38. parent[b]=a;
  39.  
  40. /* else use this
  41. if (rankk[a] < rankk[b]){swap(a, b);} parent[b] = a; if (rankk[a] == rankk[b]){rankk[a]++;}
  42. */
  43. }
  44. }
  45.  
  46.  
  47. double distancee(double xi,double xj, double yi, double yj)
  48. {
  49. return sqrt((xi-xj)*(xi-xj) + (yi-yj)*(yi-yj));
  50. }
  51.  
  52. int main()
  53. {
  54. ios_base::sync_with_stdio(0);
  55. cin.tie(0);
  56. cout.tie(0);
  57.  
  58.  
  59. test
  60. {
  61. ll s,n,i,j,k,l=0;
  62.  
  63. double m=0,c,d;
  64.  
  65. cin>>s>>n;
  66.  
  67. edge.clear();
  68.  
  69. parent.resize(n);
  70. //rankk.resize(n);
  71.  
  72. make_set(n);
  73.  
  74.  
  75. double x[n+4],y[n+4];
  76.  
  77. for(i=0; i<n; i++)
  78. {
  79. cin>>x[i]>>y[i];
  80. }
  81.  
  82. for(i=0; i<n; i++)
  83. {
  84. for(j=i+1; j<n; j++)
  85. {
  86. double di=distancee(x[i],x[j],y[i],y[j]);
  87. edge.push_back({di,{i,j}});
  88. }
  89. }
  90.  
  91. sort(edge.begin(),edge.end());
  92.  
  93. for(auto dd:edge)
  94. {
  95. if(find_set(dd.second.second)!=find_set(dd.second.first))
  96. {
  97. union_sets(dd.second.first,dd.second.second);
  98.  
  99. if(l<n-s)
  100. {
  101. m=max(m,dd.first);
  102. l++;
  103. }
  104. else break;
  105. }
  106. }
  107.  
  108. cout<<fixed<<setprecision(2)<<m<<endl;
  109. }
  110.  
  111. return 0;
  112. }
  113.  
Add Comment
Please, Sign In to add comment