Codeforces 466 E. Information Graph
2014-10-28 16:01
323 查看
并查集....
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
output
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
- Codeforces 916 C. Jamie and Interesting Graph (构造)
- Codeforces 466 A. Cheap Travel
- CodeForces - 505B Mr. Kitayuta's Colorful Graph(二维并查集)
- Codeforces 466 C. Number of Ways
- Codeforces 366D Dima and Trap Graph(搜索剪枝/二分)
- CodeForces 466 D.Increase Sequence(组合数学+dp)
- 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环
- Codeforces 466 B. Wonder Room
- Information Graph CodeForces - 466E
- Codeforces 459E. Pashmak and Graph (DP)
- Codeforces 295.B Greg and Graph
- Codeforces 366D Dima and Trap Graph (DFS剪枝水过...)
- Codeforces 405 E. Graph Cutting 【DFS 】
- Codeforces 505B - Mr. Kitayuta's Colorful Graph DFS或并查集
- [最长路][二分图]Codeforces 542 E. Playing on Graph
- 【codeforces 295B】Greg and Graph ( floyd )
- CodeForces - 724G:Xor-matic Number of the Graph
- Codeforces 505B - Mr. Kitayuta's Colorful Graph (DFS)
- CodeForces - 296D.Greg and Graph(Floyd+逆序思维)