#P9978. [USACO23DEC] Cycle Correspondence S
[USACO23DEC] Cycle Correspondence S
Problem Description
Farmer John has barns (), with pairs of distinct barns connected.
At first, Annabelle assigns each barn an integer label in the range , and she finds that the barns with labels form a cycle connection in order. In other words, for all , barn is connected to barn , and barn is also connected to barn . All are distinct.
Then, Bessie also assigns each barn an integer label in the range , and she finds that the barns with labels also form a cycle connection in order. All are distinct.
Some (possibly none or all) barns are assigned the same label by both Annabelle and Bessie. Compute the maximum number of such barns.
Input Format
The first line contains and .
The next line contains .
The next line contains .
Output Format
The maximum number of barns that can be assigned the same label.
6 3
1 2 3
2 3 1
6
6 3
1 2 3
4 5 6
0
6 4
1 2 3 4
4 3 2 5
4
Hint
Sample Explanation 1
Annabelle and Bessie can assign the same label to every barn.
Sample Explanation 2
Annabelle and Bessie cannot assign the same label to any barn.
Sample Explanation 3
Annabelle and Bessie can assign labels to the same barns.
Test Point Properties
- Test points satisfy .
- Test points satisfy .
- Test points have no additional constraints.
Translated by ChatGPT 5