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 — ai, bi (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
  1. 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
  1. 2
    1
    0
Input
  1. 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
    1
    2
qNote

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条边 两个点间可以有多条边 但是两点间相同颜色的边只能有一条  q个查询 判断 u-v间可以通过多少种颜色的边联通

题解:并查集处理

  1. #pragma comment(linker, "/STACK:102400000,102400000")
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <cctype>
  9. #include <map>
  10. #include <set>
  11. #include <queue>
  12. #include <bitset>
  13. #include <string>
  14. #include <complex>
  15. #define ll __int64
  16. #define mod 1000000007
  17. using namespace std;
  18. int n,m;
  19. int fa[][];
  20. int find(int root,int c)
  21. {
  22. if(fa[c][root]==root)
  23. return root;
  24. else
  25. return fa[c][root]=find(fa[c][root],c);
  26. }
  27. void unio(int a,int b,int c)
  28. {
  29. int aa=find(a,c);
  30. int bb=find(b,c);
  31. if(aa!=bb)
  32. fa[c][aa]=bb;
  33. }
  34. int main()
  35. {
  36. scanf("%d %d",&n,&m);
  37. for(int i=;i<=m;i++)
  38. for(int j=;j<=n;j++)
  39. fa[i][j]=j;
  40. int a,b,c;
  41. for(int i=;i<=m;i++)
  42. {
  43. scanf("%d %d %d",&a,&b,&c);
  44. unio(a,b,c);
  45. }
  46. int q;
  47. scanf("%d",&q);
  48. for(int i=;i<=q;i++)
  49. {
  50. scanf("%d %d",&a,&b);
  51. int ans=;
  52. for(int j=;j<=m;j++)
  53. {
  54. if(find(a,j)==find(b,j))
  55. ans++;
  56. }
  57. printf("%d\n",ans);
  58. }
  59. return ;
  60. }

Codeforces Round #286 (Div. 2) B 并查集的更多相关文章

  1. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

  2. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

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

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

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

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

  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. CodeForces Round #286 Div.2

    A. Mr. Kitayuta's Gift (枚举) 题意: 给一个长度不超过10的串,问能否通过插入一个字符使得新串成为回文串. 分析: 因为所给的串很多,所以可以枚举 “在哪插入” 和 “插入什 ...

  7. Codeforces Round #286 (Div. 1) 解题报告

    A.Mr. Kitayuta, the Treasure Hunter 很显然的一个DP,30000的数据导致使用map+set会超时.题解给了一个非常实用的做法,由于每个点有不超过250种状态,并且 ...

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

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

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

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

随机推荐

  1. 技本功丨用短平快的方式告诉你:Flink-SQL的扩展实现

    2019年1月28日,阿里云宣布开源“计算王牌”实时计算平台Blink回馈给ApacheFlink社区.官方称,计算延迟已经降到毫秒级,也就是你在浏览网页的时候,眨了一下眼睛,淘宝.天猫处理的信息已经 ...

  2. 联邦快递 IE和IP的区别 Fedex IE VS Fedex IP

    什么是FedEx IP? FedEx IP指的是联邦快递优先服务,时效比较快些,相对来说价格也比普通的高一些. 什么是FedEx IE? FedEx IE指的是联邦快递经济服务,时效与FedEx IP ...

  3. [linux] reboot和shutdown-r的区别

    google看看: 先搜英文的资料 http://askubuntu.com/questions/441969/what-is-the-difference-between-reboot-and-sh ...

  4. Python Pygame (2) 事件

    程序在运行期间会产生许许多多的事件,事件随时可能发生(如移动鼠标,点击鼠标,敲击键盘按键),Pygame的做法是将所有的事件都放到事件队列里,通过for循环语句迭代取出每一条事件,然后处理关注的事件即 ...

  5. c# webBrowser打开pdf问题

    1.生成模式使用release加*86尝试,使用debug则webBrowser不生效

  6. 团队开发——软件需求分析报告(Hello World 团队)

    一.   项目名称 超级迷宫 二.   设计背景 随着生活节奏加快,游戏更新速度的加快,游戏大同小异缺少新颖度,同时为了满足多游戏的结合,充实人们的生活,同时增加知识,有协作模式增进友谊和感情,在闲暇 ...

  7. myeclipse生成类的帮助文档

    http://blog.csdn.net/tabactivity/article/details/11807233

  8. 团队展示(I know)

    一.队员姓名与学号 姓名 学号 组长 陈家权 031502107 赖晓连 031502118 ★ 雷晶 031502119 林巧娜 031502125 庄加鑫 031502147 二.队名 I kno ...

  9. 《DWZ笔记一》<select>动态联动菜单

    联动菜单,即组合框Combo box,在DWZ文档中对组合框combox的是这样描述的: 在传统的select 用class 定义:class=”combox”, html 扩展:保留原有属性name ...

  10. WPF和Expression Blend开发实例:Loading动画

    今天来点实际的,项目中可以真实使用的,一个Loading的动画,最后封装成一个控件,可以直接使用在项目中,先上图: 整个设计比较简单,就是在界面上画18个Path,然后通过动画改变OpacityMas ...