Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5. #define MAXN 112345
  6. #define INF LLONG_MAX
  7. #define double long double
  8. #define int long long
  9. #define pii pair<long long,long long>
  10. #define f first
  11. #define s second
  12. #define mp make_pair
  13. #define eps 1e-11
  14. #define pi acos(-1)
  15. #define MOD 1000000007
  16.  
  17. int n;
  18. int pai[MAXN],peso[MAXN];
  19.  
  20. int find(int x){
  21. return pai[x]=(x==pai[x]?x:find(pai[x]));
  22. }
  23.  
  24. void join(int x,int y){
  25. if((x=find(x))==(y=find(y)))return;
  26. if(peso[x]<peso[y])swap(x,y);
  27. peso[x]+=peso[y];pai[y]=x;
  28. }
  29.  
  30. void init(){
  31. for(int i=0;i<n;i++)peso[pai[i]=i]=1;
  32. }
  33.  
  34. pii val[MAXN];
  35.  
  36. int32_t main(){
  37. ios::sync_with_stdio(false);
  38. cin.tie(0);
  39. cout.precision(9);
  40. cout.setf(ios::fixed);
  41.  
  42. int m;
  43. cin>>n>>m;
  44. for(int i=0;i<n;i++){
  45. cin>>val[i].f;
  46. val[i].s=i;
  47. }
  48. sort(val,val+n);
  49. vector<pair<int,pii>> edges;
  50. for(int i=0;i<n-1;i++)
  51. edges.pb(mp(val[i].f-val[i+1].f,mp(val[i].s,val[i+1].s)));
  52.  
  53. for(int i=0;i<m;i++){
  54. int a,b;
  55. cin>>a>>b;
  56. a--;b--;
  57. join(a,b);
  58. }
  59.  
  60. sort(edges.begin(),edges.end());
  61. int resp=0;
  62. for(auto x:edges){
  63. if(find(x.s.f)!=find(x.s.s)){
  64. join(x.s.f,x.s.s);
  65. resp+=x.f*x.f;
  66. }
  67. }
  68.  
  69. cout<<resp<<endl;
  70.  
  71.  
  72.  
  73.  
  74. return 0;
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement