Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package qualification;
- import java.util.*;
- import java.util.stream.Collectors;
- public class B {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int k = in.nextInt();
- HashMap<String, Integer> map = new HashMap<>();
- List<String> strings = new ArrayList<>();
- int total = (int) (Math.pow(2, k) - 1);
- in.nextLine();
- for (int i = 0; i < total; i++) {
- strings.add(Arrays.stream(in.nextLine().split(" ")).sorted(String::compareTo).reduce((a, b) -> a + " " + b).get());
- }
- if (strings.size() != new HashSet<>(strings).size()) {
- System.out.println("NO SOLUTION");
- return;
- }
- for (int i = 0; i < total; i++) {
- String[] s = strings.get(i).split(" ");
- map.put(s[0], map.getOrDefault(s[0], 0) + 1);
- map.put(s[1], map.getOrDefault(s[1], 0) + 1);
- }
- if (map.keySet().size() != total + 1) {
- System.out.println("NO SOLUTION");
- return;
- }
- boolean res = true;
- int count = (total + 1) / 2;
- for (int i = 0; i < k; i++) {
- int finalI = i;
- if (i != k - 1) {
- res = res && map.values().stream().filter(q -> q == finalI + 1).count() == count;
- } else {
- res = res && map.values().stream().filter(q -> q == finalI + 1).count() == count * 2;
- }
- count /= 2;
- }
- if (res) {
- List<String> finalists = map.keySet().stream().filter(q -> map.get(q) == k - 1).collect(Collectors.toList());
- System.out.println(finalists.get(0) + " " + finalists.get(1));
- } else {
- System.out.println("NO SOLUTION");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement