【编程题解】Children’s day

Description

Children'sDay is coming, the teachers with the children to go outing, a teacher can not manage so many people, so the children need to be divided into two groups.

First,each team has at least one member;

Second,everyone belongs to one team;

Then,everyone on the team knows everyone else on the team;

Finally,the team size is as close as possible.

There may be more than one solution to this task, and you can output any solution orindicate that the solution does not exist.

 

Input

The first line contains the integer N, indicating that there are N people numbered 1,2... , N.

The next N line, the i line, contains multiple space-delimited integers representing alist of Numbers known by the person numbered i, ending in 0.

Note that just because A knows B doesn't mean B knows A.

Data range:2≤N≤100

 

Output

If No solution exists, output "No solution".

If it exists, it prints out the members of two teams, one for each team, first the number of teams, then the number of team members in turn.

Sample Input 1 

5
2 3 5 0
1 4 5 3 0
1 2 5 0
1 2 3 0
4 3 2 1 0

Sample Output 1

3 1 3 5
2 2 4

 

Personal Answer  (using language:JAVA)  Not necessarily right

import java.math.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        ArrayList<Integer>[] al = new ArrayList[n + 1];
        for (int i = 1; i <= n; i++) {
            int num;
            al[i] = new ArrayList<Integer>();
            do {
                num = scanner.nextInt();
                if (num != 0) {
                    al[i].add(num);
                }
            } while (num != 0);
        }
//        System.out.println(al);

        ArrayList ready = new ArrayList();
        for (int i = 1; i <= n; i++) {
            ready.add(i);
        }

        ArrayList<Integer>[] reverse = new ArrayList[n + 1];
        for (int i = 1; i <= n; i++) {
            reverse[i] = new ArrayList();
        }

        for (int i = 1; i <= n; i++) {
            ArrayList tmp = al[i];
            for (int j = 1; j <= n; j++) {
                if (!tmp.contains(j)&&i!=j) {
                    reverse[j].add(i);
                    reverse[i].add(j);
                }
            }
        }

        ArrayList warm=new ArrayList();
        for (int i = 1; i <= n; i++) {
            if(reverse[i].size()==0){
                warm.add(i);
                ready.remove((Object)i);
            }
        }

        ArrayList out1 = new ArrayList();
        ArrayList out2 = new ArrayList();

        do {
            int tmp = (int) ready.remove(0);
            if (out2.size() < out1.size()) {
                newadd(out1, out2, tmp, 2, reverse, ready);
            } else {
                newadd(out1, out2, tmp, 1, reverse, ready);
            }
        } while (ready.size() != 0);

        do {
            int tmp = (int) warm.remove(0);
            if (out2.size() < out1.size()) {
                out2.add(tmp);
            } else {
                out1.add(tmp);
            }
        } while (warm.size() != 0);

        if(out1.size()+out2.size()!=n){
            System.out.println("No solution");
            System.exit(1);
        }

        Collections.sort(out1);
        System.out.print(out1.size());
        for (int i = 0; i < out1.size(); i++) {
            System.out.print(" "+out1.get(i));
        }
        System.out.println();
        Collections.sort(out2);
        System.out.print(out2.size());
        for (int i = 0; i < out2.size(); i++) {
            System.out.print(" "+out2.get(i));
        }

    }

    public static void newadd(ArrayList out1, ArrayList out2, int data, int tip, ArrayList<Integer>[] reverse, ArrayList ready) {
        if (tip == 1) {
            if (!out1.contains(data)) {
                out1.add(data);
                ready.remove((Object) data);
                for (int i : reverse[data]) {
                    newadd(out1, out2, i, 2, reverse, ready);
                }
            }
        } else {
            if (!out2.contains(data)) {
                out2.add(data);
                for (int i : reverse[data]) {
                    newadd(out1, out2, i, 1, reverse, ready);
                }
            }
        }
    }
}

Welcome to communicate!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值