`
Simone_chou
  • 浏览: 198107 次
  • 性别: 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;
}

 

 

分享到:
评论

相关推荐

    This demonstrates how to graph information from a database t

    在实际操作中,开发人员可能使用ADO.NET(对于VB.NET)或DAO(对于VB6)等数据访问技术来连接到Access数据库,执行SQL查询并获取结果集。然后,他们可能会使用图形库,如GDI+(在.NET Framework中)或第三方库,如...

    UG相关参数化设计培训教程.pdf

    用户可以使用Information Object (Object Dependency Graph)、Information Feature、Object Dependency Browser、Model Navigator、Information Expression List All by Reference和Tools Expression ...

    电子病历的检索和结果多样化算法研究.docx

    随后,通过EMRSearch算法,找到一个最优结果集。最后,应用SortDiversity算法对这些结果进行多样性的重排序,以更好地匹配用户的检索意图。 实验结果显示,这种基于图结构的检索和多样化排序方法能够显著提高用户...

    异构信息网上的可达性查询

    在当前信息技术高速发展的背景下,异构信息网络(Heterogeneous Information Networks)正逐步成为数据存储与管理的主要形式之一。异构信息网络可以被看作是由不同类型的节点和边构成的图(Graph),这些图能够模拟...

    计算机专业英语词汇加课后答案

    Range指定数据集的范围。Record在数据库中代表一个独立的数据项。Relational database基于关系理论,方便数据管理和查询。Report用于汇总和呈现数据。Ribbons是现代软件中取代传统菜单栏的设计,包含多个功能区。Row...

    Chatbot_CN - 基于金融-司法领域(兼有闲聊性质)的聊天机器人-python

    一、信息抽取(Information Extraction) 信息抽取是Chatbot_CN的核心组件之一,旨在从大量的文本数据中自动提取结构化信息。在金融和司法领域,这可能包括股票价格、法律条款、案件信息等。通过使用正则表达式、...

    最完整的Toad For Oracle使用手册

    - **Session Information**:提供了会话信息的查看方式。 - **Change Password**:指导用户如何更改密码。 - **Commit & Rollback**:讲解了提交和回滚的操作方法。 #### 十、故障诊断与优化 - **Diagnosing ...

Global site tag (gtag.js) - Google Analytics