-
Notifications
You must be signed in to change notification settings - Fork 35
/
Kruskal(MST)ReallySpecialSubtree.cs
163 lines (131 loc) · 4.01 KB
/
Kruskal(MST)ReallySpecialSubtree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Kruskal (MST): Really Special Subtree
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Result
{
/*
* Complete the 'kruskals' function below.
*
* The function is expected to return an INTEGER.
* The function accepts WEIGHTED_INTEGER_GRAPH g as parameter.
*/
/*
* For the weighted graph, <name>:
*
* 1. The number of nodes is <name>Nodes.
* 2. The number of edges is <name>Edges.
* 3. An edge exists between <name>From[i] and <name>To[i]. The weight of the edge is <name>Weight[i].
*
*/
public static int kruskals(int gNodes, List<int> gFrom, List<int> gTo, List<int> gWeight)
{
var ds = new DisjointSets(gNodes);
var edges = new Edge[gFrom.Count];
for(var index = 0; index < gFrom.Count; index++)
{
edges[index] = new Edge
{
From = gFrom[index],
To = gTo[index],
Weight = gWeight[index]
};
}
Array.Sort(edges);
var edgesUsed = 0;
var sum = 0;
foreach(var edge in edges)
{
if(edgesUsed == gNodes - 1) break;
if(ds.Find(edge.From) == ds.Find(edge.To)) continue;
edgesUsed++;
sum += edge.Weight;
ds.Union(edge.From, edge.To);
}
return sum;
}
class DisjointSets
{
int[] Parents;
int[] Ranks;
public DisjointSets(int nodes)
{
Parents = new int[nodes];
Ranks = new int[nodes];
for(var index = 0; index < nodes; index++)
Parents[index] = index;
}
public int Find(int value) => FindImpl(value - 1) + 1;
private int FindImpl(int value)
{
if(Parents[value] == value)
return value;
else
return FindImpl(Parents[value]);
}
public void Union(int left, int right)
{
left = Find(left) - 1;
right = Find(right) - 1;
if(Ranks[left] > Ranks[right])
{
Parents[right] = left;
}
else if(Ranks[left] < Ranks[right])
{
Parents[left] = right;
}
else
{
Parents[right] = left;
Ranks[left]++;
}
}
}
class Edge : IComparable<Edge>
{
public int From;
public int To;
public int Weight;
public int CompareTo(Edge other)
{
var result = Weight.CompareTo(other.Weight);
if(result != 0) return result;
return (From + Weight + To).CompareTo(other.From + other.Weight + other.To);
}
}
}
class Solution
{
public static void Main(string[] args)
{
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] gNodesEdges = Console.ReadLine().TrimEnd().Split(' ');
int gNodes = Convert.ToInt32(gNodesEdges[0]);
int gEdges = Convert.ToInt32(gNodesEdges[1]);
List<int> gFrom = new List<int>();
List<int> gTo = new List<int>();
List<int> gWeight = new List<int>();
for (int i = 0; i < gEdges; i++)
{
string[] gFromToWeight = Console.ReadLine().TrimEnd().Split(' ');
gFrom.Add(Convert.ToInt32(gFromToWeight[0]));
gTo.Add(Convert.ToInt32(gFromToWeight[1]));
gWeight.Add(Convert.ToInt32(gFromToWeight[2]));
}
int res = Result.kruskals(gNodes, gFrom, gTo, gWeight);
textWriter.Write(res);
textWriter.Flush();
textWriter.Close();
}
}