Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- /////////////////////////////////////////////////
- class People {
- String name;
- int num;
- ArrayList<People> group = new ArrayList<People>();
- People(String name) {
- this.name = name;
- num = -1;
- }
- People(String name, int num) {
- this.name = name;
- this.num = num;
- }
- @Override
- public boolean equals(Object ppl) {
- People people = (People) ppl;
- if (name == null)
- return false;
- if (name == people.name)
- return true;
- return name.equals(people.name);
- }
- @Override
- public String toString() {
- if (num != -1)
- return name + " " + num;
- else
- return name + " undefined";
- }
- }
- /////////////////////////////////////////////////
- ArrayList<People> man = new ArrayList<People>();
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- scanner.nextLine();
- for (int i = 0; i < n; i++) {
- String[] group = new String[3];
- for (int j = 0; j < 3; j++) {
- String name = scanner.next();
- group[j] = name;
- int pos = man.indexOf(new People(name));
- if (pos < 0) {
- if(name.equals("Isenbaev")) {
- man.add(new People("Isenbaev", 0));
- }
- else {
- man.add(new People(name));
- }
- }
- }
- int A = man.indexOf(new People(group[0]));
- int B = man.indexOf(new People(group[1]));
- int C = man.indexOf(new People(group[2]));
- man.get(A).group.add(man.get(B));
- man.get(A).group.add(man.get(C));
- man.get(B).group.add(man.get(A));
- man.get(B).group.add(man.get(C));
- man.get(C).group.add(man.get(A));
- man.get(C).group.add(man.get(B));
- }
- ArrayDeque<People> queue = new ArrayDeque<People>();
- int Isenbaev = man.indexOf(new People("Isenbaev"));
- if(Isenbaev != -1)
- queue.add(man.get(Isenbaev));
- while (!queue.isEmpty()) {
- People v = queue.getFirst();
- queue.pop();
- for (People people : v.group) {
- if (people.num == -1) {
- people.num = v.num + 1;
- queue.add(people);
- }
- }
- }
- Collections.sort(man, new Comparator<People>() {
- @Override
- public int compare(final People o1, final People o2) {
- return o1.name.compareTo(o2.name);
- }
- });
- for (People people : man) {
- System.out.println(people.toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement