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 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-并查集-解题报的更多相关文章
- 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/ ...
- Mr. Kitayuta's Colorful Graph 多维并查集
Mr. Kitayuta's Colorful Graph 并查集不仅可以用于一维,也可以用于高维. 此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的 ...
- Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)
题目链接 Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$ 涉及的点的个数 $<= ...
- CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集
Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~
B. Mr. Kitayuta's Colorful Graph -> Link <- 题目链接在上面,题目比较长,就不贴出来了,不过这是道很好的题,很多方法都可以做,真心邀请去A了这 ...
- CodeForces 505B Mr. Kitayuta's Colorful Graph
Mr. Kitayuta's Colorful Graph Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- codeforces 505B Mr. Kitayuta's Colorful Graph(水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...
- CF 286(div 2) B Mr. Kitayuta's Colorful Graph【传递闭包】
解题思路:给出n个点,m条边(即题目中所说的两点之间相连的颜色) 询问任意两点之间由多少种不同的颜色连接 最开始想的时候可以用传递闭包或者并查集来做,可是并查集现在还不会做,就说下用传递闭包来做的这种 ...
随机推荐
- Jquery.Datatables 基本设置的中文注解
$(document).ready(function() { $('#example').dataTable({ "sScrollX": "100%", //表 ...
- EF – 5.DbSet与DbContext,数据更新奥秘
5.6.4 <DbSet与DbContext> 介绍DbSet与DbContext中的核心属性及重要方法. 5.6.5 <数据更新的奥秘> 这一讲极为重要,因为它揭示出了En ...
- jquery.query.js 插件(示例及简单应用)
帮助文档 var url = location.search; > "?action=view§ion=info&id=123&debug&te ...
- Pyqt 获取windows系统中已安装软件列表
开始之前的基础知识 1. 获取软件列表 在Python的标准库中,_winreg可以操作Windows的注册表.获取已经安装的软件列表一般是读去windows的注册表: SOFTWARE\Micros ...
- 【JAVA多线程中使用的方法】
一.sleep和wait的区别. 1.wait可以指定时间,也可以不指定. 而sleep必须制定. 2.在同步的时候,对于CPU的执行权和以及锁的处理不同. wait:释放执行权,释放锁. sleep ...
- JavaScript - 事件流
事件流 事件冒泡就是事件沿DOM树向上传播,在没一级节点上都会发生,直至传播到document对象. 事件捕获正好相反,但是老版本的浏览器不支持,因此很少有人使用事件捕获. 事件处理程序 HTMl 事 ...
- jQuery Mobile Datepicker 使用
插件引入文件: <meta name="viewport" content="width=device-width, initial-scale=1"&g ...
- Android开发如何去除标题栏title(转)
去除标题栏title其实非常简单,他有两种方法,一种是在代码中添加,另一种是在AndroidManifest.xml中添加: 1.在代码中实现:在此方法setContentView(R.layout. ...
- hdu 3183(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 思路:比较前后两个相邻的字符,如果前面一个字符大于后面一个字符,就把它去掉. #include ...
- Java学习笔记(四)——流程控制语句
一.条件语句 1.if条件语句 (1)语法: if(条件) { 条件成立时的代码 } (2)执行过程 2.if else语句 if(条件) { 条件成立时的代码 } else { 不成立的代码 } 3 ...