Mr. Kitayuta's Colorful Graph(巧用并查集)
来源:互联网 发布:淘宝卖家充值平台 编辑:程序博客网 时间:2024/06/07 12:10
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 ton. Each edge, namely edge i, has a color ci, connecting vertexai andbi.
Mr. Kitayuta wants you to process the following q queries.
In the i-th query, he gives you two integers —ui andvi.
Find the number of the colors that satisfy the following condition: the edges of that color connect vertexui and vertexvi 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) andci (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 andvi (1 ≤ ui, vi ≤ n). It is guaranteed thatui ≠ vi.
For each query, print the answer in a separate line.
4 51 2 11 2 22 3 12 3 32 4 331 23 41 4
210
5 71 5 12 5 13 5 14 5 11 2 22 3 23 4 251 55 12 51 51 4
11112
Let's consider the first sample.
- Vertex 1 and vertex 2 are connected by color1 and 2.
- Vertex 3 and vertex 4 are connected by color3.
- Vertex 1 and vertex 4 are not connected by any single color.
题意:
下面有q个询问.
每条询问有两个整数 — ui 和 vi.
找到满足下面条件的颜色个数: 同一种颜色的路径连接顶点 ui 和 顶点 vi
将不同颜色的同一个点看成不同点,开一个二维并查集,既然f[x]储存的是x的父亲节点,那么我们用f[][],第一维表示颜色,第二维便是节点x,我们就用f[c][x]表示与颜色都为c的x的父亲节点,这样,在输入的时候,直接将颜色相同的两个点用并查集联通起来
代码:
#include<stdio.h>#include<algorithm>#define MAX 105using namespace std;int n,m;int pre[MAX][MAX];//pre[c][j]表示与颜色都为c的x的父亲节点void init(){ int i,j; for(i=0;i<=m;i++)//最多m种颜色 for(j=0;j<=n;j++)//顶点数 pre[i][j]=j; //颜色是i的j点父亲是j点本身}int Find(int c,int x){ //寻找颜色为c 的x的父亲 int r=x; while(r!=pre[c][r]) r=pre[c][r]; int i=x,j; while(i!=r){ //路径压缩 j=pre[c][i]; pre[c][i]=r; i=j; } return r;}void mix(int a,int b,int c){ //将颜色为c的a,b两点连起来 int fa=Find(c,a),fb=Find(c,b); if(fa!=fb){ pre[c][fb]=fa; }}int sea(int x,int y,int col){ //查看是否颜色为col 的x,y相连 return Find(col,x)==Find(col,y);}int main(){ scanf("%d%d",&n,&m); init(); int i; int maxx=-1; for(i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); maxx=max(maxx,c); //记录所有颜色的最大值 mix(a,b,c); } int k; scanf("%d",&k); int j; for(i=0;i<k;i++){ int a,b; scanf("%d%d",&a,&b); int cnt=0; for(j=0;j<=maxx;j++) //枚举颜色,判断是否有同一颜色的a,b相连 cnt+=sea(a,b,j); printf("%d\n",cnt); }return 0;}
- Mr. Kitayuta's Colorful Graph(巧用并查集)
- B. Mr. Kitayuta's Colorful Graph(并查集)
- CF 505B(Mr. Kitayuta's Colorful Graph-并查集)
- Codeforces 505B. Mr. Kitayuta's Colorful Graph 并查集
- 505B. Mr. Kitayuta's Colorful Graph(拆分图+并查集)
- B. Mr. Kitayuta's Colorful Graph (CF #286 (Div. 2) 并查集)
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph (并查集 STL)
- Codeforces 506D:Mr. Kitayuta's Colorful Graph 并查集
- 【种类并查集】codeforces 505B Mr. Kitayuta's Colorful Graph
- Mr. Kitayuta's Colorful Graph
- Mr. Kitayuta's Colorful Graph
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph ACM解题报告(并查集)
- Codeforces Round #286 div.1 D 506D D. Mr. Kitayuta's Colorful Graph【并查集】
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph(BFS || DFS || Floyd || 并查集)
- B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 高维并查集,STL连招
- B. Mr. Kitayuta's Colorful Graph
- 506D Mr. Kitayuta's Colorful Graph
- 机器学习入门学习笔记:(2.1)线性回归理论推导
- 有C/C++/Java基础的,学习Python一篇文章就够了
- 《leetCode》:Implement strStr()
- RocketMQ原理解析-broker 1. broker的启动
- Copying Books(最大化最小值问题)
- Mr. Kitayuta's Colorful Graph(巧用并查集)
- Mariadb
- python的上下文管理和with语句详解
- 51Nod-1597-有限背包计数问题
- 使用Keras进行图像分类
- 深入学习js之浅谈作用域(作用域闭包)
- LinkList02
- BZOJ 2301 Problem b (莫比乌斯反演+区间容斥+分块)
- 学习javascript权威指南系列