并查集....

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; const int maxn=100100; int n,m;
int fa[maxn],bef[maxn];
vector<int> v1,v2; int find(int x)
{
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
} int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n+10;i++) fa[i]=bef[i]=i;
int c,a,b;
while(m--)
{
scanf("%d",&c);
if(c==1)
{
scanf("%d%d",&a,&b);
bef[a]=b; fa[a]=b;
}
else if(c==2)
{
scanf("%d",&a);
v1.push_back(a);
v2.push_back(find(a));
}
else if(c==3)
{
scanf("%d%d",&a,&b);
b--;
int son=v1[b],father=v2[b];
bool flag=false;
while(true)
{
if(son==a)
{
flag=true;
break;
}
if(son==father) break;
son=bef[son];
}
if(flag) puts("YES");
else puts("NO");
}
}
return 0;
}

Codeforces 466 E. Information Graph的更多相关文章

  1. Codeforces 466E Information Graph

    Information Graph 把询问离线之后就能随便搞了, 去check一下是不是祖先, 可以用倍增也能用dfs序. #include<bits/stdc++.h> #define ...

  2. Codeforces 459E Pashmak and Graph(dp+贪婪)

    题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...

  3. ACM - 最短路 - CodeForces 295B Greg and Graph

    CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...

  4. CodeForces 466E Information Graph --树形转线性+并查集

    题意:有三种操作: 1.新增一条边从y连向x,此前x没有父节点 2.x接到一份文件,(文件标号逐次递增),然后将这份文件一路上溯,让所有上溯的节点都接到这份文件 3.查询某个节点x是否接到过文件F 解 ...

  5. codeforces gym100801 Problem G. Graph

    传送门:https://codeforces.com/gym/100801 题意: 给你一个DAG图,你最多可以进行k次操作,每次操作可以连一条有向边,问你经过连边操作后最小拓扑序的最大值是多少 题解 ...

  6. codeforces 21D:Traveling Graph

    Description You are given undirected weighted graph. Find the length of the shortest cycle which sta ...

  7. Codeforces 459E Pashmak and Graph

    http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...

  8. codeforces 340D Bubble Sort Graph(dp,LIS)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Bubble Sort Graph Iahub recently has lea ...

  9. 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法

    Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. C++11 lambda 表达式

    C++11 新增了很多特性,lambda 表达式是其中之一,如果你想了解的 C++11 完整特性,建议去这里,这里,这里,还有这里看看.本文作为 5 月的最后一篇博客,将介绍 C++11 的 lamb ...

  2. RAC 的一些概念性和原理性的知识(转)

    一 集群环境下的一些特殊问题 1.1 并发控制 在集群环境中, 关键数据通常是共享存放的,比如放在共享磁盘上. 而各个节点的对数据有相同的访问权限, 这时就必须有某种机制能够控制节点对数据的访问. O ...

  3. single-row function和muti-row function

    1.single-row function 指一行数据输入,返回一个值的函数. 常见的有 字符函数(如:substr) 日期函数(如:months_between) 数字函数(如:MOD) 转换函数( ...

  4. 常用aliyun公共资源列表

    公共DNS      223.5.5.5      223.6.6.6   源软件镜像站点      mirros.aliyun.com   NTP服务器 unix like      ntp1-7. ...

  5. ASP.net获取当前页面的文件名,参数,域名等方法

    ASP.net后台获取当前页面的文件名 System.IO.Path.GetFileName(Request.Path).ToString(); 获取当前页面文件名,参数,域名等方法 假设当前页完整地 ...

  6. pagefile.sys怎么删除

    pagefile.sys是虚拟内存文件,目地是使用一部分硬盘空间来充当内存使用.当一个程序请求的内存空间大于物理内存时,就需要pagefile.sys文件来提供较大的虚拟内存,从而满足程序对大内存的需 ...

  7. iOS的属性声明:retain和strong的区别

    声明属性时用strong或者retain效果是一样的(貌似更多开发者更倾向于用strong).不过在声明Block时,使用strong和retain会有截然不同的效果.strong会等于copy,而r ...

  8. PHP中PDO DEMO

    PDO => PHP DATABASE OBJECT 1.Select $dsn = "mysql:host=127.0.0.1;port=3306;dbname=dbname&quo ...

  9. Server.MapPath() 解析

    Server.MapPath获得的路径都是服务器上的物理路径,也就是常说的绝对路径 ./当前目录 /网站主目录 ../上层目录 ~/网站虚拟目录 1.Server.MapPath("/&qu ...

  10. linux上安装apache以及httpd.conf基本配置

    1.yum安装apache #yum install httpd -y 2.随系统自启动 #chkconfig httpd on 3.开启apache #service httpd start PS: ...