分组的最多数量

幼儿园老师安排小朋友做游戏,现在需要给N个小朋友进行分组,老师让每个同学写个名字, 代表这位小朋友想和谁分到组。请问老师在满足所有小朋友意愿的情况下,最多可以将班级分成多少组?

输入:第一行输入N, 0<N<=100000接下来是N行代表每个小朋友希望和谁分到-组,如"John Jack",代表John希望和Jack分到一组,两个名字之间以空格分割,名字本身不存在空格。

输出描述: 分组的最多数量

import java.util.*;

public class huawei {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        //name_set存放所有名字
        Set<String> name_set = new HashSet<>();
        HashMap<String, String> cp = new HashMap<>();
        for (int i = 0; i < n; ++i) {
            String name = sc.nextLine();
            String[] info = name.split(" ");
            if(!name_set.contains(info[0])){
                name_set.add(info[0]);
            }
            if(!name_set.contains(info[1])){
                name_set.add(info[1]);
            }
            cp.put(info[0], info[1]);
        }

        //names存放所有名字
        String[] names = new String[n + 1];
        int i = 0;
        Iterator<String> it = name_set.iterator();
        while (it.hasNext()) {
            names[i] = it.next();
            i++;
        }

        /*
        * 遍历所有名字,对于每个名字,例如A
        *       检索方向有2个:纵向检索:A->B B->C C->D ...ABCD这样的算是一个组
        *                     横向检索:B->A C->A D->A ...ABCD这样的算是一个组
        * 对于在一个组里面的名字,都存放在tmp里面,
        *
        * nams[i] = ""时,表示第i个录入的名字处理过了
        *
        * */
        //分组最多的数量
        int max_number = 0;
        //一个组数量记录
        int number_tmp;

        for (int l = 0; l < names.length -1; l++) {
            Queue<String> tmp = new LinkedList<>();
            number_tmp = 0;
            if(!"".equals(names[l]) ) {
                String A = names[l];
                tmp.add(A);
                while(!tmp.isEmpty()){
                    A = tmp.poll();
                    number_tmp ++;
                    names[l] = "";
                    while (cp.get(A) != null) {
                        // 横向检索
                        for (int j = 0; j < names.length; j++) {
                            if(cp.get(names[j])!= null && cp.get(names[j]).equals(A) && !tmp.contains(names[j])) {
                                tmp.add(names[j]);

                                names[name2key(names, names[j])] = "";
                            }
                        }
                        //纵向检索
                        String B = cp.get(A);
                        if(name2key(names, B) != -1) {
                            tmp.add(B);
                            names[name2key(names, B)] = "";
                            A = B;
                        }
                        else break;
                    }

                }
                // 可以划分为一组的处理完毕
                max_number = Math.max(max_number, number_tmp);
            }

        }
        System.out.println(max_number);
    }
    // 姓名到下标的转化
    public static int name2key(String []names, String name){
        for (int i = 0; i < names.length; i++) {
            if(name.equals(names[i])) return i;
        }
        return -1;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值