Codeforces Round #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 xdoesn'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

题意:RT

思路:离线做就可以了

            这里我写了两种写法,第一种是用并查集判断两个结点是否具有父子关系

            第二种写法是看了tourist的神写法,用树状数组判断两个结点的父子关系 Orz...

            感觉这题不好讲,具体看代码吧,写的比较清楚

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; const int MAXN = 100010; int fa[MAXN]; int x[MAXN]; int y[MAXN]; int res[MAXN]; int t[MAXN]; int vin[MAXN]; int vout[MAXN]; int sz; int n,m; vector 
        
          head[MAXN]; vector 
         
           q[MAXN]; vector 
          
            doc; int mfind(int x) { if(x==fa[x])return x; else return fa[x]=mfind(fa[x]); } void dfs(int u) { vin[u]=++sz; int l=head[u].size(); for(int i=0;i 
           
             =vin[en] && vout[st]<=vout[en]){ if(mfind(st)==mfind(en)){ res[q[i][j]]=1; } } } } else { res[i]==1 ? printf("YES\n") : printf("NO\n") ; } } return 0; } 
            
           
          
         
       
      
      
     
     
    
    
   
   

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; const int MAXN = 100010; int fa[MAXN]; int x[MAXN]; int y[MAXN]; int res[MAXN]; int t[MAXN]; int depth[MAXN]; int vin[MAXN]; int vout[MAXN]; int sum[MAXN]; int sz; int n,m; vector 
        
          head[MAXN]; vector 
         
           q[MAXN]; vector 
          
            doc; void addbit(int x,int v) { while(x<=n){ sum[x]+=v; x += (x&(-x)); } } int getbit(int x) { int res=0; while(x>=1){ res+=sum[x]; x -= (x&(-x)); } return res; } void dfs(int u) { vin[u]=++sz; int l=head[u].size(); for(int i=0;i 
           
             =vin[en] && vout[st]<=vout[en]){ if(getbit(vin[st])-getbit(vin[en]) == depth[st]-depth[en]){ res[q[i][j]]=1; } } } } else { res[i]==1 ? printf("YES\n") : printf("NO\n") ; } } return 0; } 
            
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值