二维并查集——CodeForces - 505B Mr. Kitayuta's Colorful Graph
2017-08-11 20:43
489 查看
原题链接CodeForces - 505B Mr. Kitayuta’s Colorful Graph
一个无向图包含n个点m条边,顶点编号从1到n。 对于每条边有颜色ci, 连接着顶点 ai 和 b i.
下面有q个询问.
每条询问有两个整数 — ui 和 vi.
找到满足下面条件的颜色个数: 同一种颜色的路径连接顶点 ui 和 顶点 vi
Input
第一行是两个整数 — n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100), 代表着定点个数和边的个数
接下来m行有三个整数 — ai, bi (1 ≤ ai < bi ≤ n) and ci (1 ≤ ci ≤ m).
下一行有一个整数 — q (1 ≤ q ≤ 100), 代表询问次数
接下来q行,每行两个整数 — ui 和 vi (1 ≤ ui, vi ≤ n). 默认 ui ≠ vi.
Output
对于每次询问,在单独的一行输出答案
Example
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
思路:数据量很小 可以用搜索
并查集解决连接问题 每种颜色都需要开一个并查集 所以是二维的
一个无向图包含n个点m条边,顶点编号从1到n。 对于每条边有颜色ci, 连接着顶点 ai 和 b i.
下面有q个询问.
每条询问有两个整数 — ui 和 vi.
找到满足下面条件的颜色个数: 同一种颜色的路径连接顶点 ui 和 顶点 vi
Input
第一行是两个整数 — n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100), 代表着定点个数和边的个数
接下来m行有三个整数 — ai, bi (1 ≤ ai < bi ≤ n) and ci (1 ≤ ci ≤ m).
下一行有一个整数 — q (1 ≤ q ≤ 100), 代表询问次数
接下来q行,每行两个整数 — ui 和 vi (1 ≤ ui, vi ≤ n). 默认 ui ≠ vi.
Output
对于每次询问,在单独的一行输出答案
Example
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
思路:数据量很小 可以用搜索
并查集解决连接问题 每种颜色都需要开一个并查集 所以是二维的
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <stack> #include <vector> #define max_ 110 #define inf 0x3f3f3f3f #define ll long long using namespace std; int pre[max_][max_]; int n,m; int find(int x,int t)//t代表颜色种类 { return pre[t][x]==x?x:find(pre[t][x],t); } void join(int x,int y,int t)//t代表颜色种类 { x=find(x,t); y=find(y,t); if(x!=y) pre[t][x]=y; } int main(int argc, char const *argv[]) { scanf("%d%d",&n,&m); int i,j,r=m; for(i=1;i<max_;i++) //初始化 for(j=1;j<max_;j++) pre[i][j]=j; while(m--) { int x,y,t; scanf("%d%d%d",&x,&y,&t);//t代表颜色种类 join(x,y,t); } int q; scanf("%d",&q); while(q--) { int u,v; scanf("%d%d",&u,&v); int cnt=0; for(i=1;i<=r;i++) { if(find(u,i)==find(v,i)) cnt++; } printf("%d\n",cnt); } return 0; }
相关文章推荐
- CodeForces - 505B Mr. Kitayuta's Colorful Graph(二维并查集)
- 【种类并查集】codeforces 505B Mr. Kitayuta's Colorful Graph
- CodeForces - 505B Mr. Kitayuta's Colorful Graph —— 多维并查集
- B. Mr. Kitayuta's Colorful Graph,二维并查集!
- Codeforces 505B - Mr. Kitayuta's Colorful Graph DFS或并查集
- B. Mr. Kitayuta's Colorful Graph(并查集)
- Codeforces 505B __Mr. Kitayuta's Colorful Graph(floyd)
- Mr. Kitayuta's Colorful Graph(巧用并查集)
- CodeForces-505B-Mr. Kitayuta's Colorful Graph(弗洛伊德)
- Codeforces 505B - Mr. Kitayuta's Colorful Graph (DFS)
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 高维并查集,STL连招
- codeforces 505B Mr. Kitayuta's Colorful Graph
- Mr. Kitayuta's Colorful Graph
- cf#286-B - Mr. Kitayuta's Colorful Graph-dfs
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph
- [Codeforces Round #286 DIV1D (CF506D)] Mr. Kitayuta's Colorful Graph
- Mr. Kitayuta's Colorful Graph
- Codeforces 366D Dima and Trap Graph 【并查集】
- Codeforces--366D--Dima and Trap Graph(并查集)
- Codeforces 505A Mr. Kitayuta's Gift 题解