Created
July 27, 2017 16:07
-
-
Save kriznaraj/e9eb75b219696b9f5fcb4ca3467d83c7 to your computer and use it in GitHub Desktop.
Kingdom Division (Solution for https://www.hackerrank.com/challenges/kingdom-division)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| namespace KingdomDivision | |
| { | |
| class Edge | |
| { | |
| public int start; | |
| public int end; | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var edges = new List<Edge>(); | |
| var n = int.Parse(Console.ReadLine()); | |
| for (int i = 0; i < n - 1; i++) | |
| { | |
| var edge = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); | |
| edges.Add(new Edge() {start= edge[0], end = edge[1]}); | |
| } | |
| Console.WriteLine(PartitionWays(edges)); | |
| Console.ReadKey(); | |
| } | |
| static int PartitionWays(List<Edge> edges) | |
| { | |
| var n = new Dictionary<int,int>(); | |
| foreach (var edge in edges) | |
| { | |
| n[edge.start] = n.ContainsKey(edge.start) ? n[edge.start] + 1 : 1; | |
| n[edge.end] = n.ContainsKey(edge.end) ? n[edge.end] + 1 : 1; | |
| } | |
| var split = 0; | |
| foreach (var edge in edges) | |
| { | |
| if(n[edge.start] > 1 && n[edge.end] > 1) | |
| { | |
| split++; | |
| } | |
| } | |
| return (split + 1)*2; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is the solution correct?