시간 제한메모리 제한제출정답맞힌 사람정답 비율
7 초 1024 MB1810562.500%

문제

You are given a two-dimensional array of integers of size $N \times K$, $A$ $=$ $A_{0,0}$, $A_{0,1}$, $\dots$, $A_{0,K-1}$, $A_{1,0}$, $\dots$, $A_{N-1,K-1}$ and also arrays of integers of size $M$, $U$ $=$ $U_{0}$, $\dots$, $U_{M-1}$ and $V$ $=$ $V_0$, $\dots$, $V_{M-1}$.

Jimin made a cute weighted undirected graph $G$, which is a complete graph with the weight of an edge connecting vertices $u$ and $v$ is $\left\lvert A_{u, (v \, \bmod \, K)} - A_{v, (u \, \bmod \, K)} \right\rvert$. Eunsoo then found the minimum spanning tree of $G$.

However, Jongyoung brutally deleted edges of $G$ connecting $U_i$ and $V_i$ for $0 \le i \le M-1$. Note that $G$ may not be connected after deleting the edges.

Now, to help poor Jimin and Eunsoo, you should find the minimum spanning forest of $G$. A minimum spanning forest is a union of the minimum spanning trees of its connected components.

입력

The first line contains three space-separated integers, $N$, $K$, and $M$.

Each of the following $N$ lines contains $K$ space-separated integers, $A_{i,0},$ $\dots,$ $A_{i,K-1}$. $(0 \le i \le N-1)$

Each of the following $M$ lines contains two space-separated integers, $U_i$ and $V_i$. $(0 \le i \le M-1)$

출력

Output the sum of the weight of edges in the minimum spanning forest of $G$.

제한

  • $1 \le NK \le 300\,000$
  • $1 \le K \le N$
  • $0 \le M \le \min\left(\frac{N(N-1)}{2},\ 300\,000\right)$
  • $-10^9 \le A_{i,j} \le 10^9$ $(0 \le i \le N-1, 0 \le j \le K-1)$
  • $0 \le U_i < V_i \le N - 1$ $(0 \le i \le M-1)$
  • $(U_i,$ $V_i) \neq (U_j,$ $V_j)$ $(0 \le i < j \le M-1)$

서브태스크 1 (7점)

This subtask has an additional constraint: 

  • $N \le 1\,000$

서브태스크 2 (23점)

This subtask has additional constraints:

  • $NK \le 150\,000$
  • $M = 0$

서브태스크 3 (46점)

This subtask has additional constraints:

  • $NK \le 150\,000$
  • $M \le 150\,000$

서브태스크 4 (24점)

This subtask has no additional constraints.

예제 입력 1

7 1 7
0
1
2
1
2
-5
-1
4 5
1 3
4 6
0 4
2 5
1 4
3 4

예제 출력 1

8

예제 입력 2

7 2 7
5 1
4 5
-2 4
4 1
-5 -5
2 -1
3 3
5 6
0 5
0 3
1 2
4 6
2 3
2 6

예제 출력 2

11

출처

University > KAIST > 2022 KAIST RUN Spring Contest H번

채점 및 기타 정보

  • 예제는 채점하지 않는다.