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.

 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;
}

  

ACM: Mr. Kitayuta's Colorful Graph-并查集-解题报的更多相关文章

  1. 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/ ...

  2. Mr. Kitayuta's Colorful Graph 多维并查集

    Mr. Kitayuta's Colorful Graph 并查集不仅可以用于一维,也可以用于高维. 此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的 ...

  3. Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)

    题目链接  Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$    涉及的点的个数 $<= ...

  4. CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集

    Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...

  5. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

  6. B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~

    B. Mr. Kitayuta's Colorful Graph ->  Link  <- 题目链接在上面,题目比较长,就不贴出来了,不过这是道很好的题,很多方法都可以做,真心邀请去A了这 ...

  7. CodeForces 505B Mr. Kitayuta's Colorful Graph

    Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  8. codeforces 505B Mr. Kitayuta's Colorful Graph(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...

  9. CF 286(div 2) B Mr. Kitayuta's Colorful Graph【传递闭包】

    解题思路:给出n个点,m条边(即题目中所说的两点之间相连的颜色) 询问任意两点之间由多少种不同的颜色连接 最开始想的时候可以用传递闭包或者并查集来做,可是并查集现在还不会做,就说下用传递闭包来做的这种 ...

随机推荐

  1. Clr Via C#读书笔记----基元线程同步构造

    线程文章:http://www.cnblogs.com/edisonchou/p/4848131.html 重点在于多个线程同时访问,保持线程的同步. 线程同步的问题: 1,线程同步比较繁琐,而且容易 ...

  2. Delphi中函数定义和声明的位置

    当函数(或过程)A定义在函数(或过程)B之前,那么函数B就可以调用函数A,并且编译成功,例如下面的 procedure TForm1.btn1Click(Sender: TObject); 和   f ...

  3. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  4. K-MEANS算法总结

    K-MEANS算法 摘要:在数据挖掘中,K-Means算法是一种 cluster analysis 的算法,其主要是来计算数据聚集的算法,主要通过不断地取离种子点最近均值的算法. 在数据挖掘中,K-M ...

  5. AngularJS 开发中常犯的10个错误

    简介 AngularJS是目前最为活跃的Javascript框架之一,AngularJS的目标之一是简化开发过程,这使得AngularJS非常善于构建小型app原型,但AngularJS对于全功能的客 ...

  6. 6-02使用SQL语句向表中插入数据

    插入语句的语法: INSERT INTO 表() VALUES(值列表) 注意事项: 1:每次插入一行数据,不能只插入半行或几列数据. 2:每一个数据值的数据类型.精度和小数位数必须与相应的列匹配. ...

  7. jquery获取radio和select选中值

    //jquery 获取radio选中值 <input type="radio" name="c_type" value="a" > ...

  8. c#写windows服务(转)

    序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而IIs又不稳定因素,如果重启IIs就要打开页面才能运行项目.有不便之处,就改用Windows服务实现.这篇就总 ...

  9. 【JavaScript基础入门】总结目录

    一.JavaScript基础 1.1JavaScript概述 1.2如何使用的JavaScript 1.3JavaScript基本语法 1.4JavaScript数据类型 1.5JavaScript运 ...

  10. 领域模型(domain model)&贫血模型(anaemic domain model)&充血模型(rich domain model)

    领域模型是领域内的概念类或现实世界中对象的可视化表示,又称为概念模型或分析对象模型,它专注于分析问题领域本身,发掘重要的业务领域概念,并建立业务领域概念之间的关系. 贫血模型是指使用的领域对象中只有s ...