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 ...
随机推荐
- LR翻页脚本并在每页实现业务操作
性能需求:在列表中删除后有记录,或对列表中的每条记录进行操作(如点击每条记录的“单号”进入订单详情页面,或在列表中对每条记录进行“启用”.“停止”操作) 举例:Vuser脚本模拟用户在订单列表中点击每 ...
- mount USB Device(U disk) on crux based on vmware
1. 在 /mnt 下建立一个名叫USB的文件夹,文件夹名自定 cd /mnt mkdir USB 2. 查看一下磁盘分区情况 fdisk –l 3. 插入U盘 4. 再次查看磁盘分区情况,对比第一次 ...
- Android调试系列—使用android studio调试smali代码
1.工具介绍 使用工具 android killer:用于反编译apk包,得到smali代码 android studio:调试smali代码工具,或者使用idea,android studio就是在 ...
- 掌握jQuery插件开发,这篇文章就够了
---恢复内容开始--- 在实际开发工作中,总会碰到像滚动,分页,日历等展示效果的业务需求,对于接触过jQuery以及数据jQuery使用的人来说,首先想到的肯定是寻找现有的jQuery插件来满足形影 ...
- (转) [老老实实学WCF] 第三篇 在IIS中寄存服务
第三篇 在IIS中寄宿服务 通过前两篇的学习,我们了解了如何搭建一个最简单的WCF通信模型,包括定义和实现服务协定.配置服务.寄宿服务.通过添加服务引用的方式配置客户端并访问服务.我们对WCF的编程生 ...
- BootStrap-validator 使用记录(JAVA SpringMVC实现)
BootStrap 是一个强大的前面框架,它用优雅的方式解决了网页问题.最近正在使用其开发网站的表单验证,一点体会记录如下: 注:本文中借鉴了博客Franson 的文章<使用bootstrapv ...
- SQL中常用的时间格式
一些常用的时间格式 先讲一下一些基本的格式模式 格式模式 说明 d 月中的某一天.一位数的日期没有前导零. dd 月中的某 ...
- C#在局域网中连接Liunx上的MySql数据库
前期准备工作: 我所用的平台是VS2010和Ubuntu 14.04.3 LTS 一.由于MySql并没有集成在VS2010中所以要先安装MySQL Connector Net 6.9.8连接工具, ...
- silverlight visifire控件图表制作——silverlight 静态页面xaml
一.silverlight 静态页面 1. 时间控件:DatePicker ,添加引用: xmlns:sdk="clr-namespace:System.Windows.Controls;a ...
- jQuery和DOM对象
html示例 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=" ...