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 ...
随机推荐
- Linux的用户和组
1. 查看配置文件/etc/shadow第一行中root账号的第三个字段(以':'分隔)中的数字,请算一下这个数字是怎么来的?距离1970年1月1日到上次更改密码的时间的间隔天数.例如root密码日期 ...
- [.NET | 發佈] 如何指定固定的目錄給程式調用的外部DLL?
1.OverView 一般程式只會查找與主程式同目錄的DLL檔案 解決方案主要可以參考這篇:http://support.microsoft.com/kb/837908 2.實作app.config方 ...
- 让DIV垂直居中的几种办法
1.使用CSS3 的伸缩盒布局 <!doctype html> <html> <head> <meta charset="utf-8"&g ...
- Windows服务简单实例
1.定时器使用 partial class TimerService : ServiceBase { public TimerService() { InitializeComponent(); } ...
- (转).net开发者对android开发一周的学习体会
春节期间,相对比较闲,上班时也没什么事情做.利用这一周的时间,简单的学习了一下移动方面的开发.主要是针对android,其实我对IOS更感兴趣 (因为我用iphone),苦于暂时没有苹果电脑,只能把它 ...
- iOS图片拉伸技巧—— resizableImageWithCapInsets
http://blog.csdn.net/chaoyuan899/article/details/19811889
- JSP 实现 之 调用java方法实现MySQL数据库备份和恢复
package cn.qm.db; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOEx ...
- DOM 之selection
有关文章的集合 MOZILLA 开发者网络 selection: MOZILLA DEVELOPER NETWORK document.activeElement MOZILLA DEVELOPER ...
- MYSQL常用简单语句
使用SQL语法大写,增加可读性(小写部分就是自己数据库写的表/字段喽,具体你懂得...). 创建数据库:CREATE DATABASE mysql_db;删除数据库:DROP DATABASE mys ...
- C# 数据实现设计模式
一个人没事,写了一个底层数据实现读取设计模式,个人觉得还是蛮好扩展,里面有不足的地方希望大家给予指导.话不多说先看个图吧!图可能不正规,伤害了你的眼睛见谅.有图有真相 其实这个设计模式,就是一个简单的 ...