We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Graph Theory
  4. Even Tree

Even Tree

Problem
Submissions
Leaderboard
Discussions
Editorial
  1. Prepare
  2. Algorithms
  3. Graph Theory
  4. Even Tree
Exit Full Screen View
  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

You are given a tree (a simple connected graph with no cycles).

Find the maximum number of edges you can remove from the tree to get a forest such that each connected component of the forest contains an even number of nodes.

As an example, the following tree with nodes can be cut at most time to create an even forest.

image

Function Description

Complete the evenForest function in the editor below. It should return an integer as described.

evenForest has the following parameter(s):

  • t_nodes: the number of nodes in the tree
  • t_edges: the number of undirected edges in the tree
  • t_from: start nodes for each edge
  • t_to: end nodes for each edge, (Match by index to t_from.)

Input Format

The first line of input contains two integers and , the number of nodes and edges.
The next lines contain two integers and which specify nodes connected by an edge of the tree. The root of the tree is node .

Constraints

Note: The tree in the input will be such that it can always be decomposed into components containing an even number of nodes. is the set of positive even integers.

Output Format

Print the number of removed edges.

Sample Input 1

CopyDownload
%0 Undirected Graph: t 2 2 1 1 2--1 3 3 3--1 4 4 4--3 5 5 5--2 6 6 6--1 7 7 7--2 8 8 8--6 9 9 9--8 10 10 10--8
10 9
2 1
3 1
4 3
5 2
6 1
7 2
8 6
9 8
10 8

Sample Output 1

2

Explanation 1

Remove edges and to get the desired result.

image image

No more edges can be removed.

  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy