Under construction

Still being built. Things may change

Digitalisation

Time limit2sMemory limit1024 MB

Summary
Count how many students end up admitted to their first and second priority schools after two rounds of capacity-limited matching.
Level

Medium7 of 10

Topics
Greedy, Simulation
Solved
No attempts yet

Problem

The students of Tomorrow Programming School are admitted in the nationwide School Admission Process (SAP), which involves most schools in the region. SAP has two rounds.

At the start of the first round, each student applies to two schools and ranks them: a first priority and a second priority. A national exam gives each student a unique score and produces a common list CL of students sorted by score in descending order, so the highest score comes first. Every school can see CL. Each school prefers students with better scores. Each school has a capacity limit CC, and each school keeps a candidate list that also holds at most CC students. All candidate lists start empty.

The second round consists of one or more cycles of update events. In a cycle, each school, one after another, performs exactly one update event.

In an update event, school LL searches CL from its end, starting with the lowest-scoring applicant, and selects the first applicant AA who is not in LL and meets all of these conditions:

  • Either LL is not full, or the score of AA is better than the worst score among the candidates in LL.
  • School LL is either the first or the second priority of AA.
  • AA is either on no candidate list, or on the candidate list of AA's second priority school.

If school LL finds no such applicant, the update event is empty and ends. Otherwise, if LL is full, the candidate with the worst score in LL is removed from LL. Then AA is added to LL. If AA was already on another school's candidate list, AA is removed from that list. The update event is then valid.

If at least one update event in a cycle is valid, another cycle starts. The second round ends when every update event in one cycle is empty. Then each school admits the students on its candidate list.

The statisticians want to know how many students were admitted to their first priority school and how many were admitted to their second priority school.

Input

The first line contains three integers NN, MM, and CC (2≤N,M≤1052 \le N, M \le 10^5; 1≤C≤1001 \le C \le 100), the number of student applicants, the number of schools, and the common capacity limit.

Each of the following NN lines contains the first and second priority of one student, given as school labels from 11 to MM. The lines are sorted in descending order of the students' scores.

Output

Print two numbers on one line: the number of students admitted to their first priority school, and the number of students admitted to their second priority school.

Examples2

  1. Example 1

    Input
    9 3 4
    1 2
    2 3
    1 3
    3 2
    1 2
    3 2
    2 3
    2 3
    2 1
    
    Expected output
    9 0
    
  2. Example 2

    Input
    4 2 1
    1 2
    1 2
    1 2
    1 2
    
    Expected output
    1 1