[ICPC 2021 Macao R] LCS Spanning Tree

Luogu
IDLGP9664
Time5000ms
Memory1024MB
DifficultyP6
2021后缀自动机 SAMO2优化生成树后缀数组 SA后缀树ICPC澳门
Given a complete undirected graph of $n$ vertices and $n$ strings $s_1, s_2, \cdots, s_n$, the weight of edge connecting vertices $i$ and $j$ is equal to the length of the longest common substring (LCS) between $s_i$ and $s_j$. Compute the maximum total weight of any spanning tree on this graph. A substring of a string can be obtained by removing some (possibly zero) characters from the beginning and/or the end of that string. For example, ``maca``, ``aca`` and ``cau`` are all substrings of ``macau``, while ``acu`` is not. ## Input There is only one test case in each test file. The first line of the input contains one integer $n$ ($1 \leq n \leq 2 \times 10^6$) indicating the number of vertices and strings. For the following $n$ lines, the $i$-th line contains one string $s_i$ ($1 \le |s_i| \le 2 \times 10^6$) consisting only of lowercase English letters. It's guaranteed that the sum of lengths of all strings will not exceed $2 \times 10^6$. ## Output Output one line containing one integer indicating the answer. [samples]
Samples
Input #1
4
icpc
macau
regional
contest
Output #1
4
Input #2
3
ababa
babab
aba
Output #2
7
API Response (JSON)
{
  "problem": {
    "name": "[ICPC 2021 Macao R] LCS Spanning Tree",
    "description": {
      "content": "Given a complete undirected graph of $n$ vertices and $n$ strings $s_1, s_2, \\cdots, s_n$, the weight of edge connecting vertices $i$ and $j$ is equal to the length of the longest common substring (LC",
      "description_type": "Markdown"
    },
    "platform": "Luogu",
    "limit": {
      "time_limit": 5000,
      "memory_limit": 1048576
    },
    "difficulty": {
      "LuoguStyle": "P6"
    },
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "LGP9664"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Given a complete undirected graph of $n$ vertices and $n$ strings $s_1, s_2, \\cdots, s_n$, the weight of edge connecting vertices $i$ and $j$ is equal to the length of the longest common substring (LC...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments