码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3805 Machine

时间:2014-08-24 19:17:32      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   ar   art   

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337

bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 vector<int>g[1001100];
 8 int a[100101];
 9 int in[100101];
10 int n;
11 int dfs(int u)
12 {
13     int c,x;
14     if(in[u]==0) return 1;
15     if(in[u]==1)
16     {
17         c=dfs(g[u][0]);
18         return c;
19     }
20     else
21     {
22         c=dfs(g[u][0]);
23         x=dfs(g[u][1]);
24         if(c!=x) return max(x,c);
25         else return c+1;
26     }
27 
28 }
29 
30 int main()
31 {
32    while(scanf("%d",&n)!=EOF)
33    {
34        for(int i=1; i<=n; i++)
35        {
36            g[i].clear();
37        }
38        memset(in,0,sizeof(in));
39        for(int i=1; i<=n-1; i++)
40        {
41            scanf("%d",&a[i]);
42            g[a[i]].push_back(i+1);
43            in[a[i]]++;
44        }
45        printf("%d\n",dfs(1));
46    }
47    return 0;
48 }
View Code

 

zoj 3805 Machine

标签:style   blog   http   color   os   io   for   ar   art   

原文地址:http://www.cnblogs.com/fanminghui/p/3933060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!