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 x doesn'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
题意:
给出 N 个结点和 M 个事件,1 代表 x 是 y 的 boss,2 代表从 x 开始往上传文件,3 代表询问第 x 个人有没有收到过这个文件。每次往上传文件都是传到最高的 boss 为止。输出每次询问的结果,有则输出 YES, 没有则输出 NO。
思路:
并查集。维护两个并查集,一个是更新的,一个是不更新的,并且记录每次文件从哪里开始到哪里结束。最后询问的时候,查找不更新的并查集即可。
AC:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int NMAX = 100005; int root[NMAX], num[NMAX]; int f[NMAX], tt[NMAX]; int Find (int x) { return x == root[x] ? x : root[x] = Find(root[x]); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { root[i] = num[i] = i; } int ans = 0; while (m--) { int t; scanf("%d", &t); if (t == 1) { int x, y; scanf("%d%d", &x, &y); num[x] = root[x] = y; } else if (t == 2) { int x; scanf("%d", &x); ++ans; f[ans] = x; tt[ans] = Find(x); } else if (t == 3) { int x, i; scanf("%d%d", &x, &i); bool temp = false; int k = f[i]; if (k == x || x == tt[i]) temp = true; while (!temp && k != tt[i]) { if (k == x) { temp = true; break; } else k = num[k]; } if (temp) printf("YES\n"); else printf("NO\n"); } } return 0; }
相关推荐
网络(图 graph)数据集CollegeMsg网络(图 graph)数据集CollegeMsg网络(图 graph)数据集CollegeMsg网络(图 graph)数据集CollegeMsg网络(图 graph)数据集CollegeMsg网络(图 graph)数据集CollegeMsg网络(图...
Graphis
Like many other disciplines, the fields of natural language processing (NLP) and information retrieval (IR) also deal with data that can be represented as a graph. In this light, it is somewhat ...
MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13...
the dynamic information as the graph evolving. In particular, the proposed framework can keep updating node information by capturing the sequential information of edges (interactions), the time ...
Graph
It converts the graph data into a low dimensional space in which the graph structural information and graph properties are maximumly preserved. In this survey, we conduct a comprehensive review of ...
janusgraph可视化 janusgraph可视化的简单工具。 (一个简单的janusgraph查询可视化工具) 使用工具查看janusgraph。 怎么跑? 下载发布jar文件 nohup java -jar janusgraph-visualization-x.x.x.jar >/dev/null ...
However, these tasks require dealing with non-Euclidean graph data that contains rich relational information between elements and cannot be well handled by traditional deep learning models (e.g., ...
graph类型数据窗口改变graph类型 通过鼠标显示数值
graph说明书,西门子语言教程,大家学习产考 graph说明书,西门子语言教程,大家学习产考 graph说明书,西门子语言教程,大家学习产考
ArangoDB vs. JanusGraph vs. Neo4j vs. OrientDB vs. TigerGraph System Properties Comparison ArangoDB vs. JanusGraph vs. Neo4j vs. OrientDB vs. TigerGraph
S7-Graph汉化,测试过,好用。复制替换到s7graph文件中即可。
PB中嵌入Microsoft Graph97
基于子图的推荐系统,融合异构信息网络Meta-Graph Based Recommendation Fusion over Heterogeneous Information Networks
SIMATIC_S7_GRAPH_V56.exe格式;SIMATIC_S7_GRAPH_V56.exe格式;SIMATIC_S7_GRAPH_V56.exe格式
Arduino项目开发 Communication_Graph_Graph.pdf Arduino项目开发 Communication_Graph_Graph.pdf Arduino项目开发 Communication_Graph_Graph.pdf Arduino项目开发 Communication_Graph_Graph.pdf Arduino项目开发 ...
西门子GRAPH帮助手册
CCS中的graph详细使用说明CCS中的graph详细使用说明
visual graph入门教程visual graph入门教程visual graph入门教程visual graph入门教程visual graph入门教程