Course Swap
Time limit4sMemory limit512 MB
Each student holds course A_i and wants B_i; two students can swap their courses, and chains of swaps are allowed. Find the minimum number of students who cannot get their desired course.
- Level
Medium6 of 10
- Topics
- Graph, Greedy, Hash map, Implementation
- Solved
- No attempts yet
Problem
In 20XX, Yonsei University, the only university in the country using a mileage based course registration system, plans to introduce a new system for course changes as well.
Starting in 20XX, "course swaps" are allowed during the course change period.
A course swap is when two people exchange different courses, and it is only possible with the consent of both people.
Three-way swaps are not possible, but after two people swap courses, one of them may swap that course with yet another person.
When the system was first introduced there was much discussion, but good cases are increasing, such as students who want to take each other's major courses swapping courses, or graduating students with leftover mileage handing courses down to juniors.
As always, the course change period has begun. Given one course each student wants to take and one course each student wants to swap, find the minimum number of students who fail to take the course they want after all course swaps are finished.
Input
The first line gives the number of students . ()
The second line gives integers . is the course student is currently enrolled in and also the number of the course they want to swap. ()
The third line gives integers . is the number of the course student wants to take through a swap. ()
All input is separated by spaces.
Output
Print the number of students who fail to take the course they want when all students finish course swaps using the optimal method.