1. 程式人生 > >洛谷 P2959 [USACO09OCT]悠閑漫步The Leisurely Stroll

洛谷 P2959 [USACO09OCT]悠閑漫步The Leisurely Stroll

targe traversal 表示 follow ras 離開 exist side sin

P2959 [USACO09OCT]悠閑漫步The Leisurely Stroll

題目描述

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.

思路:把樹標記一下深度,深度最深的就是答案。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 5001
using namespace std;
int n,m,num,tot,ans;
int vis[MAXN],deep[MAXN],dad[MAXN];
int to[MAXN*2],net[MAXN*2],head[MAXN*2];
void add(int u,int v){
    to[++tot]=v;net[tot]=head[u];head[u]=tot;
    to[++tot]=u;net[tot]=head[v];head[v]=tot;
}
void dfs(int now){
    deep[now]=deep[dad[now]]+1;
    for(int i=head[now];i;i=net[i])
        if(dad[now]!=to[i]){
            dad[to[i]]=now;
            dfs(to[i]);
        }
}
int main(){
    scanf("%d",&n);num=n;
    for(int i=1;i<n;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        if(y==0)    add(x,++num),vis[num]=1;
        else add(x,y);
        if(z==0)    add(x,++num),vis[num]=1;
        else add(x,z);
    }
    dfs(1);
    for(int i=n+1;i<=num;i++)
        ans=max(ans,deep[i]);
    cout<<ans-1;
} 

洛谷 P2959 [USACO09OCT]悠閑漫步The Leisurely Stroll