Skip to content

Instantly share code, notes, and snippets.

@kaathewise
Last active December 20, 2016 10:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaathewise/2c838b74ce0000f81b2582f8b9d496f3 to your computer and use it in GitHub Desktop.
Save kaathewise/2c838b74ce0000f81b2582f8b9d496f3 to your computer and use it in GitHub Desktop.
#/usr/bin/env perl -wl
use strict;
use List::Util qw(sum);
my %graph;
<>;
while (<>) {
my ($a, $b) = split;
push @{$graph{$a}}, $b;
push @{$graph{$b}}, $a;
}
my $even_subtrees;
sub traverse {
my ($node, $parent) = @_;
my $children_size = sum map { traverse($_, $node) } grep { $_ != $parent } @{$graph{$node}};
$even_subtrees += $children_size % 2;
return $children_size + 1;
}
traverse(1, 0);
print $even_subtrees - 1, "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment