Jealous Teachers
Time limit3sMemory limit1024 MB
Each of N-1 students must send exactly N-1 flowers to teachers they learned from, and every teacher must receive exactly N-1 flowers in total, or report that no such assignment exists.
- Level
Medium6 of 10
- Topics
- Graph, Implementation, Greedy, Array
- Solved
- No attempts yet
Problem
The Korea Science Academy of KAIST (KSA) has teachers and students. Because tomorrow is Teacher's Day in Korea, each student bought flowers. However, one student quit, so only students remain at the school.
The teachers are very jealous, so they give an F grade to a student who gives them fewer flowers than the other students do. Therefore, every teacher must receive exactly flowers. A student can give flowers only to teachers who have taught that student, and you are given which students learned from which teachers.
Seunghyun is a student at KSA, and he needs your help organizing this event.
Input
The first line contains two integers and , the number of teachers and the number of (student, teacher) pairs where the student learned from the teacher.
The next lines describe the relations. The -th line contains two integers and , meaning that the -th student can give flowers to the -th teacher. All pairs are distinct.
Output
If it is impossible to give all teachers the same number of flowers ( flowers), print on the first line.
Otherwise, output lines. The -th line must contain a single integer, the number of flowers that the -th student gave to the -th teacher.
If there are multiple possible answers, you may output any of them.
Constraints
- ()
- ()