Mr. Kitayuta has just bought an undirected graph consisting of n vertices and medges. 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.
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e5+;
typedef long long ll;
using namespace std;
struct node
{
int v;
int val;
};
int ans=;
int vis[];
vector<node>vec[];
void bfs(int x,int y)
{
node start;
for(int j=;j<=;j++)
{
queue<node>q;
memset(vis,,sizeof(vis));
vis[x]=;
for(int t=;t<vec[x].size();t++)
{
if(vec[x][t].val==j&&vis[vec[x][t].v]==)
{
q.push(vec[x][t]);
vis[vec[x][t].v]=;
}
}
while(!q.empty())
{
node now=q.front();
q.pop();
if(now.v==y)
{
ans++;
}
for(int t=;t<vec[now.v].size();t++)
{
if(vec[now.v][t].val==j&&vis[vec[now.v][t].v]==)
{
vis[vec[now.v][t].v]=;
q.push(vec[now.v][t]);
}
}
}
}
}
int main()
{
int n,m;
cin>>n>>m; int a,b,c;
set<int>s;
for(int t=;t<m;t++)
{
scanf("%d%d%d",&a,&b,&c);
s.insert(c);
node st;
st.v=b;
st.val=c;
vec[a].push_back(st);
st.v=a;
st.val=c;
vec[b].push_back(st);
}
int q;
cin>>q;
int uu,vv;
for(int t=;t<q;t++)
{
scanf("%d%d",&uu,&vv);
ans=;
bfs(uu,vv);
printf("%d\n",ans);
} return ;
}

CodeForces - 505B-Mr. Kitayuta's Colorful Graph(暴力)的更多相关文章

  1. CodeForces 505B Mr. Kitayuta's Colorful Graph

    Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  2. codeforces 505B Mr. Kitayuta's Colorful Graph(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...

  3. CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集

    Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...

  4. Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)

    题目链接  Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$    涉及的点的个数 $<= ...

  5. CodeForces 506D Mr. Kitayuta's Colorful Graph

    brute force ? 其实是平方分解.很容易想到的是每一个颜色建一个图,然后并查集维护一下连通性. 问题在于颜色有O(m)种,每种颜色的图点数都是O(n)的,因此并查集的空间只能重复利用. 但是 ...

  6. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  7. Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs

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

  8. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集

    D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...

  9. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph

    D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...

  10. Mr. Kitayuta's Colorful Graph 多维并查集

    Mr. Kitayuta's Colorful Graph 并查集不仅可以用于一维,也可以用于高维. 此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的 ...

随机推荐

  1. SpringMvc响应数据和结果视图

    响应数据和结果视图 返回值分类 字符串 controller 方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址. //指定逻辑视图名,经过视图解析器解析为 jsp 物理路径:/WEB ...

  2. [vue] computed 和 method

    计算属性 计算属性只有在它的相关依赖发生改变时才会重新取值 Method method每次渲染的时候都会被执行 举一个栗子 <template>...<div>  <p& ...

  3. Python 3.9 beta2 版本发布了,看看新特性?

    随着 Python 3.9.0b1 的发布,即开发周期中计划的四个 beta 版本的首个,Python 3.9 的功能已经是完善了.在 10 月发布最终版本之前,还会有许多测试和稳定性方面的工作要做. ...

  4. 【lhyaaa】图的存储&遍历

    呀,图真是一个令人头疼而又很重要的东西.在现实生活中,我们有很多的问题都不能用树来实现,所以烦人啊不伟大的图就出现了—— 图的存储 没有存储哪来的操作,所以存储是最基础的呢. 邻接矩阵 我们对于图的存 ...

  5. Java查表法实现十进制转化成其它进制

    首先了解十进制转化成二级制的原理 156的二进制为: 156 % 2 = 78 …… 0 83 % 2 = 39 …… 0 39 % 2 = 19 …… 1 19 % 2 = 9 …… 1 9 % 2 ...

  6. Python爬虫获取百度贴吧图片

    #!/usr/bin/python# -*- coding: UTF-8 -*-import urllibimport re文章来源:https://www.cnblogs.com/Axi8/p/57 ...

  7. 【MySQL】如何最大程度防止人为误操作MySQL数据库?这次我懂了!!

    写在前面 今天,一位哥们打电话来问我说误操作了他们公司数据库中的数据,如何恢复.他原本的想法是登录数据库update一个记录,结果忘了加where条件,于是悲剧发生了.今天,我们不讲如何恢复误操作的数 ...

  8. golang 整型

    目录 前言 整型 1.分类 2.类型 3.类型补充 4.补充: 跳转 前言 不做文字的搬运工,多做灵感性记录 这是平时学习总结的地方,用做知识库 平时看到其他文章的相关知识,也会增加到这里 随着学习深 ...

  9. MyKTV系统项目的感想

    不粉身碎骨,何以脱胎换骨! 3月11号,我们迎来S1的尾巴.这期间有温暖,默契,有项目.一切刚刚好.刚刚正式接到KTV这个微微型的项目的时候,还是很害怕的,虽然老师在前两天就已经提到也讲到,KTV系统 ...

  10. 只要动手就能学到东西4 JSON.stringify

    今天在调试javascript时,需要将不可显示字符如\r\n在console显示出来,查了下,原来可以用JSON.stringify().这个方法以前也用过,但都是将javascript对象变成js ...