Skip to content

Instantly share code, notes, and snippets.

@kriznaraj
Created July 27, 2017 16:07
Show Gist options
  • Select an option

  • Save kriznaraj/e9eb75b219696b9f5fcb4ca3467d83c7 to your computer and use it in GitHub Desktop.

Select an option

Save kriznaraj/e9eb75b219696b9f5fcb4ca3467d83c7 to your computer and use it in GitHub Desktop.
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;
}
}
}
@gypsysunny
Copy link

is the solution correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment