fork download
  1. // LonggVuz.
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. // Legendary Grandmaster
  5. void End(){
  6. cerr << "=> Thời gian code chạy: ";
  7. cerr << (1.0 * clock() / CLOCKS_PER_SEC) << " giây" << string(27, '\t');
  8. }
  9. #define Sonic() ios_base::sync_with_stdio(false); cin.tie(NULL);
  10. #define use(x) freopen(x".inp", "r", stdin); freopen(x".out", "w", stdout);
  11. #define fix(x) fixed << setprecision(x)
  12. #define all(x) x.begin(), x.end()
  13. #define len(x) (int)x.size()
  14. #define ms(a, x) memset(a, x, sizeof(a))
  15. #define bit(n, x) (n >> x & 1)
  16. #define pb push_back
  17. #define is insert
  18. #define el '\n'
  19. #define ld long double
  20. #define int int64_t
  21.  
  22. const int mod = 1e9 + 7;
  23. const int oo = 1e18 + 7;
  24. const int maxn = 1e5 + 7;
  25.  
  26. int n, m, c[maxn], r[maxn];
  27.  
  28. int get(int u){
  29. if(u == r[u]) return u;
  30. return r[u] = get(r[u]);
  31. }
  32.  
  33. void LonggVuz(){
  34. cin >> n >> m;
  35. for(int i=1; i<=n; i++){
  36. cin >> c[i];
  37. r[i] = i;
  38. }
  39. while(m--){
  40. int x, y; cin >> x >> y;
  41. x = get(x); y = get(y);
  42. if(x != y){
  43. if(c[x] < c[y]){
  44. r[y] = x;
  45. }else{
  46. r[x] = y;
  47. }
  48. }
  49. }int res = 0;
  50. for(int i=1; i<=n; i++){
  51. int x = get(i);
  52. if(c[x] > 0){
  53. res += c[x];
  54. c[x] = 0;
  55. }
  56. }cout << res;
  57. }
  58.  
  59. signed main(){
  60. Sonic();
  61.  
  62. int TC = 1; //cin >> TC;
  63. while(TC--) LonggVuz();
  64.  
  65. End();
  66. }
Success #stdin #stdout #stderr 0.01s 5280KB
stdin
5 4
6 4 2 3 3
1 2
2 4
4 1
3 5
stdout
5
stderr
=> Thời gian code chạy: 0.004793 giây