diff --git a/Task02/SequentialAlgoKruskal.cs b/Task02/SequentialAlgoKruskal.cs new file mode 100644 index 0000000..4ab4200 --- /dev/null +++ b/Task02/SequentialAlgoKruskal.cs @@ -0,0 +1,46 @@ +using System; + +namespace Task02 +{ + public class SequentialAlgoKruskal + { + static int[] dsu; + + static int getDsu(int x) + { + return dsu[x] == -1 ? x : dsu[x] = getDsu(dsu[x]); + } + + static bool unionDsu(int x, int y) + { + x = getDsu(x); + y = getDsu(y); + if (x == y) + return false; + if ((x + 2 * y) % 4 * 2 % 6 != 0) // псевдорандом + dsu[y] = x; + else + dsu[x] = y; + return true; + } + + public static int Execute() + { + int ans = 0; + (int, int, int)[] edges = GeneralResources.graphListEdges; + Array.Sort(edges); + + dsu = new int[GeneralResources.n]; + Array.Fill(dsu, -1); + + for (int i = 0; i < edges.Length; i++) + { + if (unionDsu(edges[i].Item2, edges[i].Item3)) + { + ans += edges[i].Item1; + } + } + return ans; + } + } +}