#P2078. 朋友

朋友

Description

The employees of each company share one characteristic: everyone in the same company is of the same sex.

Company A has NN employees with PP friend pairs. Company B has MM employees with QQ friend pairs. Friends of friends are also friends.

Each friend pair is given as two integers (Xi,Yi)(X_i, Y_i), indicating the IDs of the two friends are XiX_i and YiY_i. Male IDs are positive numbers, and female IDs are negative numbers. Xiao Ming’s ID is 11, and Xiao Hong’s ID is 1-1.

It is known that Xiao Ming and Xiao Hong are friends. Please write a program to find the maximum total number of couples that can be formed between the two companies via people known through Xiao Ming and Xiao Hong (including themselves).

Input Format

The first line contains 44 space-separated positive integers N,M,P,QN,M,P,Q.

Then follow PP lines, each containing two positive integers Xi,YiX_i, Y_i.

Then follow QQ lines, each containing two negative integers Xi,YiX_i, Y_i.

Output Format

Output a single positive integer on one line, representing the maximum total number of couples that can be formed via people known through Xiao Ming and Xiao Hong (including themselves).

4 3 4 2
1 1
1 2
2 3
1 3
-1 -2
-3 -3

2

Hint

For 30%30 \% of the testdata, N,M100N,M \le 100, P,Q200P,Q \le 200.

For 80%80 \% of the testdata, N,M4×103N,M \le 4 \times 10^3, P,Q104P,Q \le 10^4.

For 100%100 \% of the testdata, N,M104N,M \le 10^4, P,Q2×104P,Q \le 2 \times 10^4.

Translated by ChatGPT 5