#P1682. 过家家
过家家
Description
There are elementary school students playing the “playing house” game: boys labeled to , and girls labeled to . Each girl may choose a boy with whom she does not quarrel as her partner. In addition, if girl has a friend (also a girl) who does not quarrel with boy , then may also choose . Moreover, friendship is transitive: if and are friends, and and are friends, then and are also considered friends. Note that a boy may be chosen by multiple girls as their partner.
Once every girl has chosen a partner, they start a new round of the game. After each round, every girl looks for a new boy as her partner (one she has never chosen before). Also, each girl may, in total, force at most boys to accept her, regardless of whether they have quarreled before.
Your task is to determine the maximum number of rounds the students can play.
Input Format
The first line contains four integers (Constraints: , , ).
Here, means there are students in total, with boys and girls.
The next lines each contain two integers , indicating that girl and boy have never quarreled.
The next lines each contain two integers , indicating that girls and are friends.
Output Format
For each test, output one integer, the maximum number of rounds the students can play.
4 5 1 2
1 1
2 3
3 2
4 2
4 4
1 4
2 3
3
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号