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

  题意:给你一个图n个点,m条边;

     每条边有个颜色,每次u,v只能走一条颜色的路径;

     求有多少条路;

  思路:暴力dfs;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14http://v.ifeng.com/program/lyyy/
const int N=2e5+,M=1e6+,inf=1e9+,MOD=;
const ll INF=1e18+;
struct edge
{
int v,w;
int nex;
}edge[N];
int head[N],edg;
void init()
{
memset(head,-,sizeof(head));
edg=;
}
void add(int u,int v,int w)
{
++edg;
edge[edg].v=v;
edge[edg].w=w;
edge[edg].nex=head[u];
head[u]=edg;
}
int now,to,ans;
int flag[N];
int hh[N];
void dfs(int u,int pre)
{
//cout<<u<<" "<<pre<<endl;
if(u==to)
{
if(!hh[pre]&&pre!=-)
{
ans++; hh[pre]=;
}
return;
}
for(int i=head[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
if(flag[v])continue;
int w=edge[i].w;
if(pre==-||w==pre)
{
flag[v]=;
dfs(v,w);
flag[v]=;
}
}
}
int main()
{
init();
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
int q;
scanf("%d",&q);
while(q--)
{
memset(hh,,sizeof(hh));
ans=;
scanf("%d%d",&now,&to);
flag[now]=;
dfs(now,-);
flag[now]=;
printf("%d\n",ans);
}
return ;
}

Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs的更多相关文章

  1. Codeforces Round #286 (Div. 2)B. Mr. Kitayuta's Colorful Graph(dfs,暴力)

    数据规模小,所以就暴力枚举每一种颜色的边就行了. #include<iostream> #include<cstdio> #include<cstdlib> #in ...

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

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

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

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

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

  5. 水题 Codeforces Round #286 (Div. 2) A Mr. Kitayuta's Gift

    题目传送门 /* 水题:vector容器实现插入操作,暴力进行判断是否为回文串 */ #include <cstdio> #include <iostream> #includ ...

  6. CF 286(div 2) B Mr. Kitayuta's Colorful Graph【传递闭包】

    解题思路:给出n个点,m条边(即题目中所说的两点之间相连的颜色) 询问任意两点之间由多少种不同的颜色连接 最开始想的时候可以用传递闭包或者并查集来做,可是并查集现在还不会做,就说下用传递闭包来做的这种 ...

  7. Codeforces Round #286 (Div. 1) B. Mr. Kitayuta&#39;s Technology (强连通分量)

    题目地址:http://codeforces.com/contest/506/problem/B 先用强连通判环.然后转化成无向图,找无向图连通块.若一个有n个点的块内有强连通环,那么须要n条边.即正 ...

  8. Codeforces Round #286 Div.1 A Mr. Kitayuta, the Treasure Hunter --DP

    题意:0~30000有30001个地方,每个地方有一个或多个金币,第一步走到了d,步长为d,以后走的步长可以是上次步长+1,-1或不变,走到某个地方可以收集那个地方的财富,现在问走出去(>300 ...

  9. Codeforces Round #286 (Div. 2)A. Mr. Kitayuta's Gift(暴力,string的应用)

    由于字符串的长度很短,所以就暴力枚举每一个空每一个字母,出现行的就输出.这么简单的思路我居然没想到,临场想了很多,以为有什么技巧,越想越迷...是思维方式有问题,遇到问题先分析最简单粗暴的办法,然后一 ...

随机推荐

  1. python 去除字符串中连续的空格,并使用其他的分隔符替代连续的空格

    例:1: filt函数用法及匿名函数lamda用法,详见http://www.cnblogs.com/apple2016/p/5657698.html join()用法详见http://www.cnb ...

  2. svn使用相关问题:eclipse插件,加锁,解锁,偷锁,更新不了,记住密码

    svn使用相关问题:eclipse插件,加锁,解锁,偷锁,更新不了,记住密码 获取锁的时候可以看下 是谁锁住了,让对方提交解锁,如果是给离职人员锁住需要使用偷锁的方式先解锁再提交偷锁处理办法:选中该文 ...

  3. 从追MM谈Java的23种设计模式(转)

    从追MM谈Java的23种设计模式    这个是从某个文章转载过来的.但是忘了原文链接.如果知道的,我追加一下. 1.FACTORY-追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西 ...

  4. HDU 3076:ssworld VS DDD(概率DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3076 ssworld VS DDD Problem Description   One day, s ...

  5. WPF:获取控件内的子项

    一.界面内容(部分:仅供参考) <Window> <Window.Resources> <!--工具数据源--> <XmlDataProvider x:Key ...

  6. C#:向控件添加信息类

    using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; names ...

  7. C#Winform常引用的库

    1.DevExpress:引用其下的不同DLL,可操作使用不同的控件 2.IrisSkin2:引用对应的DLL后,代码如下 private Sunisoft.IrisSkin.SkinEngine s ...

  8. include<stdio.h> 和include<iostream.h>的区别

    stdio 是C标准库里面的函数库 对应的基本都是标准输入输出等等C语言常用库的定义iostream是C++标准库的头定义, 对应的基本上是C++的输入输出相关库定义开发C程序用Stdio, C++用 ...

  9. ACM题目————小A的计算器

    Description 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-25分别由a-z表示.  现在小A要在这个操作系统上实现一个计算器,这 ...

  10. Capturing Audio & Video in HTML5

    使用HTML5抓取 Audio & Video 原文地址: http://www.html5rocks.com/en/tutorials/getusermedia/intro/ 本地化的文章: ...