题干:

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.

解题报告:

ac代码1:(并查集)(15ms,0.1MB)

原来的代码加上了ra数组,至今不明白是干啥的。

#include <iostream>
using namespace std;
#define MAXN 110
int pa[110][110],ra[110][110];
void init() {for(int i=0; i<MAXN; i++) {for(int j=0; j<MAXN; j++) {pa[i][j]=j;ra[i][j]=0;}}
}
int getf(int x,int c) {if(pa[c][x]!=x)pa[c][x]=getf(pa[c][x],c);return pa[c][x];
}
void merge(int x,int y,int c) {int t1=getf(x,c);int t2=getf(y,c);if(t1==t2) return;else {pa[c][t2]=pa[c][t1];}
//  if(ra[c][t1]<ra[c][t2]) {
//      pa[c][t1]=t2;
//  } else {
//      pa[c][t2]=t1;
//      if(ra[c][t1]==ra[c][t2])ra[c][t1]++;
//  }
}
bool same(int x,int y,int c) {return getf(x,c)==getf(y,c);
}int main() {ios::sync_with_stdio(false);int n,m;init();cin>>n>>m;int u,v,c;for(int i=0; i<m; i++) {cin>>u>>v>>c;u--,v--,c--;merge(u,v,c);}int q;cin>>q;for(int i=0; i<q; i++) {cin>>u>>v;u--;v--; int ans=0;for(int i=0; i<m; i++) {if(same(u,v,i))ans++;}cout<<ans<<endl;}return 0;
}

ac代码2:(广搜bfs)(31ms,0.3MB)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#define MAX 107using namespace std;int n,m,c,x,y,q;
vector<int> e[MAX][MAX];
bool vis[MAX];void add ( int u , int v , int w )
{e[w][u].push_back ( v );e[w][v].push_back ( u );
}bool bfs ( int c )
{memset ( vis , 0 , sizeof ( vis ) );queue<int> q;q.push ( x );while ( !q.empty() ){int u = q.front();if ( u == y ) return true;q.pop();for ( int i = 0 ; i < e[c][u].size() ; i++ ){int v = e[c][u][i];if ( vis[v] ) continue;vis[v] = true;q.push ( v );}}return false;
}int main ( )
{while ( ~scanf ( "%d%d" , &n , &m ) ){for ( int i = 0 ; i < MAX ; i++ )for ( int j = 0 ; j < MAX ; j++ )e[i][j].clear();for ( int i = 0 ; i < m ; i++ ){scanf ( "%d%d%d" , &x , &y , &c );add ( x , y , c );}scanf ( "%d" , &q );while ( q-- ){int ans = 0;scanf ( "%d%d" , &x , &y );for ( int i = 1 ; i <= m ; i++ )if ( bfs ( i ) ) ans++;printf ( "%d\n" , ans );}}
}

ac代码3:(Floyd)

#include<bits/stdc++.h>using namespace std;
const int MAX = 100 + 5;int maze[MAX][MAX][MAX];
bool bk[MAX];
int main()
{int n,m;int a,b,c;int q,cnt;scanf("%d %d",&n,&m);while(m--) {scanf("%d %d %d",&a,&b,&c);maze[a][b][c]=1;maze[b][a][c]=1;bk[c]=1;}for(int c = 1; c<=MAX; c++) {//看看这个颜色有没有出现过 //他确实是输入m条路所以最多有m个颜色,但是你不能搜索到m啊!! if(bk[c]==0) continue;for(int k = 1; k<=n; k++) {for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if( maze[i][k][c]==1&&maze[k][j][c]==1) {maze[i][j][c]=1;}}}}}scanf("%d",&q);while(q--) {cnt=0;scanf("%d %d",&a,&b);for(int c = 1; c<=MAX; c++) {if(bk[c]==1 && maze[a][b][c] == 1) {cnt++;}}printf("%d\n",cnt);}return 0 ;
}

总结:

【CF#505B】Mr. Kitayuta's Colorful Graph (并查集或Floyd或BFS)相关推荐

  1. 505B. Mr. Kitayuta‘s Colorful Graph

    B. Mr. Kitayuta's Colorful Graph:题目 一开始就像到了DFS,并查集也不难想到. 弗洛伊德算法倒是不容易想到,平时不这么用..... #include <bits ...

  2. 根号分治 ---- D. Mr. Kitayuta‘s Colorful Graph(根号均摊复杂度 + 数据结构维护)

    题目链接 题目大意: 给你一个nnn个点mmm条边的无向图,每条边上面有个颜色cic_ici​,每次询问给你一个(u,v)(u,v)(u,v),问你有多少种颜色使得至少有一条路径从(u→v)(u\ri ...

  3. AGC012D - Colorful Balls(并查集)

    AGC012D - Colorful Balls Solution 连边题. 找出www最小的球yyy和www最小且颜色和yyy不同的球zzz. yyy向所有colt≠coly,wt+wy≤Ycol ...

  4. 牛客多校3 - Operating on a Graph(并查集+链表合并)

    题目链接:点击查看 题目大意:给出一个由 n 个点和 m 条边组成的无向图,每个点初始时都有颜色 i ,i ∈ [ 0 , n - 1 ] ,接下来有 q 次操作,每次操作会给出一种颜色 x ,分两种 ...

  5. Harmonious Graph(并查集)

    思路 题意:n个点,m条边,组成若干个连通图,如果一个点x能到另一个点y,那么如果(x,y)中任意一个点,都能到y,就称这个图为和谐图,求需要连多少个边,使得所有连通图成为和谐图 做法:用并查集存下所 ...

  6. 【每日一题】8月12日题目精讲 Mr. Kitayuta, the Treasure Hunter

    来源:牛客网: 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 The Shuseki I ...

  7. Mr. Kitayuta‘s Technology CodeForces - 505D(并查集+拓扑排序或dfs找环) 题解

    题目  Shuseki Kingdom is the world's leading nation for innovation and technology. There are n cities ...

  8. 【IOI2020国家集训队作业 Part 1】CF505E Mr. Kitayuta vs. Bamboos

    题目 题目描述 Mr. Kitayuta's garden is planted with nn bamboos. (Bamboos are tall, fast-growing tropical p ...

  9. CF506C Mr. Kitayuta vs. Bamboos

    CF506C Mr. Kitayuta vs. Bamboos 有nnn个竹子,第iii棵竹子第一天之前的高度是hih_ihi​,每一天的末尾会长高aia_iai​ 每一天你可以将砍kkk刀,每一刀将 ...

最新文章

  1. aliyun redis 链接超时_用redis做异步队列,原来还可以这样
  2. 常用数据绑定控件详解
  3. Spring IOC实现原理
  4. linux-新建一个centos虚拟机系统-安装全过程图示
  5. zabbix自动同步ldap帐号到数据库
  6. UISearchController使用方法及注意事项
  7. 税控服务器组件接口v2.1.1.1,税控开票服务器组件接口规范标准版V1.9(2016.04.04).pdf...
  8. 龙果学院从无到有构建亿级微服务秒杀系统
  9. 单声道120W大功率D类功放芯片CS8683-TPA3116对比测试
  10. EIA/TIA布线标准(568A、568B)
  11. C++实现走迷宫算法(1)
  12. 计算机网络在资源共享信息交换的体会,计算机网络学习心得体会.doc
  13. html的color粉颜色,HTML颜色一览(color)
  14. 自学python经验_我学Python的经验,Python学习经验分享
  15. linux MySQL操作
  16. 风雨十年:一个老程序员的心里话!
  17. python unpacking_python packing unpacking 组包解包
  18. 单源最短路径(1):Dijkstra算法
  19. 小米5怎么安android,小米5怎么插卡 小米5手机安装sim卡图文教程
  20. 在newsmth注册了!

热门文章

  1. WCF从理论到实践(14):WCF解决方案模板 (转)
  2. PAT 1114 Family Property 并查集
  3. javascript用户登录_SAP HANA XS的JavaScript安全事项
  4. linux命令 sed 有的功能有,Linux命令:sed简介
  5. 条形图坐标轴_解密咨询报告中常见的双层条形图的制作方法
  6. git checkout 单个文件_IntelliJ IDEA下的使用 Git
  7. mysql集群重启报错lock_CentOS7.2 下 MySQL 之 PXC 集群部署【Docker+单机多节点】
  8. 爬虫技术python流程图_基于Python的网络爬虫技术研究
  9. Linux下boost库的安装
  10. websocket多人聊天php,php-notes/基于websocket实现多人聊天室.md at master · dd-code-site/php-notes · GitHub...