//树的深度 import java.util.*; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin=new Scanner(System.in); int[] nn= new int[1001]; int i=0; int n=0; int count = cin.nextInt(); nn[i++] = count; while((count--)!=0) { n=cin.nextInt(); nn[i++]=n; } getlength(nn,i); } public static void getlength(int[] nn,int i){ int number = i-1; if(number==0){System.out.print(0);return;} else if(number==1){System.out.print(1);return;} else if(number>1&&number<5){System.out.print(2);return;} int start = 1; int censhu = 0; censhu = number/2; int hehe[][] = new int[censhu][2]; for(int i1=0;i1<censhu;i1++){ for(int j1=0;j1<2;j1++){ hehe[i1][j1] = nn[start]; start++; } } int maxlength1 = 2; for(int i2=0;i2<censhu-1;i2++){ for(int j2=i2+1;j2<censhu;j2++){ if(hehe[i2][1]==hehe[j2][0]){ maxlength1++; i2 = j2; } } } int maxlength2 = 2; for(int i3=1;i3<censhu-1;i3++){ for(int j3=i3+1;j3<censhu;j3++){ if(hehe[i3][1]==hehe[j3][0]){ maxlength2++; i3 = j3; } } } int maxlength = 0; if(maxlength1>maxlength2){ maxlength = maxlength1; }else{ maxlength = maxlength2; } System.out.print(maxlength); } }
点赞 评论

相关推荐

牛客网
牛客企业服务