题目描述

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:


  1. % %
  2. / /
  3. 2----% 7----8----% 2----% 7####8----%
  4. / \ / \ # # # #
  5. 1 5----6 9----% 1 5####6 9----%
  6. \ \ \ \ \ \ \ #
  7. \ % % % \ % % %
  8. \ \
  9. 3-----% 3-----%
  10. \ \
  11. 4----% 4----%
  12. \ \
  13. % %

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: 复制

  1. 10
  2. 7 8 0
  3. 5 0 6
  4. 9 0 0
  5. 6 0 7
  6. 3 4 0
  7. 2 5 0
  8. 8 0 9
  9. 4 0 0
  10. 1 2 3
输出样例#1: 复制

  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.

思路:把树标记一下深度,深度最深的就是答案。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #define MAXN 5001
  6. using namespace std;
  7. int n,m,num,tot,ans;
  8. int vis[MAXN],deep[MAXN],dad[MAXN];
  9. int to[MAXN*],net[MAXN*],head[MAXN*];
  10. void add(int u,int v){
  11. to[++tot]=v;net[tot]=head[u];head[u]=tot;
  12. to[++tot]=u;net[tot]=head[v];head[v]=tot;
  13. }
  14. void dfs(int now){
  15. deep[now]=deep[dad[now]]+;
  16. for(int i=head[now];i;i=net[i])
  17. if(dad[now]!=to[i]){
  18. dad[to[i]]=now;
  19. dfs(to[i]);
  20. }
  21. }
  22. int main(){
  23. scanf("%d",&n);num=n;
  24. for(int i=;i<n;i++){
  25. int x,y,z;
  26. scanf("%d%d%d",&x,&y,&z);
  27. if(y==) add(x,++num),vis[num]=;
  28. else add(x,y);
  29. if(z==) add(x,++num),vis[num]=;
  30. else add(x,z);
  31. }
  32. dfs();
  33. for(int i=n+;i<=num;i++)
  34. ans=max(ans,deep[i]);
  35. cout<<ans-;
  36. }

洛谷 P2959 [USACO09OCT]悠闲漫步The Leisurely Stroll的更多相关文章

  1. 题解 洛谷P2959 【[USACO09OCT]悠闲漫步The Leisurely Stroll】

    原题:洛谷P2959 不得不说这道题的图有点吓人,但实际上很多都没有用 通过题上说的“三岔路口”(对于每一个节点有三条连接,其中一条连接父节点,另外两条连接子节点)和数据,可以那些乱七八糟的路和牧场看 ...

  2. 洛谷 P2376 [USACO09OCT]津贴Allowance 解题报告

    P2376 [USACO09OCT]津贴Allowance 题目描述 作为创造产奶纪录的回报,\(Farmer\) \(John\)决定开始每个星期给\(Bessie\)一点零花钱. \(FJ\)有一 ...

  3. 洛谷——P2958 [USACO09OCT]木瓜的丛林Papaya Jungle

    P2958 [USACO09OCT]木瓜的丛林Papaya Jungle 题目描述 Bessie has wandered off the farm into the adjoining farmer ...

  4. 洛谷—— P1339 [USACO09OCT]热浪Heat Wave

    P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texas are having a heatwave this summer. Their ...

  5. 洛谷 P2960 [USACO09OCT]Milkweed的入侵Invasion of the Milkweed

    P2960 [USACO09OCT]Milkweed的入侵Invasion of the Milkweed 题目描述 Farmer John has always done his best to k ...

  6. 洛谷 P2958 [USACO09OCT]木瓜的丛林Papaya Jungle

    P2958 [USACO09OCT]木瓜的丛林Papaya Jungle 题目描述 Bessie has wandered off the farm into the adjoining farmer ...

  7. 洛谷P1339 [USACO09OCT]热浪Heat Wave 题解

    题目传送门 这道题实际非常简单好奇是怎么变黄的... 其实也就是一个SPFA,本人非常懒,不想打邻接表,直接用矩阵就好啦... #include<bits/stdc++.h> using ...

  8. 洛谷 P2639 [USACO09OCT]Bessie的体重问题Bessie's We… 题解

    题目传送门 这也是个01背包,只是装的很... #include<bits/stdc++.h> #define MAXN 45010 using namespace std; int f[ ...

  9. 洛谷 2957 [USACO09OCT]谷仓里的回声Barn Echoes

    题目描述 The cows enjoy mooing at the barn because their moos echo back, although sometimes not complete ...

随机推荐

  1. Rails 拉数据初始数据库

    rails c   [1] pry(main)> Scraping.exec

  2. [Swift通天遁地]五、高级扩展-(1)快速检测设备属性:版本、类型、屏幕尺寸

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. 【知识总结】多项式全家桶(一)(NTT、加减乘除和求逆)

    我这种数学一窍不通的菜鸡终于开始学多项式全家桶了-- 必须要会的前置技能:FFT(不会?戳我:[知识总结]快速傅里叶变换(FFT)) 以下无特殊说明的情况下,多项式的长度指多项式最高次项的次数加\(1 ...

  4. flask中的蓝图(BluePrint)

    蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看 ...

  5. 记一次Oracle冷备恢复的过程

    一.故障来临 某日中午,市电意外中断,机房UPS电源由于负载过重而未接管供电,所有服务器全部重启...... 待所有服务器重启后,正在逐一检查设备和业务运行情况时,意外发生了.一台年代久远的HP PC ...

  6. 【Leetcode 166】 Fraction to Recurring Decimal

    Description: Given two integers representing the numerator and denominator of a fraction, return the ...

  7. CSS3悬浮动画效果

    利用CSS3的伪类元素hover以及transform,transition等动画属性,可以做出一些炫酷的动画效果.下面将一些项目中使用到的示例发布出来,供大家一起学习研究.演示地址:runjs. 浏 ...

  8. [Android]异常6-TextView setText延迟显示

    背景:Thread和Handler显示数据到界面 解决办法有: 解决一>界面使用了ListView.GridView等,把高度和宽度调整为固定值或者match_parent 解决二>某处U ...

  9. Canvas——基本入门

    基本概念 1.canvas 是 HTML5 提供的一个用于展示绘图效果的标签. canvas 原意画布, 帆布. 在 HTML 页面中用于展示绘图效果. 最早 canvas 是苹果提出的一个方案, 今 ...

  10. THREE.js代码备份——webgl - geometry - dynamic(模拟海浪,通过时间(毫秒)来控制平面点的运动模拟海浪,鼠标控制写在另外的js中)

    HTML: <!DOCTYPE html> <html lang="en"> <head> <title>three.js webg ...