并查集....

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. LoadRunner测试下载功能点脚本(方法一)

    性能需求:对系统某页面中,点击下载功能做并发测试,以获取在并发下载文件的情况下系统的性能指标. 备注:页面上点击下载时的文件可以是word.excel.pdf等. 问题1:录制完下载的场景后,发现脚本 ...

  2. Canvas制作排序算法演示动画

    tips: 形象化演示排序算法可以让初学者快速理解,比较好的例子:jun-lu的SortAnimate,旧金山大学的David Galles教授的算法演示课件.最近在看canvas,试着用js+can ...

  3. EasyUI layout动态设置Split属性

    switch (rowData.ISREGISTERTASK) {            case 0: 显示north分割线                $('#RightContent').la ...

  4. AndroidAnnotations框架配置

    如今Android Studio的普及,Android开发基本要舍弃Eclipse了,最近使用AndroidAnnotations 注解框架的时候,找了些资料慢慢整出来了,在这给大家简单分享一下: 首 ...

  5. Proguard 保留native methods的问题

    发现一个奇怪的问题,如果使用下面的配置来keep的话,native的方法还是被删掉了,百思不得其解. -keepclasseswithmembers class * {     native *; } ...

  6. C++的学习记录 - 0

    最近玩Arduino发现,在编写函数库的时候要用到C++.正好手头有一本教材,于是时隔2年,开始重学. 又看到重载.构造.拷贝这些词竟然还有些小兴奋. 开个系列日志记录一下学习过程中的问题和体会. 看 ...

  7. pair/sort/find/qsort

    1. pair template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_t ...

  8. 关于HTML在手机端自适应的一个问题

    在写页面的时候 一直以为是自己调节的大小,结果页面跳出来的效果完全不适应手机的尺寸和宽度 其实主要是因为head头中没有放自适应标签导致:下面就让我们来认识一下这款神器吧! 其实主要就是改掉HTML页 ...

  9. Linux中重命名文件

    linux下重命名文件有两种方式: 1.较简单的处理命令:mv mv 原文件名 新文件名 如:mv myFile newName 将MyFile重命名为newName. 2.linux提供了一个重命名 ...

  10. SVN - 详细文档

    1.首先打开Cornerstone 2.然后如下图所示: 3.选择对应的仓库,如下图所示 4.然后Import完成之后,就把本地的文件提交到SVN服务器上了,如下图所示,另外如果你想要使用SVN进行版 ...