Skip to content

Commit

Permalink
Create SequentialAlgoKruskal.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyKuz1001 authored Nov 1, 2019
1 parent 06eaed3 commit e8ad5fe
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Task02/SequentialAlgoKruskal.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit e8ad5fe

Please sign in to comment.