悠闲的漫步

悠闲的漫步

题目链接:http://acm.ocrosoft.com/problem.php?cid=1616&pid=9

题目描述:

Bessie looks out the barn door at the beautiful spring day and thinks to herself, 'I'd really like to enjoy my walk out to the pastures for the tender spring grass.' She knows that once she leaves the barn, she will traverse a path for a while, take one of two choices that lead to other paths, follow one of them, take one of two other choices, and continue on until the path leads to a verdant pasture.

She decides to make the set of path choices that enables her to walk over the greatest number of cow paths on her way to breakfast. Given the description of these paths, determine how many cow paths she traverses, presuming that she commences choosing various paths as soon as she leaves the barn.

The farm has P (1 <= P <= 1,000) pastures that are lead to by P-1 choice-nodes (range 1..P-1) connected by paths. From the barn (which is node 1), only one set of path traversals exists to reach any choice-node or pasture.

Consider this set of paths (lines), pastures ('%'), and the highlighted ('#') route to a pasture on the right:

 

                 %                             %

                /                             /

      2----%   7----8----%          2----%   7####8----%

     / \      /      \             # #      #      #

    1   5----6        9----%      1   5####6        9----%

     \   \    \        \           \   \    \        #

      \   %    %        %           \   %    %        %

       \                             \

        3-----%                       3-----%

         \                             \

          4----%                        4----%

           \                             \

            %                             %

The pasture reached from choice-node 9 is one of two that enable Bessie to traverse seven different cowpaths on the way to breakfast. These are the 'furthest' pastures from node 1, the barn.

Three integers describe each node: Cn, D1, and D2. Cn is the

nodenumber (1 <= Cn <= P-1); D1 and D2 are the destinations from that node (0 <= D1 <= P-1; 0 <= D2 <= P-1). If D1 is 0, the node leads to a pasture in that direction; D2 has the same property.

POINTS: 100

Bessie透过牛棚的大门向外望去。发现今天是一个美丽的春季早晨。她想,“我真的好想好想沐浴着春风,走在草地之中,感受嫩草温柔地抚摸四蹄地的感觉。”她知道一旦她离开了牛棚,她将沿着一条小径走一段路,然后就会出现一个三岔路口,她必须在两条小径中选择一条继续走下去。然后她又会遇到更多的三岔路口,进行更多的选择,知道她到达一个青翠的牧场为止。

她决定作一个选择使得她在去吃早草的路途中可以走过最多的小径。给你这些小径的描述,求出Bessie最多可以走过多少条小径。假定Bessie一出牛棚就有2条路径,Bessie需要从中选择一条。

农场中有P-1 (1 <= P <= 1,000) 个分岔节点(范围是1..P),引向P片草地,它们之间由小径连接。对任意一个节点来说,只有一条从牛棚(被标记为节点1)开始的路径可以到达。

考虑下面的图。线段表示小径,"%"表示草地。右边的图中的"#"表示一条到达草地的高亮的路径。

从分岔节点9到达的草地是两个可以让Bessie走过最多小径的草地之一。在去吃早草的路上Bessie将走过7条不同的小径。这些草地是离牛棚也就是节点1最“远”的。

由3个整数来表示每一个节点:Cn, D1和D2,Cn是节点的编号(1 <= Cn <= P-1); D1和D2是由该节点引出的两条小径的终点(0 <= D1 <= P-1; 0 <= D2 <= P-1)。如果D1为0,表示这条小径引向的是一片牧草地;D2也一样。

输入输出格式

输入格式:
 

* Line 1: A single integer: P

* Lines 2..P: Line i+1 contains three space-separated integeres that describe a choice-node: Cn, D1, and D2

输出格式:
 

* Line 1: A single integer that is the largest number of paths Bessie can traverse on the way to the furthest pasture.

输入输出样例

输入样例#1: 

10

7 8 0

5 0 6

9 0 0

6 0 7

3 4 0

2 5 0

8 0 9

4 0 0

1 2 3

输出样例#1: 

7

说明

This input describes the example farm layout in the task description.

1-2-5-6-7-8-9-P is one of the longest routes.

思路:

刚拿到这道题,感觉好难啊,这个搜索的地图怎么建都不知道。后来发现,这道题,用并查集的思想,加上简单的DFS,就很简单了!!

 

代码:

#include<bits/stdc++.h>

using namespace std;

#define maxn 1005

int l[maxn], r[maxn], x, maxx = 0;

void dfs(int xx, int sum)

{

         if (l[xx] != 0)dfs(l[xx], sum + 1);//没到终点,往左岔路搜索

         if (r[xx] != 0)dfs(r[xx], sum + 1);//没到终点,往右岔路搜索

         maxx = max(maxx, sum);//每次刷新记录下最大值

}

int main()

{

         int n;

         cin >> n;

         for (int i = 1; i < n; i++)

         {

                  cin >> x;

                  cin >> l[x] >> r[x];//每个岔路点通向的地方

         }

         dfs(1, 1);//深度搜索

         cout << maxx << endl;

}

 

 

 

 

全部评论

相关推荐

点赞 评论 收藏
分享
暑期是进不了大厂了想问问前端友友们&nbsp;,后面应该如何沉淀自己,我想秋招再冲一下尤其是八股,应该抓哪一块是重点,理解到什么程度呢,要学到什么深度才能抗住拷打。还有场景题如何去准备。期待友友们的解答。
命烈焰带我飞走:找个中厂小厂先看看吧,去了熟悉熟悉项目,简历上扒点东西,之后刷刷sobb上百度美团快手的日常实习,流程都比较快轮次也少,别给自己太大压力,一步一步来,先不用想着暑期,转正,秋招那些事情,另外如果可能的话可以关注下面试时候的形象,穿搭,环境这些,其实实习主要就是看个眼缘,看着好看声音好听其实加分不少..八股这些不要死记硬背,挨个拿去问问chatgpt,这个东西做出来是为了解决什么问题,有啥效果,自己有想法有个模糊的概念就可以了,人家也知道你是学生,实习生没有什么kpi,放你去面都是希望能把你招进去的,场景题算法题没做过你可以边试着写边跟面试官说你的想法思路,也可以直说没见过让他们给你提示,反正最后都是与或非顺序分支循环存取值那套。总之建议是别为了秋招..出去旅旅游放松放松,少投几家少背八股多写写代码
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务