CF 題目集錦 PART 5 #266 div 2 E

【原題】

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:

  • either employee y became the boss of employee x (at that, employee x didn't have a boss before);
  • or employee x gets a packet of documents and signs them; then he gives the packet to his boss. The boss signs the documents and gives them to his boss and so on (the last person to sign the documents sends them to the archive);
  • or comes a request of type "determine whether employee x signs certain documents".

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).

  • If t = 1, then next follow two integers x and y (1 ≤ x, y ≤ n) — numbers of the company employees. It is guaranteed that employee x doesn't have the boss currently.
  • If t = 2, then next follow integer x (1 ≤ x ≤ n) — the number of the employee who got a document packet.
  • If t = 3, then next follow two integers x and i (1 ≤ x ≤ n; 1 ≤ i ≤ [number of packets that have already been given]) — the employee and the number of the document packet for which you need to find out information. The document packets are numbered started from 1 in the chronological order.

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 9
1 4 3
2 4
3 3 1
1 2 3
2 2
3 1 2
1 3 1
2 2
3 1 3
output
YES
NO
YES

【題意】意思看了半天~就是有N個人,M個操作。每次操作有3種。

①把x的父親置爲y。

②從x開始發新的一份文件(是從1開始標號的)。從x開始,一直傳遞到最遠的祖先。

③詢問x號文件有沒有落到y的手裏。

【分析】即要簡單地判斷y是否是x的祖先。一開始有思維定勢,覺得要求一遍LCA。但是後來想了想,可以直接用DFS序搞出同一棵樹的深度,再用並查集維護是否在同一棵樹上。代碼很簡單。

【代碼】

#include<cstdio>
#include<vector>
#define N 200005
#define pb push_back
using namespace std;
vector<int>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<son[k].size();i++)
    dfs(son[k][i]);
  R[k]=tot;
}
int main()
{
  scanf("%d%d",&n,&m);
  for (i=1;i<=m;i++)
  {
    scanf("%d%d",&a[i].opt,&a[i].x);a[i].id=i;
    if (a[i].opt!=2) scanf("%d",&a[i].y);
    if (a[i].opt==1) son[a[i].y].pb(a[i].x),fa[a[i].x]=1;
    if (a[i].opt==3) ask[a[i].y].pb(a[i].id);
  }
  for (i=1;i<=n;i++) if (!fa[i]) dfs(i);
  for (i=1;i<=n;i++) f[i]=i;now=0;
  for (i=1;i<=m;i++)
  {
    if ((P=a[i].opt)==1) {f[get(a[i].x)]=get(a[i].y);continue;}
    if (P==3) {puts(ans[i]?"YES":"NO");continue;}now++;
    for (j=0;j<ask[now].size();j++)
    {
      id=ask[now][j];num=a[id].x;
      if (get(num)==get(a[i].x)&&L[num]<=L[a[i].x]&&R[num]>=R[a[i].x]) ans[id]=1;
    }
  }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章