A11252 | Rowena Ravenclaw's Diadem
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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$ .
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$ .
输入格式
First line of input contains the number $n$ ( $1<=n<=10^{5}$ ), 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<=10^{5}$ ), 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$ ).
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<=10^{5}$ ), 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$ ).
输出格式
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).
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 $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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted