This page is still under construction.

Parts of this page are still being built. What you see may change.

Hospital

Time limit2sMemory limit128 MB

Summary
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 77 nurses where 11–55 are special and 66–77 are general, and the nurses who can cover each special nurse are as follows: nurse 11 by 66 or 77, nurse 22 by 77, nurse 33 by 22 or 77, nurse 44 by 55, nurse 55 by 44. Here nurses 44 and 55 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 11, 22, and 33 can each go on vacation. However, nurses 22 and 33 both ultimately depend on nurse 77, who cannot cover two posts at once, so 22 and 33 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 nn and the number of special nurses kk. Nurses 11 through kk are special and nurses k+1k+1 through nn are general. (1≤k<n≤10001 \le k < n \le 1000)

Each of the next kk lines describes who can cover a special nurse; the ii-th of these lines is for special nurse ii. Its first number is the count did_i of nurses who can cover nurse ii, followed by those did_i nurse numbers. The total length of all lists does not exceed 10 00010\,000.

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 (A,B)(A, B) and (B,A)(B, A) count as the same pair. If this number is at most 10 00010\,000, then on each following line print one such pair as its two nurse numbers with A<BA < B, listing the pairs sorted in ascending order by the first number and then by the second.

Examples2

  1. Example 1

    Input
    7 5
    2 6 7
    1 7
    2 2 7
    1 5
    1 4
    
    Expected output
    2
    4 5
    3
    2 3
    2 7
    3 7
    
  2. Example 2

    Input
    2 1
    1 2
    
    Expected output
    0
    
    1
    1 2