-->
当前位置:首页 > 题库

PROGRAMMING:Is It a Valid DFS Traversal Sequence

Luz5年前 (2021-05-10)题库531
Given a directed graph and its DFS traversal sequences, you should judge if given sequences are valid.
For example, with respect to the graph below,
![ figure.png](~/ffa16db5-61fa-423f-909c-729e4d011257.png)
`0 1 4 2 5 3` , `0 2 5 1 4 3` and `3 2 5 1 4 0` are all valid DFS traversal sequences, but `0 2 3 1 4 5` is not.
#### Input Specification:
Each input file contains one test case. For each case, the first line contains two number $$M$$ (<= 100, the total count of vertices), and $$N$$ (the total count of edges), separated by a space. Notice that vertices are indexed from 0 to $$M-1$$.
The following $$N$$ lines each give an edge in the format shown below:
`ID1 ID2`
It denotes a directed edge from the vertex indexed `ID1` to the vertex indexed `ID2`. Then the next line contains a number $$K$$ (<= 20, the count of sequences to be judged). And the following $$K$$ lines each give a DFS traversal sequence consisting of $$M$$ different numbers separated by a space.
#### Output Specification:
For each DFS traversal sequence to be judged, output `Yes` if it’s valid, `No` if not.
#### Sample Input:
```in
6 9
0 1
0 2
0 3
2 1
3 2
4 2
1 4
2 5
4 5
three
0 1 4 2 5 3
0 2 5 1 4 3
0 2 3 1 4 5
```
#### Sample Output:
```out
Yes
Yes
No
```







answer:If there is no answer, please comment