C - 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 ci, connecting vertex ai and bi.

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

In the i-th query, he gives you two integers — ui and vi.

Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.

Input

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 — aibi (1 ≤ ai < bi ≤ n) and ci (1 ≤ ci ≤ 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, (ai, bi, ci) ≠ (aj, bj, cj).

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 — ui and vi (1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.

Output

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

Examples

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

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

Note

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.

题意:

给你两个数字,n,m,代表有n个顶点和m条边,接下来有m行,a,b,c,代表ab边之间有c颜色的边,需要注意的是每两个点之间不止一条边,但是同一颜色的边只有一条

然后一个q,代表有q个查询量,u,v,然后让你判断u,v两点之间有几种颜色可以到达,注意每一种可能只能走同一种颜色。

思路:

这是一道并查集的变式,我们要注意每一条边的颜色,所以我们可以开一个二维数组f【x】【y】,x代表颜色,y代表点,f【x】【y】代表x颜色的y的边的祖宗,然后进行并查集,合并,最后在判断两个顶点的每一种颜色的祖宗是否相同就可以了。

我错了两遍,因为我最后判断的时候把颜色的种类写成了n,尴尬。

代码如下:

#include<stdio.h>
#include<string.h>
#define N 200

int n,m,q;
int f[N][N];//代表颜色,点;
int a,b,c;

void init()//首先初始化,自己的祖宗是自己;
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            f[i][j]=j;
        }
    }
    return ;
}

int getf(int k,int t)//找祖宗;
{
    if(f[k][t]==t)
        return t;
    else
       return f[k][t]=getf(k,f[k][t]);
}

void merge(int k,int t,int v)//合并祖宗;
{
    int t1=getf(k,t);
    int t2=getf(k,v);
    if(t1!=t2)
        f[k][t2]=f[k][t1];
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int i,j,aa,bb,sum;
        init();
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            merge(c,a,b);
        }
        scanf("%d",&q);
        for(i=0;i<q;i++)
        {
            sum=0;
            scanf("%d%d",&aa,&bb);
            for(j=1;j<=m;j++)//简直气到炸,把颜色的种类写成了n;
            {
                if(getf(j,aa)==getf(j,bb))//祖宗相同,代表这种颜色能够到达;
                    sum++;
            }
            printf("%d\n",sum);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/81840746