CF855D.Rowena Ravenclaw's Diadem
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
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 n objects, numbered from 1 to n. Some of the objects have a parent object, that has a lesser number. Formally, object i may have a parent object parent__i such that parent__i < i.
There is also a type associated with each parent relation, it can be either of type 1 or type 2. Type 1 relation means that the child object is like a special case of the parent object. Type 2 relation means that the second object is always a part of the first object and all its special cases.
Note that if an object b is a special case of object a, and c is a special case of object b, then c is considered to be a special case of object a as well. The same holds for parts: if object b is a part of a, and object c is a part of b, then we say that object c is a part of a. Also note, that if object b is a part of a, and object c is a special case of a, then b is a part of c 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 v is a special case of object u.
- 2 u v: he needs to tell if object v is a part of object u.
哈利向拉文克劳的幽灵海伦娜打听后得知,她曾将罗伊纳·拉文克劳的冠冕一事告诉了汤姆·里德尔(即“那个不能提名字的人”),而里德尔随后便从她那里偷走了这件物品。
哈利推测,里德尔会认为自己是唯一发现有求必应屋的人,因此很可能将冠冕藏匿于该房间内。于是,哈利试图进入有求必应屋,以摧毁这件魂器——他知道这顶冠冕正是魂器之一。
但他必须先解开一道谜题才能进入房间。他被给予 $ n $ 个编号为 $ 1 $ 到 $ n $ 的物体。其中部分物体拥有一个父物体,其编号比自身小。形式化地,物体 $ i $ 可能拥有一个父物体 $ \text{parent}_i $,满足 $ \text{parent}_i < i $。
此外,每条父子关系还关联一种类型,该类型只能是类型 1 或类型 2:
- 类型 1 关系表示子物体是父物体的一个特例;
- 类型 2 关系表示第二个物体(即子物体)始终是第一个物体(即父物体)及其所有特例的一部分。
注意:若物体 $ b $ 是物体 $ a $ 的一个特例,且物体 $ c $ 是物体 $ b $ 的一个特例,则 $ c $ 也被视为物体 $ a $ 的一个特例。同理,对于“组成部分”关系也成立:若物体 $ b $ 是 $ a $ 的一部分,且物体 $ c $ 是 $ b $ 的一部分,则称物体 $ c $ 是 $ a $ 的一部分。此外还需注意:若物体 $ b $ 是 $ a $ 的一部分,且物体 $ c $ 是 $ a $ 的一个特例,则 $ b $ 同样是 $ c $ 的一部分。
一个物体既不被视为自身的一部分,也不被视为自身的特例。
现在,哈利需回答两类查询:
1 u v:判断物体 $ v $ 是否为物体 $ u $ 的一个特例;2 u v:判断物体 $ v $ 是否为物体 $ u $ 的一部分。
输入格式
First line of input contains the number n (1 ≤ n ≤ 105), the number of objects.
Next n lines contain two integer parent__i and type__i ( - 1 ≤ parent__i < i parent__i ≠ 0, - 1 ≤ type__i ≤ 1), implying that the i-th object has the parent parent__i. (If type__i = 0, this implies that the object i is a special case of object parent__i. If type__i = 1, this implies that the object i is a part of object parent__i). In case the i-th object has no parent, both parent__i and type__i are -1.
Next line contains an integer q (1 ≤ q ≤ 105), the number of queries.
Next q lines each represent a query having three space separated integers type__i, u__i, v__i (1 ≤ type__i ≤ 2, 1 ≤ u, v ≤ n).
输入的第一行包含一个整数 n(1≤n≤105),表示对象的数量。
接下来的 n 行,每行包含两个整数 parenti 和 typei(−1≤parenti<i,parenti=0,−1≤typei≤1),表示第 i 个对象的父对象为 parenti。(若 typei=0,表示对象 i 是对象 parenti 的一种特例;若 typei=1,表示对象 i 是对象 parenti 的一个组成部分)。若第 i 个对象没有父对象,则 parenti 和 typei 均为 −1。
下一行包含一个整数 q(1≤q≤105),表示查询的数量。
接下来的 q 行,每行表示一个查询,包含三个以空格分隔的整数 typei, ui, vi(1≤typei≤2,1≤u, v≤n)。
输出格式
Output will contain q 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).
输出包含 q 行,每行对应一个查询的答案,为 "YES"(肯定)或 "NO"(不带引号)。
每个字母可输出为大写或小写。
输入输出样例
输入#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 1, as object 2 is a special case of object 1 and object 3 is a special case of object 2, this makes object 3 a special case of object 1.
In test case 2, as object 2 is a special case of object 1 and object 1 has object 3, this will mean that object 2 will also have object 3. This is because when a general case (object 1) has object 3, its special case (object 2) will definitely have object 3.
在测试用例 1 中,由于对象 2 是对象 1 的特例,且对象 3 是对象 2 的特例,因此对象 3 也是对象 1 的特例。
在测试用例 2 中,由于对象 2 是对象 1 的特例,且对象 1 拥有对象 3,这意味着对象 2 也拥有对象 3。这是因为当一个一般情况(对象 1)拥有对象 3 时,其特例(对象 2)必定也拥有对象 3。
输入解题思路,AI测评打分。不知道怎么写?