CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集
Mr. Kitayuta's Colorful Graph
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and medges. 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.
Example
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
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.
题意:有多条不同颜色的路径,求给定两点间相同颜色的路径条数。
思路:由于数据范围不大,可以开一个二维数组,使用二维并查集来处理多个相交的连通块。f[i][j],i表示颜色,f[i][j]表示j的祖先。枚举每种color,当两点祖先相同时,即为有这种color路径使他们连通。
#include<stdio.h> int f[][]; int find(int c,int x)
{
return f[c][x]==x?x:f[c][x]=find(c,f[c][x]);
} void join(int c,int x,int y)
{
int fx=find(c,f[c][x]),fy=find(c,f[c][y]);
if(fx!=fy) f[c][fy]=fx;
} int main()
{
int n,m,q,a,b,c,type,cc,i,j;
scanf("%d%d",&n,&m);
type=;
for(i=;i<=;i++){
for(j=;j<=;j++){
f[i][j]=j;
}
}
for(i=;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
if(c>type) type=c;
join(c,a,b);
}
scanf("%d",&q);
for(i=;i<=q;i++){
scanf("%d%d",&a,&b);
cc=;
for(j=;j<=type;j++){
if(find(j,f[j][a])==find(j,f[j][b])) cc++;
}
printf("%d\n",cc);
}
return ;
}
CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集的更多相关文章
- Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)
题目链接 Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$ 涉及的点的个数 $<= ...
- Mr. Kitayuta's Colorful Graph 多维并查集
Mr. Kitayuta's Colorful Graph 并查集不仅可以用于一维,也可以用于高维. 此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的 ...
- CodeForces 505B Mr. Kitayuta's Colorful Graph
Mr. Kitayuta's Colorful Graph Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- codeforces 505B Mr. Kitayuta's Colorful Graph(水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...
- CodeForces 506D Mr. Kitayuta's Colorful Graph
brute force ? 其实是平方分解.很容易想到的是每一个颜色建一个图,然后并查集维护一下连通性. 问题在于颜色有O(m)种,每种颜色的图点数都是O(n)的,因此并查集的空间只能重复利用. 但是 ...
- 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/ ...
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~
B. Mr. Kitayuta's Colorful Graph -> Link <- 题目链接在上面,题目比较长,就不贴出来了,不过这是道很好的题,很多方法都可以做,真心邀请去A了这 ...
- 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 ...
随机推荐
- 使用OpenSessionInViewFilter的注意事项
假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter或者OpenSessionInViewInterc ...
- virtual dynamic shared object
vdso(7) - Linux manual page http://man7.org/linux/man-pages/man7/vdso.7.html NAME | SYNOPSIS | DESCR ...
- lvs虚拟服务器
NAT模式 1.模拟环境: LVS服务器有两块网卡,连接外网(用户端)和内网(服务器),充当"交警"角色. 优点: 节省ip开销 缺点: LVS服务器负载过高,数据吞吐量降低 三台 ...
- JavaEE详解
本文主要讲JavaEE相关知识. 一 JavaEE 简介 JavaEE是很多技术的合集.提供了一套做B/S结构应用时,可能遇到问题的一套解决方案. 例如:处理客服端请求的servlet技术方案.处理数 ...
- python基础教程_学习笔记11:魔法方法、属性和迭代器
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/31417309 魔法方法.属性和迭代器 在pyth ...
- Redis3.x HA 方案(基于 Sentinel 方式)
第一部分 Redis-HA 搭建 一.Redis-HA 拓扑 一主两从,主从复制,故障时主从切换 三个Redis节点 + Sentinel 节点 Master 127.0.0.1 ...
- CUDA: 原子操作
1.1以上计算功能集支持全局内存上的原子操作, 1.2以上支持共享内存上的原子操作. atomicAdd(add,y)将生成一个原子的操作序列,这个操作序列包括读取地址addr处的值,将y增加到这个值 ...
- win 10 安装.msi 程序出现the error code is 2503
解决方法: C:\Windows\temp文件夹的权限不够,需要给其更高权限 右键temp文件夹 点击属性进入属性对话框 组或用户名的里面的All APPLICATION PACKAGES和所有受限制 ...
- matlab之结构体数组struct
以下内容来自于:https://blog.csdn.net/u010999396/article/details/54413615/ 要在MALTAB中实现比较复杂的编程,就不能不用struct类型. ...
- tensorflow实现svm多分类 iris 3分类——本质上在使用梯度下降法求解线性回归(loss是定制的而已)
# Multi-class (Nonlinear) SVM Example # # This function wll illustrate how to # implement the gaussi ...