Hospital
Time limit2sMemory limit128 MB
Given substitution lists for special nurses, find those who can never take vacation and all pairs that can go individually but not simultaneously.
- Level
Medium7 of 10
- Topics
- Graph, DFS, BFS, Combinatorics
- Solved
- No attempts yet
Problem
You are building a vacation-management system for a large hospital. Its nurses come in two kinds.
- A general nurse cares for inpatients. If a general nurse goes on vacation another nurse can simply absorb the work, so there is no problem.
- A special nurse holds a dedicated post such as "surgical nurse" or "head nurse", and may go on vacation only if there is a substitute to cover the post.
Every special nurse has a list of nurses who can cover their post. When a special nurse goes on vacation, one nurse from that list takes over the post. If the nurse who took over is itself another special nurse, then that nurse's own post must in turn be covered by someone else. The vacation is possible only if this chain of substitutions eventually ends with a general nurse filling a post. A nurse can cover at most one post at a time, and a nurse who is on vacation cannot cover any post. If no valid chain of substitutions exists, that special nurse cannot go on vacation.
For example, suppose there are nurses where – are special and – are general, and the nurses who can cover each special nurse are as follows: nurse by or , nurse by , nurse by or , nurse by , nurse by . Here nurses and can only cover each other, so if one takes a vacation the other cannot cover both posts at once; neither can ever go on vacation. Nurses , , and can each go on vacation. However, nurses and both ultimately depend on nurse , who cannot cover two posts at once, so and cannot be on vacation at the same time.
Given the substitution information of all nurses, write a program that finds the nurses who can never take a vacation, and every pair of nurses who can each take a vacation individually but not at the same time.
Input
The first line contains the total number of nurses and the number of special nurses . Nurses through are special and nurses through are general. ()
Each of the next lines describes who can cover a special nurse; the -th of these lines is for special nurse . Its first number is the count of nurses who can cover nurse , followed by those nurse numbers. The total length of all lists does not exceed .
Output
On the first line, print the number of nurses who can never take a vacation. On the second line, print their numbers in ascending order separated by spaces (print an empty line if there are none).
On the third line, print the number of pairs of nurses who can each take a vacation individually but cannot be on vacation at the same time. The pairs and count as the same pair. If this number is at most , then on each following line print one such pair as its two nurse numbers with , listing the pairs sorted in ascending order by the first number and then by the second.