Cycle Correspondence

시간 제한2초메모리 제한1024 MB

요약
두 사람이 같은 K개의 헛간으로 이루어진 순환을 각자 다른 번호로 지정했을 때, 두 번호가 일치하는 헛간 수의 최댓값을 구한다.
난이도

어려움10점 중 8점

유형
그래프, 수학, 조합론
정답자
아직 제출이 없습니다

문제

Farmer John has NN barns (3≤N≤5⋅1053\le N\le 5\cdot 10^5), of which KK (3≤K≤N3\le K\le N) distinct pairs of barns are connected.

First, Annabelle assigns each barn a distinct integer label in the range \[1,N]\[1,N], and observes that the barns with labels a_1,…,a_Ka\_1,\dots,a\_K are connected in a cycle, in that order. That is, barns a_ia\_i and a_i+1a\_{i+1} are connected for all 1≤i\<K1\le i\<K, and barns a_Ka\_K and a_1a\_1 are also connected. All a_ia\_i are distinct.

Next, Bessie also assigns each barn a distinct integer label in the range \[1,N]\[1,N] and observes that the barns with labels b_1,…,b_Kb\_1,\dots,b\_K are connected in a cycle, in that order. All b_ib\_i are distinct.

Some (possibly none or all) barns are assigned the same label by Annabelle and Bessie. Compute the maximum possible number of barns that are assigned the same label by Annabelle and Bessie.

입력

The first line contains NN and KK.

The next line contains a_1,…,a_Ka\_1,\dots, a\_K.

The next line contains b_1,…,b_Kb\_1,\dots, b\_K.

출력

The maximum number of fixed points.

예제3

  1. 예제 1

    입력
    6 3
    1 2 3
    2 3 1
    
    예상 출력
    6
    
  2. 예제 2

    입력
    6 3
    1 2 3
    4 5 6
    
    예상 출력
    0
    
  3. 예제 3

    입력
    6 4
    1 2 3 4
    4 3 2 5
    
    예상 출력
    4