Codeforces 466 E. Information Graph
并查集....
1 second
512 megabytes
standard input
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.
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.
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.
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
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的更多相关文章
- Codeforces 466E Information Graph
Information Graph 把询问离线之后就能随便搞了, 去check一下是不是祖先, 可以用倍增也能用dfs序. #include<bits/stdc++.h> #define ...
- Codeforces 459E Pashmak and Graph(dp+贪婪)
题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边依 ...
- ACM - 最短路 - CodeForces 295B Greg and Graph
CodeForces 295B Greg and Graph 题解 \(Floyd\) 算法是一种基于动态规划的算法,以此题为例介绍最短路算法中的 \(Floyd\) 算法. 我们考虑给定一个图,要找 ...
- CodeForces 466E Information Graph --树形转线性+并查集
题意:有三种操作: 1.新增一条边从y连向x,此前x没有父节点 2.x接到一份文件,(文件标号逐次递增),然后将这份文件一路上溯,让所有上溯的节点都接到这份文件 3.查询某个节点x是否接到过文件F 解 ...
- codeforces gym100801 Problem G. Graph
传送门:https://codeforces.com/gym/100801 题意: 给你一个DAG图,你最多可以进行k次操作,每次操作可以连一条有向边,问你经过连边操作后最小拓扑序的最大值是多少 题解 ...
- codeforces 21D:Traveling Graph
Description You are given undirected weighted graph. Find the length of the shortest cycle which sta ...
- Codeforces 459E Pashmak and Graph
http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...
- codeforces 340D Bubble Sort Graph(dp,LIS)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Bubble Sort Graph Iahub recently has lea ...
- 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法
Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...
随机推荐
- Mysql 细节记忆
DELIMITER $$ 和 DELIMITER ; DROP PROCEDURE IF EXISTS `pro_follow_getBookBeforeExpired`$$ DECLARE p_Se ...
- Javascript基础Function
函数声明与表达式 function someFunc(){ alert("这是一个函数"); } var func=function(){ alert("函数表达式&qu ...
- angular 指令梳理 —— 前端校验
angular js内置校验的扩展 校验成功则 scope.formName.$valid=true 校验失败 元素的class: ng-invalid 成功:.ng-valid /** * 校验指 ...
- C#中子窗体获取父窗体中控件的内容
今天在做一个联系人管理的C#设计时,遇到了这个问题,我需要将父窗体中的textBox中的值传到子窗体并进行数据库查询操作,我用了new 父窗体().textBox.text;来进行值传递,然而并无卵用 ...
- 继刚接触play framework后,一些心得
我是个小菜鸟,我这些体会跟心得纯属个人观点,仅供参考,勿喷,我想记录下学习的历程,不断成长 在play2.0的框架里面 用到的最多的语言就是scala,对于习惯了java语言的我们来说 看这些语言 ...
- lnmp安装fileinfo扩展
1.错误: PHP Fileinfo extension must be installed/enabled to use Intervention Image. 2.原因: 缺少 fileinfo扩 ...
- Maven+SpringMVC+Mybatis 开发环境整合
1.maven build遇到了如下问题: [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:rede ...
- C语言函数指针变量和指针函数以及指针数组
C语言中,一个函数总是占用一段连续的内存区,而函数名就是该函数所占内存区的首地址.我们可以把函数的这个首地址(或称入口地址)赋予一个指针变量,使该指针变量指向该函数.然后通过指针变量就可以找到并调用这 ...
- hadoop学习之hadoop完全分布式集群安装
注:本文的主要目的是为了记录自己的学习过程,也方便与大家做交流.转载请注明来自: http://blog.csdn.net/ab198604/article/details/8250461 要想深入的 ...
- AutoCompleteTextView 与sqlite绑定实现记住用户输入的内容并自动提示
把用户输入的内容保存到数据库表中,然后用户输入时,进行模糊查询并把查询结果附到AutoCompleteTextView中. 1:activity_main.xml <LinearLayout x ...