Hit and Away

AtCoder
IDabc429_e
Time2000ms
Memory256MB
Difficulty
You are given a simple connected undirected graph $G$ with $N$ vertices and $M$ edges. The vertices and edges of $G$ are numbered as vertices $1,2,\ldots,N$ and edges $1,2,\ldots,M$, respectively, and edge $i$ connects vertices $U_i$ and $V_i$. You can move bidirectionally between vertices connected by an edge in time $1$. Additionally, each vertex is either safe or dangerous, and this state is given by a string $S$ of length $N$ consisting of `S` and `D`. Specifically, vertex $i$ is safe when the $i$\-th character $(1\leq i\leq N)$ of $S$ is `S`, and vertex $i$ is dangerous when it is `D`. It is guaranteed that there are at least two safe vertices and at least one dangerous vertex. For each dangerous vertex $v$, find the following value: > The minimum possible time to start from some safe vertex, pass through $v$, and move to a safe vertex **different from the starting vertex**. ## Constraints * $3\leq N\leq 2\times 10^5$ * $N-1\leq M\leq 2\times 10^5$ * $1\leq U_i,V_i\leq N$ * $U_i\neq V_i$ * If $i\neq j$, then ${ U_i,V_i }\neq { U_j,V_j }$. * $S$ is a string of length $N$ consisting of `S` and `D`. * $N,M,U_i,V_i$ are all integers. * $G$ is connected. * There are at least two safe vertices. * There is at least one dangerous vertex. ## Input The input is given from Standard Input in the following format: $N$ $M$ $U_1$ $V_1$ $U_2$ $V_2$ $\vdots$ $U_M$ $V_M$ $S$ [samples]
Samples
Input #1
5 5
1 2
1 3
2 3
3 4
4 5
SSDDS
Output #1
2
3

The dangerous vertices are (in ascending order of vertex number) vertices $3$ and $4$.
For vertex $3$, moving from vertex $1$ $\to$ vertex $3$ $\to$ vertex $2$ (for example) satisfies the condition.  
The time required for this movement is $2$, and this is the minimum.  
Therefore, print $2$ on the $1$st line.
For vertex $4$, moving from vertex $1$ $\to$ vertex $3$ $\to$ vertex $4$ $\to$ vertex $5$ (for example) satisfies the condition.  
The time required for this movement is $3$, and there is no way to move that satisfies the condition with time $2$ or less, so this is the minimum.  
Therefore, print $3$ on the $2$nd line.
Input #2
3 2
1 2
2 3
SSD
Output #2
3

The dangerous vertex is vertex $3$.
Moving from vertex $1$ $\to$ vertex $2$ $\to$ vertex $3$ $\to$ vertex $2$ (for example) satisfies the condition.  
The time required for this movement is $3$, and this is the minimum.  
Note that movements such as vertex $2$ $\to$ vertex $3$ $\to$ vertex $2$ do not satisfy the condition that the destination is "different from the starting vertex".
API Response (JSON)
{
  "problem": {
    "name": "Hit and Away",
    "description": {
      "content": "You are given a simple connected undirected graph $G$ with $N$ vertices and $M$ edges.   The vertices and edges of $G$ are numbered as vertices $1,2,\\ldots,N$ and edges $1,2,\\ldots,M$, respectively, a",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc429_e"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a simple connected undirected graph $G$ with $N$ vertices and $M$ edges.  \nThe vertices and edges of $G$ are numbered as vertices $1,2,\\ldots,N$ and edges $1,2,\\ldots,M$, respectively, a...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments