题干:

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.

解题报告:

       

ac代码1:(并查集)(15ms,0.1MB)

   原来的代码加上了ra数组,至今不明白是干啥的。

#include <iostream>
using namespace std;
#define MAXN 110
int pa[110][110],ra[110][110];
void init() {
	for(int i=0; i<MAXN; i++) {
		for(int j=0; j<MAXN; j++) {
			pa[i][j]=j;
			ra[i][j]=0;
		}
	}
}
int getf(int x,int c) {
	if(pa[c][x]!=x)pa[c][x]=getf(pa[c][x],c);
	return pa[c][x];
}
void merge(int x,int y,int c) {
	int t1=getf(x,c);
	int t2=getf(y,c);
	if(t1==t2) return;
	else {
		pa[c][t2]=pa[c][t1];
	}
//	if(ra[c][t1]<ra[c][t2]) {
//		pa[c][t1]=t2;
//	} else {
//		pa[c][t2]=t1;
//		if(ra[c][t1]==ra[c][t2])ra[c][t1]++;
//	}
}
bool same(int x,int y,int c) {
	return getf(x,c)==getf(y,c);
}

int main() {
	ios::sync_with_stdio(false);
	int n,m;
	init();
	cin>>n>>m;
	int u,v,c;
	for(int i=0; i<m; i++) {
		cin>>u>>v>>c;
		u--,v--,c--;
		merge(u,v,c);
	}
	int q;
	cin>>q;
	for(int i=0; i<q; i++) {
		cin>>u>>v;
		u--;
		v--; 
		int ans=0;
		for(int i=0; i<m; i++) {
			if(same(u,v,i))ans++;
		}
		cout<<ans<<endl;
	}

	return 0;
}

ac代码2:(广搜bfs)(31ms,0.3MB)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#define MAX 107

using namespace std;

int n,m,c,x,y,q;
vector<int> e[MAX][MAX];
bool vis[MAX];

void add ( int u , int v , int w )
{
    e[w][u].push_back ( v );
    e[w][v].push_back ( u );
}

bool bfs ( int c )
{
    memset ( vis , 0 , sizeof ( vis ) );
    queue<int> q;
    q.push ( x );
    while ( !q.empty() )
    {
        int u = q.front();
        if ( u == y ) return true;
        q.pop();
        for ( int i = 0 ; i < e[c][u].size() ; i++ )
        {
            int v = e[c][u][i];
            if ( vis[v] ) continue;
            vis[v] = true;
            q.push ( v );
        }
    }
    return false;
}

int main ( )
{
    while ( ~scanf ( "%d%d" , &n , &m ) )
    {
        for ( int i = 0 ; i < MAX ; i++ )
            for ( int j = 0 ; j < MAX ; j++ )
                e[i][j].clear();
        for ( int i = 0 ; i < m ; i++ )
        {
            scanf ( "%d%d%d" , &x , &y , &c );
            add ( x , y , c );
        }
        scanf ( "%d" , &q );
        while ( q-- )
        {
            int ans = 0;
            scanf ( "%d%d" , &x , &y );
            for ( int i = 1 ; i <= m ; i++ )
                if ( bfs ( i ) ) ans++;
            printf ( "%d\n" , ans );
        }
    }
}

ac代码3:(Floyd)

#include<bits/stdc++.h>

using namespace std;
const int MAX = 100 + 5;

int maze[MAX][MAX][MAX];
bool bk[MAX];
int main()
{
	int n,m;
	int a,b,c;
	int q,cnt;
	scanf("%d %d",&n,&m);
	while(m--) {
		scanf("%d %d %d",&a,&b,&c);
		maze[a][b][c]=1;
		maze[b][a][c]=1;
		bk[c]=1;
	}
	for(int c = 1; c<=MAX; c++) {//看看这个颜色有没有出现过 //他确实是输入m条路所以最多有m个颜色,但是你不能搜索到m啊!! 
		if(bk[c]==0) continue;
		for(int k = 1; k<=n; k++) {
			for(int i = 1; i<=n; i++) {
				for(int j = 1; j<=n; j++) {
					if( maze[i][k][c]==1&&maze[k][j][c]==1) {
						maze[i][j][c]=1;
					}
				}
			}
		}
	}
	scanf("%d",&q);
	while(q--) {
		cnt=0;
		scanf("%d %d",&a,&b);
		for(int c = 1; c<=MAX; c++) {
			if(bk[c]==1 && maze[a][b][c] == 1) {
				cnt++;
			}
		}
		printf("%d\n",cnt);
	}
	return 0 ;
}

总结: