eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

Gadget Hackwrench

Gadget Hackwrench

Chip ’n’ Dale rescue rangers! But observant viewers know that help is usually required by Chip and Dale themselves. Today you are in the role of cunning Gadget Hackwrench. So, Chip and Dale are again in the paws of Fat Cat. He doesn’t like rodents much and therefore prepared a treacherous test. He is going to put them to a labyrinth and see if they can escape from it. The labyrinth is actually built as a tree where each edge has fixed direction (by definition tree is a connected unoriented graph without cycles). Gadget has intercepted a talk between Fat Cat and his henchmen about future tests. For each test round she knows the exact location where Chip and Dale are to be put by Fat Cat and the location of an exit. Gadget wants to compute whether they will be able to find an exit for each test. \InputFile The first line of input contains an integer \textbf{N} (\textbf{1} ≤ \textbf{N} ≤ \textbf{10^5}) --- the number of vertices in a graph. On the next \textbf{N−1} lines of input directed arcs of the tree are given. On the \textbf{(i+1)}^th line integer numbers \textbf{a_i} and \textbf{b_i} are given (\textbf{1} ≤ \textbf{a_i}, \textbf{b_i} ≤ \textbf{N}) denoting an arc from vertex \textbf{a_i} to vertex \textbf{b_i}. It is guaranteed that arcs \textbf{a_1}, \textbf{a_2}, ..., \textbf{a_\{n-1\}} without orientation form a tree. Then a string with integer number \textbf{M} (\textbf{1} ≤ \textbf{M} ≤ \textbf{10^5}) is given --- the number of queries to process. Next \textbf{M} lines describe queries: \textbf{(n+1+i)}^th line contain integers \textbf{x_i} and \textbf{y_i} (\textbf{1} ≤ \textbf{x_i}, \textbf{y_i} ≤ \textbf{N}). \OutputFile For each query please output a separate line containing '\textbf{Yes}' (without quotes) if graph contains a path between \textbf{x_i}and \textbf{y_i}, or '\textbf{No}' (without quotes) in other case.
Лимит времени 1 секунда
Лимит использования памяти 256 MiB
Входные данные #1
4
1 2
3 1
4 1
6
1 2
3 2
2 3
4 2
4 3
2 1
Выходные данные #1
Yes
Yes
No
Yes
No
No
Источник ACM ICPC 2012-2013, NEERC, Moscow Subregion, Moscow, Sunday, October 21, 2012