#SRD0000106. Đồ thị đa sắc của Kitayuta (Mr. Kitayuta's Colorful Graph)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đồ thị đa sắc của Kitayuta (Mr. Kitayuta's Colorful Graph)
Mr. Kitayuta's Colorful Graph
Source: Codeforces Version: Phuoc Hung OJ Extended
Problem Statement
Mr. Kitayuta owns an undirected multigraph with vertices numbered through and edges. Each edge joins two different vertices and has an integer color .
There may be multiple edges between the same two vertices if their colors differ. However, no two edges have the same triple with endpoints written in the order .
For each query consisting of two distinct vertices , count the colors such that and are connected by a path consisting only of edges of color . Edges of different colors cannot be mixed when evaluating a single color.
A pair may be connected by several different colors; each valid color contributes one to the answer.
Input
- Line 1:
n m. - The next lines:
a_i b_i c_idescribing an undirected edge of color . - The next line: number of queries
q. - The final lines:
u v, where .
Output
For each query, output the number of qualifying colors on a separate line.
Subtasks
- Subtask 1 (10 points): , , ; , ; distinct triples ; , .
- Subtask 2 (20 points): , , ; , ; distinct triples ; , .
- Subtask 3 (30 points): , , ; , ; distinct triples ; , .
- Subtask 4 (40 points): , , ; , ; distinct triples ; , .
Examples
Example 1
Input
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4
Output
2
1
0
Explanation
For , both colors and provide a path, so the answer is . For , color connects the vertices along , so the answer is . There is no monochromatic path from to , giving .
Example 2
Input
5 7
1 5 1
2 5 1
3 5 1
4 5 1
1 2 2
2 3 2
3 4 2
5
1 5
5 1
2 5
1 5
1 4
Output
1
1
1
1
2
Explanation
The four color- edges form a star centered at vertex , connecting all five vertices in color . Color- edges form the path . Thus, is connected in two colors, while any queried pair involving vertex is connected only in color .