D. Substring

Codeforces
IDCF919D
Time3000ms
Memory256MB
Difficulty
dfs and similardpgraphs
English · Original
Chinese · Translation
Formal · Original
You are given a graph with $n$ nodes and $m$ **directed** edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "_abaca_", then the value of that path is $3$. Your task is find a path whose value is the largest. ## Input The first line contains two positive integers $n, m$ ($1 \leq n, m \leq 300\,000$), denoting that the graph has $n$ nodes and $m$ directed edges. The second line contains a string $s$ with only lowercase English letters. The $i$\-th character is the letter assigned to the $i$\-th node. Then $m$ lines follow. Each line contains two integers $x, y$ ($1 \leq x, y \leq n$), describing a directed edge from $x$ to $y$. Note that $x$ can be equal to $y$ and there can be multiple edges between $x$ and $y$. Also the graph can be not connected. ## Output Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output _\-1_ instead. [samples] ## Note In the first sample, the path with largest value is $1 \to 3 \to 4 \to 5$. The value is $3$ because the letter '_a_' appears $3$ times.
你被给定一个包含 $n$ 个节点和 $m$ 条有向边的图。每个节点被分配一个小写字母。我们定义一条路径的值为该路径上出现频率最高的字母的出现次数。例如,如果路径上的字母为 "_abaca_",则该路径的值为 $3$。你的任务是找到一条值最大的路径。 第一行包含两个正整数 $n, m$ ($1 lt.eq n, m lt.eq 300 thin 000$),表示图中有 $n$ 个节点和 $m$ 条有向边。 第二行包含一个仅由小写英文字母组成的字符串 $s$。第 $i$ 个字符是分配给第 $i$ 个节点的字母。 接下来 $m$ 行,每行包含两个整数 $x, y$ ($1 lt.eq x, y lt.eq n$),描述一条从 $x$ 指向 $y$ 的有向边。注意 $x$ 可以等于 $y$,$x$ 和 $y$ 之间可能存在多条边,且图可能不连通。 请输出一行一个整数,表示最大的值。如果值可以任意大,请输出 _-1_。 在第一个样例中,值最大的路径是 $1 arrow.r 3 arrow.r 4 arrow.r 5$。该路径的值为 $3$,因为字母 '_a_' 出现了 $3$ 次。 ## Input 第一行包含两个正整数 $n, m$ ($1 lt.eq n, m lt.eq 300 thin 000$),表示图中有 $n$ 个节点和 $m$ 条有向边。第二行包含一个仅由小写英文字母组成的字符串 $s$。第 $i$ 个字符是分配给第 $i$ 个节点的字母。接下来 $m$ 行,每行包含两个整数 $x, y$ ($1 lt.eq x, y lt.eq n$),描述一条从 $x$ 指向 $y$ 的有向边。注意 $x$ 可以等于 $y$,$x$ 和 $y$ 之间可能存在多条边,且图可能不连通。 ## Output 请输出一行一个整数,表示最大的值。如果值可以任意大,请输出 _-1_。 [samples] ## Note 在第一个样例中,值最大的路径是 $1 arrow.r 3 arrow.r 4 arrow.r 5$。该路径的值为 $3$,因为字母 '_a_' 出现了 $3$ 次。
**Definitions** Let $ G = (V, E) $ be a directed graph with $ n = |V| $ nodes and $ m = |E| $ edges. Let $ s \in \Sigma^n $ be a string of lowercase letters, where $ s_i $ is the letter assigned to node $ i \in V $. For a path $ P = (v_1, v_2, \dots, v_k) $, define its *letter frequency vector* $ f_P : \Sigma \to \mathbb{N} $ as $ f_P(c) = |\{ i \in \{1,\dots,k\} \mid s_{v_i} = c \}| $, and its *value* as $ \max_{c \in \Sigma} f_P(c) $. **Constraints** 1. $ 1 \le n, m \le 300{,}000 $ 2. Each edge $ (x, y) \in E $ satisfies $ 1 \le x, y \le n $ 3. Self-loops and multiple edges are allowed 4. The graph may be disconnected **Objective** Compute $ \max_{P \in \mathcal{P}} \max_{c \in \Sigma} f_P(c) $, where $ \mathcal{P} $ is the set of all finite paths in $ G $. If there exists a path with arbitrarily large value (i.e., a cycle containing at least one node with letter $ c $ and reachable from itself with positive length), output $ -1 $.
Samples
Input #1
5 4
abaca
1 2
1 3
3 4
4 5
Output #1
3
Input #2
6 6
xzyabc
1 2
3 1
2 3
5 4
4 3
6 4
Output #2
\-1
Input #3
10 14
xzyzyzyzqx
1 2
2 4
3 5
4 5
2 6
6 8
6 5
2 10
3 9
10 9
4 6
1 10
2 8
3 7
Output #3
4
API Response (JSON)
{
  "problem": {
    "name": "D. Substring",
    "description": {
      "content": "You are given a graph with $n$ nodes and $m$ **directed** edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For exam",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 3000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF919D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a graph with $n$ nodes and $m$ **directed** edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For exam...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "你被给定一个包含 $n$ 个节点和 $m$ 条有向边的图。每个节点被分配一个小写字母。我们定义一条路径的值为该路径上出现频率最高的字母的出现次数。例如,如果路径上的字母为 \"_abaca_\",则该路径的值为 $3$。你的任务是找到一条值最大的路径。\n\n第一行包含两个正整数 $n, m$ ($1 lt.eq n, m lt.eq 300 thin 000$),表示图中有 $n$ 个节点和 $m$ 条...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ G = (V, E) $ be a directed graph with $ n = |V| $ nodes and $ m = |E| $ edges.  \nLet $ s \\in \\Sigma^n $ be a string of lowercase letters, where $ s_i $ is the letter assigned t...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments