ACM: Mr. Kitayuta's Colorful Graph-并查集-解题报

时间:2021-11-28 20:41:17
Mr. Kitayuta's Colorful Graph
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 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 ( ≤ n ≤ ,  ≤ m ≤ ), denoting the number of the vertices and the number of the edges, respectively. The next m lines contain space-separated three integers — ai, bi ( ≤ ai < bi ≤ n) and ci ( ≤ 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 ( ≤ q ≤ ), denoting the number of the queries. Then follows q lines, containing space-separated two integers — ui and vi ( ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi. Output
For each query, print the answer in a separate line. Sample Input
Input Output Input Output Hint
Let's consider the first sample.

ACM: Mr. Kitayuta's Colorful Graph-并查集-解题报

 The figure above shows the first sample.
Vertex and vertex are connected by color and .
Vertex and vertex are connected by color .
Vertex and vertex are not connected by any single color. 题目还是比较水,很简单的并查集,思路清晰就够了;
AC代码:
#include"iostream"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"algorithm"
using namespace std;
const int MX=110;
int CL[101];
struct nde {
int p[MX]; //记录每个颜色下的根
} cmap[MX]; struct node {
int a,b,c; //连接的点 a b 和颜色c
} side[MX]; bool cmps(node a,node b) {
return a.c<b.c;
} struct nod {
int u,v; //查询组
} Que[MX]; int find(int x,int c) {
return cmap[c].p[x]==x?x:(cmap[c].p[x]=find(cmap[c].p[x],c)); //找到在颜色C下的根。
} void init() { //清空并初始化整个颜色图。
for(int i=1; i<=100; i++)
for(int j=1; j<=100; j++) {
cmap[i].p[j]=j;
}
} int main() {
int n,m,q;
while(~scanf("%d%d",&n,&m)) {
init();
for(int i=1; i<=m; i++) { //把整个输入先全部记录下
scanf("%d%d%d",&side[i].a,&side[i].b,&side[i].c);
}
sort(side+1,side+1+m,cmps);//按照颜色排序;
int tot=1; //tot 记录颜色的种类
CL[tot]=side[1].c;//记录下出现过的颜色
for(int i=1; i<=m; i++) {
if(side[i].c!=CL[tot]) { //如果下一个颜色与上一个颜色不同记录进数组 CL
CL[++tot]=side[i].c;
}
int rt1=find(side[i].a,side[i].c); //找到在颜色 C 下的根
int rt2=find(side[i].b,side[i].c);
if(rt1!=rt2) {
cmap[side[i].c].p[rt2]=rt1; //在颜色 C下将两点连接,更新根
}
}
scanf("%d",&q);
for(int i=1; i<=q; i++) {
scanf("%d%d",&Que[i].u,&Que[i].v); //单组输入查询
int ans=0;
for(int j=1; j<=tot; j++) { //每个出现过的颜色下都查询一次
int rt1=find(Que[i].u,CL[j]);
int rt2=find(Que[i].v,CL[j]);
if(rt1==rt2)ans++; //如果是一个联通块答案加一
}
printf("%d\n",ans); //输出该组的答案
}
}
return 0;
}