`
Simone_chou
  • 浏览: 195692 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Information Graph(并查集)

    博客分类:
  • CF
 
阅读更多
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 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.

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

 

      题意:

      给出 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网络(图 graph)数据集CollegeMsg网络(图...

    Graphis

    Graphis,这个名称可能对许多人来说并不陌生,它代表着一个专注于平面设计、字体设计和摄影的权威平台。本文将围绕Graphis这一主题,深入探讨字体设计的艺术与技术,旨在揭示其背后的创作理念与实用技巧。 首先,让...

    西门子GRAPH帮助手册

    通过在GRAPH中准确设定报警,系统可以实时监控运行状态,并在出现异常时及时给出警告。 ### GRAPH编程窗口和编程结构 GRAPH帮助手册详细介绍了GRAPH的编程窗口,这是编程人员与GRAPH编程语言交互的界面。编程窗口...

    Janusgraph分布式环境部署文档

    5. **Hbase配置**:Janusgraph可以使用Hbase作为持久化存储,需要在Hbase中创建Janusgraph所需的表,并配置相应的Janusgraph-Hbase连接参数。 6. **Elasticsearch配置**:Elasticsearch作为Janusgraph的索引存储,...

    S7-Graph汉化

    《S7-Graph汉化详解》 在工业自动化领域,西门子的S7-Graph是一款广泛应用的编程工具,主要用于创建和编辑S7系列PLC(可编程逻辑控制器)的顺序控制程序。S7-Graph以其图形化界面和强大的功能,使得复杂的顺序逻辑...

    MFC源代码 GRAPH.13

    MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13MFC源代码 GRAPH.13...

    Streaming Graph Neural Networks.pdf

    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 ...

    java算法 Graph.java

    Graph

    1500 GRAPH 指令用法.pdf

    本文档详细介绍了 S7-1500 GRAPH 指令的使用方法,包括 GRAPH 顺控器、GRAPH 操作、GRAPH LAD 指令、GRAPH FBD 指令等内容。 GRAPH 顺控器是 S7-1500 程序设计中的一个重要组件,它可以用于控制程序的执行顺序。 ...

    GraphSLAM 个人习作

    在GraphSLAM中,随着机器人移动并收集新的观测数据,图会不断扩展。每次新增一个观测时,都会添加一个新的节点和边。节点代表当前的位姿估计,边则表示新观测与已有信息之间的关系。例如,激光雷达或视觉传感器的...

    pb graph显示数值

    在PowerBuilder(PB)开发环境中,`pb graph显示数值`是一个关键功能,它涉及到如何在图形图表(graph)上清晰地展示数据值。这通常是为了提高数据可视化的可读性和理解性。`graph类型数据窗口改变graph类型`指的是...

    s7 graph v5.5

    S7-GRAPH允许用户定义步的构成及属性,并根据预设的条件执行相应的动作(action)。动作可以被分类,比如将动作分为带有或不带有输出的操作。 10. 特有地址 S7-GRAPH使用了某些特定的地址,这些地址是该软件特有的...

    刘知远-Introduction to Graph Neural Networks.pdf

    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 Embedding Survey

    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-visualization:一个简单的janusgraph查询工具(一个简单的janusgraph查询可视化工具)

    janusgraph可视化 janusgraph可视化的简单工具。 (一个简单的janusgraph查询可视化工具) 使用工具查看janusgraph。 怎么跑? 下载发布jar文件 nohup java -jar janusgraph-visualization-x.x.x.jar &gt;/dev/null ...

    S7-Graph V5.3

    安装过程中,需确保计算机满足软件的硬件需求,并遵循官方提供的安装指南。同时,对于注册机的使用,虽然可以绕过正版验证,但应理解这是不合法的行为,可能会导致法律问题和软件不稳定的风险。 总的来说,S7-Graph...

    GraphDB Linux 安装包

    GraphDB Linux 安装包

    STemwin之GRAPH的使用

    1. **创建GRAPH控件并设置属性**:使用`GRAPH_CreateEx()`函数创建控件,并指定初始位置、大小、父窗口等信息。 2. **创建数据对象**:使用`GRAPH_DATA_YT_Create()`或`GRAPH_DATA_XY_Create()`函数创建数据对象。...

    graphX 基本介绍

    2. **顶点和边的表示**:GraphX中的顶点和边都是通过RDD(弹性分布式数据集)来表示的,这些RDD包含了图中所有顶点或边的信息。每个顶点都有一个唯一的ID,称为`VertexId`。 3. **分区策略**:GraphX采用哈希函数对...

    AssetGraph插件unity资源打包

    AssetGraph的核心理念在于通过图形化的方式管理游戏中的资源,包括模型、纹理、音频、脚本等,并自定义打包逻辑。这有助于减少加载时间和内存占用,提高游戏性能。以下是一些关键知识点: 1. **资源依赖关系**:...

Global site tag (gtag.js) - Google Analytics