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. linux端口

    1.查看开放的端口 netstat -anp 来查看哪些端口被打开. 注:加参数'-n'会将应用程序转为端口显示,即数字格式的地址,如:nfs->2049, ftp->21,因此可以开启两 ...

  2. GPS模块的AT指令集

    AT+CPIN? 查询sim卡状态. 接着: AT+CGCLASS="B"AT+CGDCONT=1,"IP","CMNET"AT+CGATT ...

  3. 安装SQL Server 2005

    在安装SQL Server 2005时,经常会遇到一些错误,从而使系统无法正常安装.下面讲解在安装过程中经常出现的一些错误及其解决的方法.1.解决在安装SQL Server 2005时安装程序被挂起的 ...

  4. ansible

    3.1 配置 #vim /etc/ansible/hosts       //定义主机,支持IP和域名,支持分组 [local] 127.0.0.1 [nginx] 192.168.0.10     ...

  5. Hibernate,Session清理缓存时间点

    当应用程序调用org.hibernate.Transaction的commit()的时候,commit()方法先清理缓存,然后再向数据库提交事务. 当应用程序显示调用Session.flush()方法 ...

  6. Effective STL

    第9条:慎重选择删除元素的方法 删除特定值元素,vector.string.deque用erase-remove:c.erase(remove(c.begin(),c.end(),1963),c.en ...

  7. [ios][opengles]GLKit如何搭一个app的框架

    一个外文对GLKit的讲解: Beginning OpenGL ES 2.0 with GLKit Part 1    英文原文链接:http://www.raywenderlich.com/5223 ...

  8. ACM题目————Find them, Catch them

    Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...

  9. YTU 3013: 皇后问题(递归)

    3013: 皇后问题(递归) 时间限制: 1 Sec  内存限制: 128 MB 提交: 2  解决: 2 题目描述 编写一个函数,求解皇后问题:在n*n的方格棋盘上,放置n个皇后,要求每个皇后不同行 ...

  10. jquery 实现页面局部刷新ajax做法

    这个方法就多了去了,常见的有以下几种:下面介绍全页面刷新方法:有时候可能会用到 window.location.reload()刷新当前页面. parent.location.reload()刷新父亲 ...