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 ...
随机推荐
- PHP Strict standards:Declaration of … should be compatible with that of…(转)
今天把原来一份很老的PHP代码导入到了PaaS上,出现了许多Strict standards:Declaration of … should be compatible with that of…这样 ...
- SQL 结构化查询语言手册
摘自该学习网站: http://www.w3school.com.cn/sql/ 新学到的几点: and 和or 连用,记得用括号. 2.SQL通配符补充 例如: ...
- 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题
[题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...
- knockout --- foreach -- 前端必备
很久很久没写博客了,丫的,节操掉一地了,颓废了,惭愧. 很久很久没有弄 knouckout.js 了,今天重新操作,蛋疼啊,忘记得差不多了,于是只好硬着头皮再去看官网,于是,feel慢慢回来了. 本来 ...
- vmware桥接模式创建ubuntu虚拟机
- jquery的几种异步请求,ajax
http://blog.csdn.net/a5489888/article/details/8523316
- 升级openssl到1.0.1g
先进行支撑包的安装: # yum install -y zlib openssl升级步骤: 下载最新版本的openssl源码包 # wget ftp://ftp.openssl.org/sourc ...
- Jquery插件 easyUI属性汇总
属性分为CSS片段和JS片段. CSS类定义:1.div easyui-window 生成一个window窗口样式. 属性如下: 1)mod ...
- WAJUEJI which home strong!
算法:搜索+优先队列 描述 在一个山沟里,姐弟俩同时考上了大学.但由于家里拮据,所以这并不是什么好消息.父亲对孩子说:我就是砸锅卖铁也要把你们姐俩供出来. 当时的姐姐已经决定放弃上学的机会. 没想到第 ...
- Y - Design T-Shirt(第二季水)
Description Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA ...