给出一个 n n n 个点, m m m 条边的无向图,每条边上是有颜色的。有 q q q 组询问
对于第 i i i 组询问,给出点对 u i , v i u_i,v_i ui,vi。求有多少种颜色 c c c 满足:有至少一条 u i u_i ui 到 v i v_i vi 路径,满足该路径上的所有边的颜色都为 c c c
第一行两个整数 n , m n,m n,m 分别表示点的个数和边的个数
接下来 m m m 行,每行三个整数 x i , y i , c i x_i,y_i,c_i xi,yi,ci,表示有一条连接点 x i , y i x_i,y_i xi,yi 的边,且该边的颜色为 c i c_i ci
接下来一行一个整数 q q q,表示询问的个数
接下来 q q q 行,每行两个整数 u i , v i u_i,v_i ui,vi,表示一组询问
对于每一组询问,在单独的一行输出一个整数,表示满足上述要求的颜色种数
2 ≤ n ≤ 100 2 \le n \le 100 2≤n≤100
1 ≤ m , q ≤ 100 1 \le m,q \le 100 1≤m,q≤100
1 ≤ x i , y i , u i , v i ≤ n 1\le x_i,y_i,u_i,v_i \le n 1≤xi,yi,ui,vi≤n
1 ≤ c i ≤ m 1 \le c_i \le m 1≤ci≤m
感谢 @_Wolverine 提供的翻译
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} 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. Let’s consider the first sample. The figure above shows the first sample. - Vertex $ 1 $ and vertex $ 2 $ are connected by color $ 1 $ and $ 2 $ . (1)并查集 (1)并查集 (2)Floyed输出格式
样例 #1
样例输入 #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
样例输入 #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
提示
思路
一个二维并查集,一个记录颜色,一个记录点。
(2)Floyed
普通Floyed
加一维颜色。数据只有100四维循环不会T。AC code
#include
#include