2010年辽宁省赛 NBUT 1218【DFS实现树的遍历与更新】

    • [1218] You are my brother

    • 时间限制: 1000 ms 内存限制: 131072 K
    • 链接:NBUT 1218
    • 问题描述
    • Little
      A gets to know a new friend, Little B, recently. One day, they realize that they are family 500 years ago. Now, Little A wants to know whether Little B is his elder, younger or brother.

    • 输入
    • There are multiple test cases.

      For each test case, the first line has a single integer, n (n<=1000). The next n lines have two integers a and b (1<=a,b<=2000) each, indicating b is the father of a. One person has exactly one father, of course. Little A is numbered 1 and Little B is numbered
      2.

      Proceed to the end of file.

    • 输出
    • For each test case, if Little B is Little A’s younger, print “You are my younger”. Otherwise, if Little B is Little A’s elder, print “You are my elder”. Otherwise, print “You are my brother”. The output for each test case occupied exactly one line.
    • 样例输入
    • 5
      1 3
      2 4
      3 5
      4 6
      5 6
      6
      1 3
      2 4
      3 5
      4 6
      5 7
      6 7
      
    • 样例输出
    • You are my elder
      You are my brother

    题意:

    给定N次输入,每次输入一行a,b,表示b是a的父亲。最后求节点1与节点2的关系【即比较深度】

    N<=1000,1<=a,b<=2000

    分析:

    题意很明确,对于每个节点我都用一个值Rank保存该节点的深度,所以每次把节点a为根节点的树合并到节点b为根节点的树上面的时候,我要把从节点a以及节点a以下的节点全部的Rank值都加上节点b的Rank值。就这样,一个结构体保存着每个节点的信息,每次合并两棵树的时候DFS更新这些节点就OK了。

    代码实现:

    #include <cmath>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define FIN             freopen("input.txt","r",stdin)
    struct Node
    {
        int Rank;
        vector<int> Son;
    } Nodes[2000 + 5];
    void dfs(int pos, const int& val)
    {
        Nodes[pos].Rank += val;
        int num = Nodes[pos].Son.size();
        if(num == 0) return;
        for(int i = 0; i < num; i++)
        {
            int &np = Nodes[pos].Son[i];
            dfs(np, val);
        }
    }
    void init()
    {
        for(int i = 1; i <= 2000; i++)
        {
            Nodes[i].Rank = 1;
            Nodes[i].Son.clear();
        }
    }
    int main()
    {
    //    FIN;
        int n, a, b;
        while(~scanf("%d", &n))
        {
            init();
            for(int i = 1; i <= n; i++)
            {
                scanf("%d%d", &a, &b);
                Nodes[b].Son.push_back(a);
                dfs(a, Nodes[b].Rank);
            }
            if(Nodes[1].Rank == Nodes[2].Rank)
                printf("You are my brother\n");
            else if(Nodes[1].Rank > Nodes[2].Rank)
                printf("You are my elder\n");
            else
                printf("You are my younger\n");
    
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    时间: 2024-04-22 03:39:24

    2010年辽宁省赛 NBUT 1218【DFS实现树的遍历与更新】的相关文章

    2010辽宁省赛 NBUT 1222 English Game【字典树+DP】

    [1222] English Game 时间限制: 1000 ms 内存限制: 131072 K 链接:Click Here! 问题描述 This English game is a simple English words connection game. The rules are as follows: there are N English words in a dictionary, and every word has its own weight v. There is a wei

    [ACM]辽宁省赛2010 (HZNU 1081-1089)

    虽然退役了,但偶尔水几题醒醒脑还是不错的=_= 1085 Intermediary 暂时还没做 1081: Dinner 时间限制: 1 Sec  内存限制: 32 MB提交: 5  解决: 3[提交][状态][讨论版] [Edit] [TestData] 题目描述 Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he decided to invite all

    2014辽宁省赛 Traveling

    问题 K: Traveling 时间限制: 1 Sec  内存限制: 128 MB 提交: 13  解决: 4 [提交][状态][论坛] 题目描述 SH likes traveling around the world. When he arrives at a city, he will ask the staff about the number of cities that connected with this city directly. After traveling around 

    Go Deeper(2010成都现场赛题)(2-sat)

    G - Go Deeper Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Here is a procedure's pseudocode: go(int dep, int n, int m) begin output the value of dep. if dep < m and x[a[dep]] + x[b[dep]] != c[dep] then go(dep +

    Error Curves(2010成都现场赛题)

    F - Error Curves Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Josephina is a clever girl and addicted to Machine Learning recently. She pays much attention to a method called Linear Discriminant Analysis, which h

    辽宁省赛——杨鲁斯卡尔专场 -F

    题意: 题意就是输入N以EOF结尾,形成1-N的数字序列,然后选取一个幸运数字,每隔幸运数字的长度就将数字从序列中删掉, 选取幸运数字的规则是最开始是2,之后删掉数字之后每次选取序列中除1和选过的幸运数字外最小的.3<=n<=10000) 样例输入20 30 样例输出6 8 提示 eg.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 For the first time, delete the last number in every tw

    【bzoj1146】[CTSC2008]网络管理Network 倍增LCA+dfs序+树状数组+主席树

    题目描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构由N个路由器和N-1条高速光缆组成.每个部门都有一个专属的路由器,部门局域网内的所有机器都联向这个路由器,然后再通过这个通信子网与其他部门进行通信联络.该网络结构保证网络中的任意两个路由器之间都存在一条直接或间接路径以进行通信. 高速光缆的数据传输速度非常快,以至于利用光缆传输的延迟时间可以忽略.但是由于路由器老化,在这些

    BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状数组维护, DFS到的查询点就回答询问.时间复杂度O(|ACAM|+QlogQ) ------------------------------------------------------------------------------------------- #include<cstdio>

    【BZOJ】1146: [CTSC2008]网络管理Network(树链剖分+线段树套平衡树+二分 / dfs序+树状数组+主席树)

    第一种做法(时间太感人): 这题我真的逗了,调了一下午,疯狂造数据,始终找不到错. 后来发现自己sb了,更新那里没有打id,直接套上u了.我.... 调了一下午啊!一下午的时光啊!本来说好中午A掉去学习第二种做法,噗 好吧,现在第一种做法是hld+seg+bst+二分,常数巨大,log^4级别,目前只会这种. 树剖后仍然用线段树维护dfs序区间,然后在每个区间建一颗平衡树,我用treap,(这题找最大啊,,,囧,并且要注意,这里的rank是比他大的数量,so,我们在二分时判断要判断一个范围,即要