Mr. Kitayuta's Colorful Graph(巧用并查集)

来源:互联网 发布:淘宝卖家充值平台 编辑:程序博客网 时间:2024/06/07 12:10

Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 ton. Each edge, namely edge i, has a color ci, connecting vertexai andbi.

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

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

Find the number of the colors that satisfy the following condition: the edges of that color connect vertexui and vertexvi 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 —ai,bi (1 ≤ ai < bi ≤ n) andci (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 andvi (1 ≤ ui, vi ≤ n). It is guaranteed thatui ≠ vi.

Output

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

Examples
Input
4 51 2 11 2 22 3 12 3 32 4 331 23 41 4
Output
210
Input
5 71 5 12 5 13 5 14 5 11 2 22 3 23 4 251 55 12 51 51 4
Output
11112
Note

Let's consider the first sample.

The figure above shows the first sample.
  • Vertex 1 and vertex 2 are connected by color1 and 2.
  • Vertex 3 and vertex 4 are connected by color3.
  • Vertex 1 and vertex 4 are not connected by any single color.

题意:

一个无向图包含n个点m条边,顶点编号从1到n。  对于每条边有颜色ci,  连接着顶点 ai 和  b i.

下面有q个询问.

每条询问有两个整数 — ui 和 vi.

找到满足下面条件的颜色个数同一种颜色的路径连接顶点 ui 和 顶点 vi

思路:

将不同颜色的同一个点看成不同点,开一个二维并查集,既然f[x]储存的是x的父亲节点,那么我们用f[][],第一维表示颜色,第二维便是节点x,我们就用f[c][x]表示与颜色都为c的x的父亲节点,这样,在输入的时候,直接将颜色相同的两个点用并查集联通起来

代码:

#include<stdio.h>#include<algorithm>#define MAX 105using namespace std;int n,m;int pre[MAX][MAX];//pre[c][j]表示与颜色都为c的x的父亲节点void init(){     int i,j;     for(i=0;i<=m;i++)//最多m种颜色        for(j=0;j<=n;j++)//顶点数          pre[i][j]=j; //颜色是i的j点父亲是j点本身}int Find(int c,int x){  //寻找颜色为c 的x的父亲    int r=x;    while(r!=pre[c][r])        r=pre[c][r];    int i=x,j;    while(i!=r){  //路径压缩        j=pre[c][i];        pre[c][i]=r;        i=j;    }    return r;}void mix(int a,int b,int c){ //将颜色为c的a,b两点连起来    int fa=Find(c,a),fb=Find(c,b);    if(fa!=fb){       pre[c][fb]=fa;    }}int sea(int x,int y,int col){  //查看是否颜色为col 的x,y相连    return Find(col,x)==Find(col,y);}int main(){    scanf("%d%d",&n,&m);    init();    int i;    int maxx=-1;    for(i=0;i<m;i++){        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        maxx=max(maxx,c); //记录所有颜色的最大值        mix(a,b,c);    }    int k;    scanf("%d",&k);    int j;    for(i=0;i<k;i++){        int a,b;        scanf("%d%d",&a,&b);        int cnt=0;        for(j=0;j<=maxx;j++) //枚举颜色,判断是否有同一颜色的a,b相连           cnt+=sea(a,b,j);        printf("%d\n",cnt);    }return 0;}


阅读全文
1 0
原创粉丝点击