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条边(即题目中所说的两点之间相连的颜色) 询问任意两点之间由多少种不同的颜色连接 最开始想的时候可以用传递闭包或者并查集来做,可是并查集现在还不会做,就说下用传递闭包来做的这种 ...
随机推荐
- .net学习笔记---webconfig的读与写
System.ConfigurationManager类用于对配置文件的读取.其具有的成员如下: 一.AppSettings AppSetting是最简单的配置节,读写非常简单. 名称 说明 AppS ...
- 【转载】 Python动态生成变量
用Python循环创建多个变量, 如创建 a1= .a2= .a3= .a4= .a5= 或 self.a1= .self.a2= . self.a3= 一. 可以通 ...
- hdu 4055 递推
转自:http://blog.csdn.net/shiqi_614/article/details/7983298 题意:由数字1到n组成的所有排列中,问满足题目所给的n-1个字符的排列有多少个,如果 ...
- C# 拓展方法
/// <summary> /// 扩展类 /// </summary> public static class Extend { /// <summary> // ...
- POJ 3140 Contestants Division 树形DP
Contestants Division Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...
- MapKit地图划线
只要用于获取用户位置都要取得用户授权 #import "ViewController.h" #import <MapKit/MapKit.h> @interface V ...
- Android studio导入eclipse项目且不改变目录结构
Android studio的安装与配置论坛当中已经有很多在此就不在细说了,现在开始说下如何在Android studio当中导入eclipse的项目且不改变其目录结构和配置,让使用eclipse的同 ...
- WebView中实现文件下载功能
WebView控制调用相应的WEB页面进行展示.当碰到页面有下载链接的时候,点击上去是一点反应都没有的.原来是因为WebView默认没有开启文件下载的功能,如果要实现文件下载的功能,需要设置Web ...
- C++的模板特化 和 STL中iterator_traits模板的偏特化
C++中有类模板和函数模板,它们的定义如下所示: 类模板: template<class T1,class T2> class C { //... }; 函数模板: template< ...
- supervisor(一)基础篇
这两天干的活,是让楼主写一个supervisor的listener,用来监控supervisor所管理子进程的状态,当子进程异常退出时,楼主写的这个listener将会触发报警.在这里总结下super ...