The Club Trip
Time limit1sMemory limit256 MB
Each of n classmates rides only if one named classmate also rides; fill up to k bus seats with the largest group that respects every such condition.
- Level
Medium7 of 10
- Topics
- Graph, Dynamic programming, Tree
- Solved
- No attempts yet
Problem
Namgyu chartered a bus so that he could go on a club trip with his classmates. The department office got the seat count wrong, so the bus cannot carry everyone. Once the classmates heard about it, they started saying things like this.
Jaehyeok: If Dongwoo does not go, I am not going either.
Dongwoo: If Sejong does not go, I will not go.
Seats are limited, and every classmate refuses to go unless one particular other person goes, so Namgyu is stuck. He bought far too much to drink, so he has to bring as many people as he can.
Given the person each classmate named, find the largest number of people that can ride the bus without breaking anyone's condition.
Input
The first line contains the number of people and the number of people the bus can carry . ()
The second line contains the integers in order. () means that if person does not ride the bus, person does not ride it either.
Output
Print, on one line, the largest number of people that can ride the bus without breaking anyone's condition. Print 0 if nobody can ride.