第一题一样 public static void main(String[] args){         Scanner in = new Scanner(System.in);         String[] A = in.nextLine().split(" ");         String[] B = in.nextLine().split(" ");         int target = in.nextInt();         int[] sons = new int[A.length];         int[] parents = new int[B.length];         for (int i=0;i<A.length;i++){             sons[i] = Integer.valueOf(A[i]);             parents[i] = Integer.valueOf(B[i]);         }         Queue<Integer> queue = new LinkedList<>();         queue.offer(target);         int sum = 0;         while (!queue.isEmpty()){             int par = queue.poll();             for (int i=0;i<parents.length;i++){                 if (parents[i] == par){                     queue.offer(sons[i]);                     sum++;                 }             }         }         if (sum == 0) {             for (int i=0;i<sons.length;i++){                 if (sons[i] == target){                     sum++;                 }             }             System.out.println(sum);         } else {             System.out.println(sum + 1);         }     }
3 1

相关推荐

04-25 18:13
五邑大学 Java
后来123321:大二两段实习太厉害了,我现在大二连面试都没有
点赞 评论 收藏
分享
牛客网
牛客企业服务