CF-Mr. Kitayuta's Colorful Graph

时间:2021-11-07 07:21:18
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.

Sample test(s)
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.

CF-Mr. Kitayuta's Colorful Graph 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.

思路:

很不错的一道题目。

首先看他最后的那个问题“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.”找出可以连通两点的个数,那个可以把这个问题退一步——相同颜色的点怎样才算是连通?

其实这就是一道更高维度的并查集,从而更好的揭示了并查集的实质:两个点的连通问题——他们连通的条件是什么?

这道题是对这一隐藏现象的揭示


#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 207
using namespace std; int set[maxn][maxn]; int init()
{
for(int i = ;i <= maxn;i++)
for(int j = ;j <= maxn;j++)
set[i][j] = j;
} int find(int color,int x)
{
int i;
for(i = x;i != set[color][i];i = set[color][i])
set[color][i] = set[color][set[color][i]];
return i;
} void set_together(int color,int t1,int t2)
{
int fx = find(color,t1);
int fy = find(color,t2);
if(fx != fy)
set[color][fx] = fy;
} int main()
{
int q;
int n,m;
int t1,t2,t;
int ans;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i = ;i <= m;i++)
{
scanf("%d%d%d",&t1,&t2,&t);
set_together(t,t1,t2);
}
scanf("%d",&q);
while(q--)
{
ans = ;
scanf("%d%d",&t1,&t2);
for(int i = ;i <= m;i++)
if(find(i,t1) == find(i,t2)) ans++;
cout<<ans<<endl;
}
}
return ;
}

CF-Mr. Kitayuta's Colorful Graph的更多相关文章

  1. CF 286&lpar;div 2&rpar; B Mr&period; Kitayuta&&num;39&semi;s Colorful Graph【传递闭包】

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

  2. CodeForces 505B Mr&period; Kitayuta&&num;39&semi;s Colorful Graph

    Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  3. Codeforces Round &num;286 &lpar;Div&period; 2&rpar; B&period; Mr&period; Kitayuta&&num;39&semi;s Colorful Graph dfs

    B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...

  4. Codeforces Round &num;286 &lpar;Div&period; 1&rpar; D&period; Mr&period; Kitayuta&&num;39&semi;s Colorful Graph 并查集

    D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...

  5. B&period; Mr&period; Kitayuta&&num;39&semi;s Colorful Graph

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

  6. Mr&period; Kitayuta&&num;39&semi;s Colorful Graph 多维并查集

    Mr. Kitayuta's Colorful Graph 并查集不仅可以用于一维,也可以用于高维. 此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的 ...

  7. codeforces 505B Mr&period; Kitayuta&&num;39&semi;s Colorful Graph&lpar;水题&rpar;

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

  8. Codeforces Round &num;286 &lpar;Div&period; 1&rpar; D&period; Mr&period; Kitayuta&&num;39&semi;s Colorful Graph

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

  9. Codeforces 506D Mr&period; Kitayuta&&num;39&semi;s Colorful Graph(分块 &plus; 并查集)

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

  10. CodeForces - 505B Mr&period; Kitayuta&&num;39&semi;s Colorful Graph 二维并查集

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

随机推荐

  1. Eclipse调试 : step into&comma;step over&comma;step return 说明

    step into : 单步执行,遇到子函数就进入并且继续单步执行(F5) step over:   在单步执行时,在函数内遇到子函数时不会进入子函数内单步执行,而是将子函数整个执行完在停止,也就是把 ...

  2. iOS runtime 初步学习

    注: 在Xocde5之后, 使用运行时方法需要进行2步设置1. 在Build Setting中搜索'msg', 设置'Strict Checking' 为 NO2. 使用需要导入头文件 #import ...

  3. 深入理解css系列:meta标签

    积累太少,时间管理技巧欠缺,所以导致了博客更新的速度迟缓.学习中成长,成长中学习.加油吧!最近在做h5的项目,对于meta标签层出不穷的各式属性值有点头晕,所以查资料整理了下. 关键字:meta na ...

  4. BCB6 重装后的项目编译莫名问题

    我很少用 bcb ,重装 bcb6 后原来的项目居然不能编译成功了,看了一下是控件的问题,但很多控件实际上并不关联的,而 bcb 坚持要你"拥有"当时的控件环境,折腾很久实在是没发 ...

  5. NotifyIcon用法

    -------------------控件NotifyIcon-----------//客户端调用 private void btnShowError_Click(object sender, Eve ...

  6. some websit

    Baidu:VideoView onVideoSizeChanged http://code.taobao.org/p/TangHuZhao/src/ http://code.taobao.org/p ...

  7. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

  8. Qt带来的是更加低廉的开发成本和学习成本,对于很多小公司而言,这种优势足以让他们获得更大的利润空间 good

    不能单纯从技术上来看待这个问题,Qt本来是小众的开发平台,个人认为,它的出现只是解决特性场景的特定问题,Qt带来的是更加低廉的开发成本和学习成本,对于很多小公司而言,这种优势足以让他们获得更大的利润空 ...

  9. Oracle笔记(十三) 视图、同义词、索引

    一.视图 在之前所学习过的所有的SQL语法之中,查询操作是最麻烦的,如果程序开发人员将大量的精力都浪费在查询的编写上,则肯定影响代码的工作进度,所以 一个好的数据库设计人员,除了根据业务的操作设计出数 ...

  10. L2-025 分而治之&lpar;并查集&rpar;

    分而治之,各个击破是兵家常用的策略之一.在战争中,我们希望首先攻下敌方的部分城市,使其剩余的城市变成孤立无援,然后再分头各个击破.为此参谋部提供了若干打击方案.本题就请你编写程序,判断每个方案的可行性 ...