#P8456. 「SWTR-8」地地铁铁

「SWTR-8」地地铁铁

Background

D_T_ : D_tt : ddT_ : ddtt = 9 : 3 : 3 : 1.

Problem Description

You are given an undirected connected graph with nn vertices and mm edges. Each edge is labeled D or d.

Define an unordered pair of vertices (x,y)(x, y) to be "Iron" if and only if xyx \neq y and there exists a simple path between xx and yy on which both D and d appear.

A knows the importance of the law of independent assortment DdTt, so he asks you to count such vertex pairs.

Notes:

  • A simple path is a path that does not pass through any repeated vertex.
  • The graph is guaranteed to have no self-loops, and it may contain multiple edges.

Input Format

The first line contains an integer SS, indicating the Subtask ID of this test point.

The second line contains two integers n,mn, m.

The next mm lines each contain two integers x,yx, y and a letter cc, meaning there is an undirected edge connecting xx and yy labeled cc.

Output Format

Output one integer in one line, representing the answer.

0
8 13
1 2 d
1 3 d
2 3 d
3 4 d
3 5 D
4 5 d
4 6 d
5 6 D
6 7 d
6 8 d
6 8 D
6 8 D
7 8 d
24

Hint

Constraints and Conventions

This problem uses bundled testdata.

  • Subtask #1 (6 points): n8n \leq 8, m20m \leq 20.
  • Subtask #2 (16 points): n15n \leq 15, m822m \leq 822. Depends on Subtask #1.
  • Subtask #3 (17 points): m=n1m = n - 1.
  • Subtask #4 (18 points): m=nm = n.
  • Subtask #5 (19 points): n1064n \leq 1064, m104m \leq 10 ^ 4. Depends on Subtask #2.
  • Subtask #6 (24 points): No special limits. Depends on Subtask #3, #4, #5.

For 100%100\% of the data:

  • 2n4×1052 \leq n \leq 4\times 10 ^ 5, n1m106n - 1 \leq m \leq 10 ^ 6.
  • 1x,yn1 \leq x, y \leq n.
  • c{D,d}c \in \{\texttt{D}, \texttt{d}\}.
  • The graph is guaranteed to have no self-loops, and it may contain multiple edges.

Help and Tips

Please pay attention to I/O optimization.

Source

Translated by ChatGPT 5