ACM: Mr. Kitayuta's Colorful Graph-并查集-解题报
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 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. Sample Input 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 Hint 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.
题目还是比较水,很简单的并查集,思路清晰就够了;
AC代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | #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; } |
标签:
并查集
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法