Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 19, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/6807c94746d93641e61181d91d153638 to your computer and use it in GitHub Desktop.
Save jianminchen/6807c94746d93641e61181d91d153638 to your computer and use it in GitHub Desktop.
Hackerrank week code 28 - value of friendship - code review - Julia likes the structure of code, variable names etc.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
/*
* hackerrank ID: phhlho
* January 19, 2017
* Julia study the code.
*
*/
private class StudentNode
{
public int StudentNumber { get; set; }
public List<StudentNode> Friends { get; set; }
public bool Placed { get; set; }
public StudentNode(int num)
{
StudentNumber = num;
Friends = new List<StudentNode>();
Placed = false;
}
}
private static List<StudentNode> IntializeStudents(int nStudents, List<Tuple<int, int>> connections)
{
var students = new List<StudentNode>();
students = new List<StudentNode>();
for (var i = 0; i < nStudents; i++)
{
students.Add(new StudentNode(i));
}
foreach (var connection in connections)
{
students[connection.Item1 - 1].Friends.Add(students[connection.Item2 - 1]);
students[connection.Item2 - 1].Friends.Add(students[connection.Item1 - 1]);
}
return students;
}
/*
*/
private static List<HashSet<int>> CreateHashSets(List<StudentNode> students)
{
var returnSets = new List<HashSet<int>>();
foreach (var student in students)
{
if (student.Placed)
{
continue;
}
var newSet = CreateSetFromStudent(student);
if (newSet.Count > 1)
{
returnSets.Add(newSet);
}
}
return returnSets;
}
/*
*/
private static HashSet<int> CreateSetFromStudent(StudentNode student)
{
var returnSet = new HashSet<int>();
var studentsToProcess = new Queue<StudentNode>();
studentsToProcess.Enqueue(student);
while (studentsToProcess.Count > 0)
{
var processStudent = studentsToProcess.Dequeue();
if (returnSet.Contains(processStudent.StudentNumber))
{
continue;
}
returnSet.Add(processStudent.StudentNumber);
processStudent.Placed = true;
foreach (var friend in processStudent.Friends)
{
if (!returnSet.Contains(friend.StudentNumber))
{
studentsToProcess.Enqueue(friend);
}
}
}
return returnSet;
}
/*
*/
private static long CalculateTotalFromSets(List<HashSet<int>> sets, List<Tuple<int, int>> connections)
{
var orderedSets = sets.OrderByDescending(x => x.Count);
long finalTotal = 0;
long runningSetAddTotal = 0;
long totalConnectionsAdded = 0;
foreach (var set in orderedSets)
{
long n = set.Count - 1;
// Set total is sum from 1 to n of i * i + 1
long setTotal = ((n * (n + 1)) / 2) + ((n * (n + 1) * (2 * n + 1)) / 6);
finalTotal += setTotal + runningSetAddTotal * n;
totalConnectionsAdded += n;
runningSetAddTotal += (n * (n + 1));
}
while (totalConnectionsAdded < connections.Count)
{
finalTotal += runningSetAddTotal;
totalConnectionsAdded++;
}
return finalTotal;
}
public static long Run(int nStudents, List<Tuple<int, int>> connections)
{
var students = IntializeStudents(nStudents, connections);
var sets = CreateHashSets(students);
return CalculateTotalFromSets(sets, connections);
}
static void Main(String[] args)
{
int t = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < t; i++)
{
string[] tokens_n = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(tokens_n[0]);
int m = Convert.ToInt32(tokens_n[1]);
var connections = new List<Tuple<int, int>>();
for (int j = 0; j < m; j++)
{
string[] tokens_x = Console.ReadLine().Split(' ');
int x = Convert.ToInt32(tokens_x[0]);
int y = Convert.ToInt32(tokens_x[1]);
connections.Add(Tuple.Create(x, y));
}
Console.WriteLine(Run(n, connections));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment