ZOJ Problem Set - 2838
Few people know a secret nation in the world named Utopia. But I have been there before, where there are many beautiful villages. Seeing from the space, you will find that all the villages are connected together via some roads such that there is one and only one path between every two villages.
One day, when I was taking photos of this country, I met a handsome young man named Pierre who looked very worried. So I came up and wondered if I could do something for him. He told me that he had fallen in love with Mary, the most beautiful girl in his village A.
Every morning, Mary went to work in a bakery shop in the village B and she came home at 6 p.m in the evening. After a long time, Pierre couldn't wait telling Mary that he loved her, so he decided to wait for Mary and tell her his love in the village C.
Unfortunately, Pierre was not sure if C was on the path between B and his own village. He felt very upset.
Pierre's love moved me, but you know, I was a stranger there myself, and I knew little about Utopia. Therefore, I stayed with Pierre and waited for the help of some Wiseman.
Can you help him?
There are multiple test cases for this problem.
Each test case starts with a line containing an integer N (1 <= N <= 50000), representing that there are totally N villages (indexed from 0 to N-1), followed by N - 1 lines which provide the road information in Utopia. Each line with the form 'V1 V2' (without quotation, 0 <= V1, V2 <= N-1) means that there is a bidirectional road between village V1 and V2. Then it's an integer M (1 <= M <= 500000), followed by M queries, each query is in the form of 'A B C' (without quotation, 0 <= A, B, C <= N-1) in a single line.
For each test case, output "Case #:" first. "#" is the number of the case, which starts from 1. Then for each query 'A B C', if village C is on the path between village A and B, output 'Yes' (without quotation) in a single line, otherwise output 'No' (without quotation).
Separate two consecutive test cases with a blank line, but Do NOT output an extra blank line after the last one.
NotesThis problem has huge input and output data, please use 'scanf()' and 'printf()' instead of 'cin' and 'cout' to avoid timeout.
Author: LIU, Yaoting
Source: Zhejiang University Local Contest 2007