Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph (二维并查集)

B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

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 Copy
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 Copy
2
1
0
input Copy
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 Copy
1
1
1
1
2
Note

Let's consider the first sample.

Codeforces Round #286 (Div. 2)  B. Mr. Kitayuta's Colorful Graph (二维并查集)
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.

 

题意:

  给一张图两个结点之间的边有颜色,有q次询问,询问x,y两点直接或间接通过同一个颜色相连的路径有几条。

分析:

  学了二维并查集,算是一种新思想吧,第二维表示颜色。

Codeforces Round #286 (Div. 2)  B. Mr. Kitayuta's Colorful Graph (二维并查集)
///  author:Kissheart  ///
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e6+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
int n,m;
int pre[105][105];
int Find(int x,int y)
{
    return pre[x][y]==x?x:pre[x][y]=Find(pre[x][y],y);
}
int main()
{
    for(int i=1;i<105;i++)
        for(int j=1;j<105;j++)
            pre[i][j]=i;

    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        int fx=Find(x,z);
        int fy=Find(y,z);
        if(fx!=fy)
            pre[fx][z]=pre[fy][z];
    }
    int q;
    scanf("%d",&q);
    while(q--)
    {
        int x,y;
        int sum=0;

        scanf("%d%d",&x,&y);

        for(int i=1;i<=m;i++)
            if(Find(x,i)==Find(y,i))
                sum++;

        printf("%d\n",sum);
    }
    return 0;
}
View Code

 

上一篇:1、事例一 : 一个三角形、一个正方形(Vertex)


下一篇:运维流程系统