Mr. Kitayuta's Colorful Graph

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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.

Sample Input

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

Hint

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<bits/stdc++.h>
#define maxx 105
using namespace std;
vector <int>edg[maxx][maxx];
bool vis[maxx];
int ans;
int x,y;
bool bfs(int c)
{
queue<int> q;
q.push(x);
int now;
memset(vis,,sizeof(vis));
while(!q.empty())
{
now=q.front();
if(now==y) return true;
q.pop();
for(int i=;i<edg[c][now].size();i++)
{
int then=edg[c][now][i];
if(vis[then]) continue;
vis[then]=;
q.push(then);
}
}
return false;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int a,b,c;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
edg[c][b].push_back(a);
edg[c][a].push_back(b);
}
int q;
scanf("%d",&q);
for(int i=;i<q;i++)
{
ans=;
scanf("%d%d",&x,&y);
for(int i=;i<m;i++)
if(bfs(i+)) ans++;
printf("%d\n",ans);
}
}

这道题不难理解,用bfs,保存每种颜色的边的关联的边,然后对每种颜色的边bfs,如果能够找到符合起点终点的边,结果就加一,知道找完所有的颜色的边。

据说这题还可以用并查集做。有机会看一下。学渣现在正处于学习阶段。

CodeForces 505B Mr. Kitayuta's Colorful Graph的更多相关文章

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

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

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

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

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

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

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

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

  5. 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/ ...

  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 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...

  9. B. Mr. Kitayuta's Colorful Graph

     B. Mr. Kitayuta's Colorful Graph  time limit per test 1 second Mr. Kitayuta has just bought an undi ...

随机推荐

  1. JS判断一个数组中是否有重复值的三种方法

    方法一: var s = ary.join(",")+","; for(var i=0;i<ary.length;i++) { if(s.replace( ...

  2. JavaScript 实现鼠标拖动元素

    一.前言 最开始实现鼠标拖动元素的目的就是在一个页面上拖动很多小圆点,用于固定定位,然后在复制HTML,粘贴在页面的开发代码中,就是这么一个功能,实现了很多遍,都没有做好,不得已采用了jQuery.f ...

  3. quick cocos 的scheduler 定时器

    cocos2dx原生lua对于定时器的写法: 1.每帧调用: void scheduleUpdateWithPriority(int priority) void scheduleUpdateWith ...

  4. Lua中的常用函数库汇总

    lua库函数 这些函数都是Lua编程语言的一部分, 点击这里了解更多. assert(value) - 检查一个值是否为非nil, 若不是则(如果在wow.exe打开调试命令)显示对话框以及输出错误调 ...

  5. 在Windows Server 2012 中安装 .NET 3.5 Framework

    问题 如今,仍然有许多程序和应用需要依靠.NET 3.5 framework 来运行.在Windows Server 2012中,微软提供了.NET 3.5 和.NET 4.5的安装选项以为你的应用程 ...

  6. (转)女生应该找一个玩ACM的男生

    1.强烈的事业心 将来,他也一定会有自己热爱的事业.而且,男人最性感的时刻之一,就是他专心致志做事的时候.所以,找一个机会在他全神贯注玩ACM的时候,从侧面好好观察他,你就会发现我说的话没错. 2.永 ...

  7. shell 生成指定范围随机数与随机字符串 .

    shell 生成指定范围随机数与随机字符串         分类:             shell              2014-04-22 22:17     20902人阅读     评 ...

  8. 72 [面试题]如果不使用if-else和比较运算符,你知道如何求解2个数字中的较大一个吗?

    [本文链接] http://www.cnblogs.com/hellogiser/p/max-of-numbers-without-comparations.html [题目] 不使用if-else和 ...

  9. 1.10 编程之美-双线程下载[double threads to download]

    [本文链接] http://www.cnblogs.com/hellogiser/p/double-threads-to-download-and-write.html [题目] 网络上下载数据,然后 ...

  10. Java for LeetCode 201 Bitwise AND of Numbers Range

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...