测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A9682. Mr. Kitayuta's Colorful Graph

编程题 普及/提高-

题目描述

Mr. Kitayuta has just bought an undirected graph consisting of $n$ vertices and $m$ edges. The vertices of the graph are numbered from 1 to $n$ . Each edge, namely edge $i$ , has a color $c_{i}$ , connecting vertex $a_{i}$ and $b_{i}$ .

Mr. Kitayuta wants you to process the following $q$ queries.

In the $i$ -th query, he gives you two integers — $u_{i}$ and $v_{i}$ .

Find the number of the colors that satisfy the following condition: the edges of that color connect vertex $u_{i}$ and vertex $v_{i}$ directly or indirectly.

输入格式

The first line of the input contains space-separated two integers — $n$ and $m$ ( $2<=n<=100,1<=m<=100$ ), denoting the number of the vertices and the number of the edges, respectively.

The next $m$ lines contain space-separated three integers — $a_{i}$ , $b_{i}$ ( $1<=a_{i}<b_{i}<=n$ ) and $c_{i}$ ( $1<=c_{i}<=m$ ). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if $i≠j$ , $(a_{i},b_{i},c_{i})≠(a_{j},b_{j},c_{j})$ .

The next line contains a integer — $q$ ( $1<=q<=100$ ), denoting the number of the queries.

Then follows $q$ lines, containing space-separated two integers — $u_{i}$ and $v_{i}$ ( $1<=u_{i},v_{i}<=n$ ). It is guaranteed that $u_{i}≠v_{i}$ .

输出格式

For each query, print the answer in a separate line.

输入输出样例

输入 #1
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4
输出 #1
2
1
0
输入 #2
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
输出 #2
1
1
1
1
2

说明/提示

Let's consider the first sample.

![](/uploads/acgo/image/d5ca8fc5a417a7fd_64ce45f83b1d.jpeg) The figure above shows the first sample. - Vertex $1$ and vertex $2$ are connected by color $1$ and $2$ .
- Vertex $3$ and vertex $4$ are connected by color $3$ .
- Vertex $1$ and vertex $4$ are not connected by any single color.
上一题 去做题 下一题