Hierarchy
InterviewTime limit1sMemory limit1024 MB
After each edge reversal, report whether the directed tree of superior-subordinate edges is a valid hierarchy with one root.
- Level
Easy3 of 10
- Topics
- Graph, Array, Implementation
- Solved
- No attempts yet
Problem
Krešimir is studying corporate structures, including hierarchies. He observed the employees of a company and their relationships. This problem only considers superior-subordinate relationships, meaning cases where one employee is directly superior to another employee in the company.
A hierarchy is a structure with employees and superior-subordinate relationships, where one person is directly or indirectly superior to all employees. The observed company also has employees and such relationships, but it is not certain whether it is a valid hierarchy.
Krešimir asked you to help answer this question. He recorded all the data in his notebook. He will also make permanent changes by reversing one superior-subordinate relationship, so that the subordinate becomes superior to their former superior. After each change, you must answer the same question: is the current state a valid hierarchy?
Input
The first line contains a positive integer ().
Each of the next lines contains a pair of integers and (, ), meaning that is directly superior to , for .
The next line contains a non-negative integer ().
Each of the following lines contains a pair , (, ). At that moment, is directly superior to or is directly superior to .
It is guaranteed that at least one hierarchy can be obtained by some sequence of reversals.
Output
Print lines, one for each state (the initial state and the state after each change). Print DA if the current structure is a hierarchy, and NE otherwise.