|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.StringTokenizer; |
| 4 | +import java.util.*; |
| 5 | +import java.io.*; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static int size; |
| 11 | + static Student[] students; |
| 12 | + static List<Integer> ans = new ArrayList<>(); |
| 13 | + static HashSet<Student> set = new HashSet<>(); |
| 14 | + |
| 15 | + static StringBuilder sb = new StringBuilder(); |
| 16 | + |
| 17 | + |
| 18 | + static class Student implements Comparable<Student>{ |
| 19 | + int presentCnt = 0; |
| 20 | + int idx; |
| 21 | + int target1; |
| 22 | + int target2; |
| 23 | + |
| 24 | + public Student (int idx,int t1, int t2) { |
| 25 | + this.idx = idx; |
| 26 | + target1 = t1; |
| 27 | + target2 = t2; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public int compareTo(Student s) { |
| 32 | + return Integer.compare(this.presentCnt, s.presentCnt); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static void main(String[] args) throws IOException { |
| 37 | + init(); |
| 38 | + process(); |
| 39 | + print(); |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + private static void init() throws IOException{ |
| 44 | + size = Integer.parseInt(br.readLine()); |
| 45 | + students = new Student[size+1]; |
| 46 | + |
| 47 | + for (int i = 1; i <= size; i++) { |
| 48 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 49 | + int t1 = Integer.parseInt(st.nextToken()); |
| 50 | + int t2 = Integer.parseInt(st.nextToken()); |
| 51 | + students[i] = new Student(i,t1, t2); |
| 52 | + set.add(students[i]); |
| 53 | + } |
| 54 | + |
| 55 | + for (int i = 1; i <= size; i++) { |
| 56 | + Student s = students[i]; |
| 57 | + students[s.target1].presentCnt++; |
| 58 | + students[s.target2].presentCnt++; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static void process() throws IOException { |
| 63 | + PriorityQueue<Student> pq = new PriorityQueue<>(); |
| 64 | + |
| 65 | + for (int i = 1; i <= size; i++) { |
| 66 | + pq.add(students[i]); |
| 67 | + } |
| 68 | + |
| 69 | + while(pq.size() > 0 && pq.peek().presentCnt < 2) { |
| 70 | + Student s = pq.poll(); |
| 71 | + if (!set.contains(s)) continue; |
| 72 | + set.remove(s); |
| 73 | + students[s.target1].presentCnt--; |
| 74 | + students[s.target2].presentCnt--; |
| 75 | + |
| 76 | + if (students[s.target1].presentCnt < 2) { |
| 77 | + pq.add(students[s.target1]); |
| 78 | + } |
| 79 | + if (students[s.target2].presentCnt < 2) { |
| 80 | + pq.add(students[s.target2]); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + for (int i = 1; i <= size; i++) { |
| 85 | + if ( students[i].presentCnt == 2) ans.add(i); |
| 86 | + } |
| 87 | + |
| 88 | + sb.append(ans.size()); |
| 89 | + if (ans.size() == 0) return; |
| 90 | + sb.append("\n"); |
| 91 | + for (int n: ans) { |
| 92 | + sb.append(n).append(" "); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + private static void print() { |
| 99 | + System.out.println(sb); |
| 100 | + } |
| 101 | + |
| 102 | +} |
| 103 | +``` |
0 commit comments