CF-Mr. Kitayuta's Colorful Graph
1 second
256 megabytes
standard input
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.
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.
For each query, print the answer in a separate line.
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4
2
1
0
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
1
1
1
1
2
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.
思路:
很不错的一道题目。
首先看他最后的那个问题“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的更多相关文章
- CF 286(div 2) B Mr. Kitayuta's Colorful Graph【传递闭包】
解题思路:给出n个点,m条边(即题目中所说的两点之间相连的颜色) 询问任意两点之间由多少种不同的颜色连接 最开始想的时候可以用传递闭包或者并查集来做,可是并查集现在还不会做,就说下用传递闭包来做的这种 ...
- CodeForces 505B Mr. Kitayuta's Colorful Graph
Mr. Kitayuta's Colorful Graph Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...
- 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/ ...
- B. Mr. Kitayuta's Colorful Graph
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second Mr. Kitayuta has just bought an undi ...
- Mr. Kitayuta's Colorful Graph 多维并查集
Mr. Kitayuta's Colorful Graph 并查集不仅可以用于一维,也可以用于高维. 此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的 ...
- codeforces 505B Mr. Kitayuta's Colorful Graph(水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
- Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)
题目链接 Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$ 涉及的点的个数 $<= ...
- CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集
Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...
随机推荐
- Android-语言设置流程分析
Android手机语言切换行为,是通过设置-语言和输入法-语言来改变手机的语言,其实这个功能很少被用户使用. 以Android5.1工程源码为基础,从设置app入手来分析和学习语言切换的过程: ...
- TreeSet与TreeMap
TreeSet底层使用的存储容器为TreeMap TreeMap使用红黑树(一种自平衡的排序二叉树)实现,检索效率为O(logn) 红黑树的三个基本操作:左旋.右旋.着色 平衡二叉树:空树或左右子树高 ...
- Android Configuration change引发的问题及解决方法
之前在学习Fragment和总结Android异步操作的时候会在很多blog中看到对Configuration Change的讨论,以前做的项目都是固定竖屏的,所以对横竖屏切换以及横竖屏切换对程序有什 ...
- Java基础知识强化74:正则表达式之分割功能 (扩展练习)
1. 看程序写结果:(面试题考过) package cn.itcast_03; /* * 分割功能练习 */ public class RegexDemo2 { public static void ...
- 综合使用LruCache和DiskLruCache 缓存图片
Activity public class MainActivity extends Activity { private GridView mPhotoWall; private P ...
- webform 复杂点的服务器控件
1 , dropdownlist: 下拉框 属性items 列表集合, 里面的每一个元素是一个 listitem . 联动的时候注意要 设置属性 .Autopostback 为ture: 注注 ...
- C++关联容器<map>简单总结
C++关联容器<map>简单总结 map提供大小可变的关联容器,基于关联键值高效检索元素值.当你处理键值对的数据是,都可以考虑使用map关联容器. 特点: 大小可变的关联容器,基于关联键值 ...
- Visual Studio 2013如何破解(密钥激活)
其实有个方法最简单,就是点击“帮助”,选择注册产品,点击打开页面右下边的“使用秘钥注册产品”,输入上述秘钥即可. 在输入密钥界面,输入密钥“BWG7X-J98B3-W34RT-33B3R-JVYW ...
- canvas 绘制矩形
XXX(x,y,width,height) x矩形左上角x坐标 y矩形左上角y坐标 ...
- 一个php小白找工作的历程
一个php小白找工作的历程其实对新工作还是有点忐忑的,对于我这样一个有着特殊工作经历的来说更是如此.为了更好的迎接未来,不得不总结下过去.在经历一段时间的职业生涯探索期后,还是觉得自己更适合做程序员这 ...