CF 题目集锦 PART 5 #266 div 2 E_html/css_WEB-ITnose
E. Information Graph
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output
There are n employees working in company "X" (let's number them from 1 to n for convenience). Initially the employees didn't have any relationships among each other. On each of m next days one of the following events took place:
Your task is to write a program that will, given the events, answer the queries of the described type. At that, it is guaranteed that throughout the whole working time the company didn't have cyclic dependencies.
Input
The first line contains two integers n and m (1?≤?n,?m?≤?105) ? the number of employees and the number of events.
Each of the next m lines contains the description of one event (the events are given in the chronological order). The first number of the line determines the type of event t (1?≤?t?≤?3).
It is guaranteed that the input has at least one query of the third type.
Output
For each query of the third type print "YES" if the employee signed the document package and "NO" otherwise. Print all the words without the quotes.
Sample test(s)
input
4 91 4 32 43 3 11 2 32 23 1 21 3 12 23 1 3
output
YESNOYES
【题意】意思看了半天~就是有N个人,M个操作。每次操作有3种。
①把x的父亲置为y。
②从x开始发新的一份文件(是从1开始标号的)。从x开始,一直传递到最远的祖先。
③询问x号文件有没有落到y的手里。
【分析】即要简单地判断y是否是x的祖先。一开始有思维定势,觉得要求一遍LCA。但是后来想了想,可以直接用DFS序搞出同一棵树的深度,再用并查集维护是否在同一棵树上。代码很简单。
【代码】
#include#include #define N 200005#define pb push_backusing namespace std;vector son[N],ask[N];struct arr{int x,y,id,opt;}a[N];int f[N],L[N],R[N],fa[N],ans[N];int i,n,m,now,num,id,tot,P,j;inline int get(int u){return f[u]==u?f[u]:f[u]=get(f[u]);}void dfs(int k){ L[k]=++tot; for (int i=0;i =R[a[i].x]) ans[id]=1; } }}