CF505B.Mr. Kitayuta's Colorful Graph

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

小野寺先生刚刚购买了一个由 nn 个顶点和 mm 条边构成的无向图。图中的顶点编号为 11 到 nn。每条边(即第 ii 条边)具有颜色 cic_i,并连接顶点 aia_i 和 bib_i。

小野寺先生希望你处理以下 qq 个查询。

在第 ii 个查询中,他会给出两个整数 uiu_i 和 viv_i。

请找出满足如下条件的颜色种数:该颜色的所有边(仅考虑该颜色的边构成的子图)能够使顶点 uiu_i 和顶点 viv_i 直接或间接连通。

输入格式

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.

输入的第一行包含两个用空格分隔的整数 nn 和 mm(2≤n≤1002 \leq n \leq 100,1≤m≤1001 \leq m \leq 100),分别表示顶点数和边数。

接下来的 mm 行每行包含三个用空格分隔的整数 aia_i、bib_i(1≤ai<bi≤n1 \leq a_i < b_i \leq n)和 cic_i(1≤ci≤m1 \leq c_i \leq m)。注意:两个顶点之间可能存在多条边。但任意两个顶点之间不会存在两条颜色相同的边,即若 i≠ji \neq j,则 (ai, bi, ci)≠(aj, bj, cj)(a_i,\,b_i,\,c_i) \neq (a_j,\,b_j,\,c_j)。

下一行包含一个整数 qq(1≤q≤1001 \leq q \leq 100),表示查询次数。

随后是 qq 行,每行包含两个用空格分隔的整数 uiu_i 和 viv_i(1≤ui, vi≤n1 \leq u_i,\,v_i \leq n)。保证 ui≠viu_i \neq 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.

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.

我们来考虑第一个样例。

上图展示了第一个样例。

  • 顶点 1 和顶点 2 通过颜色 1 和颜色 2 相连。
  • 顶点 3 和顶点 4 通过颜色 3 相连。
  • 顶点 1 和顶点 4 之间不存在某种单一颜色的路径相连。

输入解题思路,AI测评打分。不知道怎么写?

首页