Bamboo Forest
Time limit3sMemory limit1024 MB
Given a graph, decide whether every connected component is a bamboo: a tree with a trunk of at least 3 edges where each trunk vertex has 0 or 2 side leaves and all vertices lie within distance 1 of the trunk.
- Level
Medium7 of 10
- Topics
- Graph, Tree, DFS, Implementation
- Solved
- No attempts yet
Problem
Jeonghwi defined a bamboo as follows. (It differs from the term Bamboo Tree used in graph theory.)
- A bamboo is a kind of tree.
- It has a trunk of length at least 3 (made up of at least 3 edges).
- Vertices can branch off to both sides from a vertex on the trunk.
- A vertex on the trunk cannot have 1 or 3 or more vertices branching off from it. (Only 0 or 2 are allowed.)
- Every vertex must be at distance at most 1 from the trunk.
A bamboo forest is a forest made up only of bamboos.
Given a graph, determine whether it is a bamboo forest.

Sample 2 is not a bamboo forest because only 1 vertex branches off from vertex 2.
Sample 4 is not a bamboo forest because 3 vertices branch off from vertex 2.
Sample 5 is not a bamboo forest because among the vertices branching off from vertex 3 there is a vertex at distance 2 or more from the trunk.
Sample 6 is not a bamboo forest because no trunk of length at least 3 exists.
Sample 8 is not a forest.
Input
The first line gives the number of vertices and the number of edges , separated by a space.
From the second line, lines follow, each giving the two vertices connected by an edge, separated by a space.
Output
Print TAK if the given graph is a bamboo forest, and NIE otherwise.
Constraints
- No edge connects a vertex to itself.
- No duplicate edge is given.