CF855D.Rowena Ravenclaw's Diadem

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Harry, upon inquiring Helena Ravenclaw's ghost, came to know that she told Tom Riddle or You-know-who about Rowena Ravenclaw's diadem and that he stole it from her.

Harry thought that Riddle would have assumed that he was the only one to discover the Room of Requirement and thus, would have hidden it there. So Harry is trying to get inside the Room of Requirement to destroy the diadem as he knows that it is a horcrux.

But he has to answer a puzzle in order to enter the room. He is given nn objects, numbered from 11 to nn . Some of the objects have a parent object, that has a lesser number. Formally, object ii may have a parent object parentiparent_{i} such that parent_{i}<i .

There is also a type associated with each parent relation, it can be either of type 11 or type 22 . Type 11 relation means that the child object is like a special case of the parent object. Type 22 relation means that the second object is always a part of the first object and all its special cases.

Note that if an object bb is a special case of object aa , and cc is a special case of object bb , then cc is considered to be a special case of object aa as well. The same holds for parts: if object bb is a part of aa , and object cc is a part of bb , then we say that object cc is a part of aa . Also note, that if object bb is a part of aa , and object cc is a special case of aa , then bb is a part of cc as well.

An object is considered to be neither a part of itself nor a special case of itself.

Now, Harry has to answer two type of queries:

  • 1 u v: he needs to tell if object vv is a special case of object uu .
  • 2 u v: he needs to tell if object vv is a part of object uu .

输入格式

First line of input contains the number nn ( 1<=n<=1051<=n<=10^{5} ), the number of objects.

Next nn lines contain two integer parentiparent_{i} and typeitype_{i} ( -1<=parent_{i}<i parenti0parent_{i}≠0 , 1<=typei<=1-1<=type_{i}<=1 ), implying that the ii -th object has the parent parentiparent_{i} . (If typei=0type_{i}=0 , this implies that the object ii is a special case of object parentiparent_{i} . If typei=1type_{i}=1 , this implies that the object ii is a part of object parentiparent_{i} ). In case the ii -th object has no parent, both parentiparent_{i} and typeitype_{i} are -1.

Next line contains an integer qq ( 1<=q<=1051<=q<=10^{5} ), the number of queries.

Next qq lines each represent a query having three space separated integers typei,ui,vitype_{i},u_{i},v_{i} ( 1<=typei<=2,1<=u,v<=n1<=type_{i}<=2,1<=u,v<=n ).

输出格式

Output will contain qq lines, each containing the answer for the corresponding query as "YES" (affirmative) or "NO" (without quotes).

You can output each letter in any case (upper or lower).

输入输出样例

  • 输入#1

    3
    -1 -1
    1 0
    2 0
    2
    1 1 3
    2 1 3
    

    输出#1

    YES
    NO
    
  • 输入#2

    3
    -1 -1
    1 0
    1 1
    2
    2 2 3
    2 3 2
    

    输出#2

    YES
    NO
    

说明/提示

In test case 11 , as object 22 is a special case of object 11 and object 33 is a special case of object 22 , this makes object 33 a special case of object 11 .

In test case 22 , as object 22 is a special case of object 11 and object 11 has object 33 , this will mean that object 22 will also have object 33 . This is because when a general case (object 11 ) has object 33 , its special case (object 22 ) will definitely have object 33 .

首页