题解 | TheLeisurelyStroll-牛客假日团队

The Leisurely Stroll

https://ac.nowcoder.com/acm/contest/1082/G

题目描述

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:

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

输入描述:

* 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 
输出
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开始的路径可以到达” ,所以可以把1作为根节点。从而将题目转化为求一棵以1为节点的二叉树的深度

核心算法:DFS

注意:

1.根节点的深度在此题中应该为1(节点1的实际意义是一个要算入答案的一个牧场)

2.有n个节点,他们之间的连接数应该为n-1

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<cmath>
typedef long long ll;
using namespace std;
struct Node{
    int id,l,r;
    Node(){
        l=r=0;
    }
}tr[1010];//保存二叉树
int p,dep[1010],ans=0;//个数,节点深度,答案(树的深度)
void addedge(int i,int r,int l){
    tr[i].id=i;
    tr[i].l=l;
    tr[i].r=r;
}//编号为i的接点的左节点l,右节点r
void Init() {
    scanf("%d",&p);
    int a,b,c;
    for(int i=1;i<p;i++){
        scanf("%d%d%d",&a,&b,&c);
        addedge(a,b,c);
    }
    dep[1]=1;//注意初始化
}
void DFS(int i){
    if(tr[i].l){//如果左节点不为0
        dep[tr[i].l]=dep[i]+1;//左节点的深度
        ans=max(ans,dep[i]+1);//更新树的深度
        DFS(tr[i].l);//接着左节点向下找
    }
    if(tr[i].r){//同理
        dep[tr[i].r]=dep[i]+1;
        ans=max(ans,dep[i]+1);
        DFS(tr[i].r);
    }
}
int main() {
    Init();
    DFS(1);
    printf("%d",ans);
    return 0;
}


来源:TPa°的茶会
全部评论

相关推荐

LazyBreeze:项目尽量体现你对技术的理解和深度,不是说把中间件用一下就完事了,你项目里面提到集群和分布式,你真在服务器上部署过吗,感觉太假了,第二个项目说自己用了微服务的什么组件,只是用了没有自己的思考,很难让面试官注意到你的简历。针对某几个技术点自己多思考一下,考虑一下有没有别的替代方案,可以写一下,即使没有真的实现
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务