#P7323. [WC2021] 括号路径
[WC2021] 括号路径
Description
Given a directed graph with vertices and edges, each edge has a label, which is either a left bracket or a right bracket. There are different bracket types, so the graph may contain different labels. Vertices, edges, and bracket types are all numbered starting from .
Each edge in the graph appears in a paired way with another edge. More specifically, if there is an edge labeled with the left bracket of type , then there must be an edge labeled with the right bracket of the same type . Similarly, every edge labeled with a right bracket corresponds to an opposite-direction edge labeled with the left bracket of the same type.
Now you need to compute how many pairs of vertices () satisfy the following: there exists a path from to in the graph such that, if you concatenate the labels of the edges along the path in the order they are traversed, the resulting string is a valid bracket sequence.
Input Format
The first line contains three integers , representing the number of vertices, the number of edge pairs, and the number of bracket types.
The next lines each contain three integers , indicating: a directed edge from to labeled with the left bracket of type ; and a directed edge from to labeled with the right bracket of type .
In the given graph, there may be multiple directed edges between any two different vertices, but there are no self-loops, i.e., .
Output Format
Output a single integer in one line, the number of vertex pairs that satisfy the condition.
4 5 1
4 3 1
4 1 1
4 2 1
1 3 1
2 1 1
3
6 8 2
6 1 2
3 5 1
1 2 2
5 1 2
3 6 2
4 3 1
6 2 2
3 2 1
10
见附件中的 bracket/bracket3.in
见附件中的 bracket/bracket3.ans
见附件中的 bracket/bracket4.in
见附件中的 bracket/bracket4.ans
Hint
[Sample Explanation #1]
The valid vertex pairs and their corresponding paths are:
: .
: .
: .
[Constraints]
For all test cases: , , , .
The specific limits for each test case are shown in the table below:
| Test Case ID | Special Restriction | |||
|---|---|---|---|---|
| None. | ||||
| There is no cycle consisting only of edges labeled with left brackets. | ||||
| None. |
Translated by ChatGPT 5
京公网安备 11011102002149号