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. Faster-rnnlm代码分析2 - HSTree的构造

    也就是构造一棵Huffman Tree,输入是按照词汇频次由高到低排序的 采用层次SoftMax的做法,是为了使得训练和预测时候的softmax输出加速,原有multinomal softmax,是和 ...

  2. mac下php开发环境搭建+CI框架使用

    一.启动apache: apachectl start 停止: apachectl stop 配置文件: vi /etc/apache2/httpd.conf 一.修改端口 因为80端口不想被占用,8 ...

  3. UDP穿透NAT原理解析

    转自:http://www.2cto.com/net/201201/116793.html NAT(Network Address Translators),网络地址转换:网络地址转换是在IP地址日益 ...

  4. linux脚本编程技术

    linux脚本编程技术 一.什么是脚本 脚本是一个包含一系列命令序列的可执行(777)文本文件.当运行这个脚本文件时,文件中包含的命令序列将得到自动执行. 二.脚本编程 #!/bin/sh 首行固定格 ...

  5. 国内大学毕业论文 LaTeX 模板集合

    西北工业大学学位论文LaTeX模板 http://code.google.com/p/nwputhesis/ 西北工业大学硕博士论文LaTeX模版 http://code.google.com/p/n ...

  6. 19条ANDROID平台设计规范(转)

    1.尺寸以及分辨率: Android的界面尺寸比较流行的有:480*800.720*1280.1080*1920,我们在做设计图的 时候建议是以 480*800的尺寸为标准; 2.界面基本组成元素: ...

  7. NuGet学习笔记(1) 初识NuGet及快速安装使用

    关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...

  8. 真机测试INSTALL_FAILED_INSUFFICIENT_STORAGE 的解决方法

    来源:http://blog.csdn.net/aikongmeng/article/details/9793809 INSTALL_FAILED_INSUFFICIENT_STORAGE 的解决方法 ...

  9. ios录音

    #import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...

  10. 智能车学习(八)——菜单的实现

    一.代码分享 1.头文件 #ifndef __MENU_H #define __MENU_H /***********宏定义************/ //页面声明 typedef enum Menu ...