[ CodeVS冲杯之路 ] P1501

   不充钱,你怎么AC?

       题目:http://codevs.cn/problem/1501/

       水题一道

       直接dfs,记录上当前深度,到了叶子节点就更新答案,并且每个节点将当前深度的计数+1,答案即为max(所有深度的计数器)

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<algorithm>
 7 #define N 20
 8 using namespace std;
 9 
10 int n,l[N],r[N],ans=0,d[N];
11 void dfs(int x,int dep)
12 {
13     d[dep]++;
14     if (l[x]==0&&r[x]==0)
15     {
16         ans=max(ans,dep);
17         return;
18     }
19     if (l[x]!=0) dfs(l[x],dep+1);
20     if (r[x]!=0) dfs(r[x],dep+1);
21 }
22 int main()
23 {
24     int i,wid=0;
25     scanf("%d",&n);
26     for (i=1;i<=n;i++) scanf("%d%d",&l[i],&r[i]);
27     dfs(1,1);
28     for (i=1;i<=n;i++) wid=max(wid,d[i]);
29     printf("%d %d
",wid,ans);
30     return 0;
31 }
原文地址:https://www.cnblogs.com/hadilo/p/5859845.html