fork download
  1. Uses Math;
  2. Const
  3. MaxN = 100005;
  4. Type
  5. Edge = record
  6. x, y : Longint;
  7. End;
  8. Var
  9. e : array[0..MaxN] of Edge;
  10. a : array[0..2 * MaxN] of Longint;
  11. head : array[0..MaxN] of Longint;
  12. c : array[0..MaxN] of Longint;
  13. visited : array[0..MaxN] of Boolean;
  14. ans : Int64;
  15. n, m : Longint;
  16.  
  17. Procedure Dfs(u : Longint; Var cost : Longint);
  18. Var
  19. i, v : Longint;
  20. Begin
  21. visited[u] := true;
  22. cost := min(cost, c[u]);
  23. For i := head[u] + 1 to head[u + 1] do
  24. Begin
  25. v := a[i];
  26. if (visited[v]) then continue;
  27. Dfs(v, cost);
  28. End;
  29. End;
  30.  
  31. Procedure Enter();
  32. Var
  33. i, x, y : Longint;
  34. Begin
  35. Readln(n, m);
  36. For i := 1 to n do
  37. read(c[i]);
  38. For i := 1 to m do
  39. Begin
  40. readln(x, y);
  41. e[i].x := x; e[i].y := y;
  42. inc(head[x]); inc(head[y]);
  43. End;
  44. For i := 1 to n + 1 do
  45. inc(head[i], head[i - 1]);
  46. For i := 1 to m do
  47. Begin
  48. x := e[i].x; y := e[i].y;
  49. a[head[x]] := y;
  50. dec(head[x]);
  51. a[head[y]] := x;
  52. dec(head[y]);
  53. End;
  54. End;
  55.  
  56. Procedure Solve();
  57. Var
  58. i, cost : Longint;
  59. Begin
  60. For i := 1 to n do
  61. Begin
  62. if (not visited[i]) then
  63. Begin
  64. cost := 1123123123;
  65. Dfs(i, cost);
  66. inc(ans, cost);
  67. End;
  68. End;
  69. End;
  70.  
  71. Begin
  72. Enter();
  73. Solve();
  74. Writeln(ans);
  75. End.
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
0