【編程題解】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!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章