CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集
Mr. Kitayuta's Colorful Graph
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and medges. 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.
Example
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4
2
1
0
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
1
1
1
1
2
Note
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.
题意:有多条不同颜色的路径,求给定两点间相同颜色的路径条数。
思路:由于数据范围不大,可以开一个二维数组,使用二维并查集来处理多个相交的连通块。f[i][j],i表示颜色,f[i][j]表示j的祖先。枚举每种color,当两点祖先相同时,即为有这种color路径使他们连通。
#include<stdio.h> int f[][]; int find(int c,int x)
{
return f[c][x]==x?x:f[c][x]=find(c,f[c][x]);
} void join(int c,int x,int y)
{
int fx=find(c,f[c][x]),fy=find(c,f[c][y]);
if(fx!=fy) f[c][fy]=fx;
} int main()
{
int n,m,q,a,b,c,type,cc,i,j;
scanf("%d%d",&n,&m);
type=;
for(i=;i<=;i++){
for(j=;j<=;j++){
f[i][j]=j;
}
}
for(i=;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
if(c>type) type=c;
join(c,a,b);
}
scanf("%d",&q);
for(i=;i<=q;i++){
scanf("%d%d",&a,&b);
cc=;
for(j=;j<=type;j++){
if(find(j,f[j][a])==find(j,f[j][b])) cc++;
}
printf("%d\n",cc);
}
return ;
}
CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集的更多相关文章
- Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)
题目链接 Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$ 涉及的点的个数 $<= ...
- Mr. Kitayuta's Colorful Graph 多维并查集
Mr. Kitayuta's Colorful Graph 并查集不仅可以用于一维,也可以用于高维. 此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的 ...
- 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 ...
- CodeForces 506D Mr. Kitayuta's Colorful Graph
brute force ? 其实是平方分解.很容易想到的是每一个颜色建一个图,然后并查集维护一下连通性. 问题在于颜色有O(m)种,每种颜色的图点数都是O(n)的,因此并查集的空间只能重复利用. 但是 ...
- 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/ ...
- 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 Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...
随机推荐
- lamp环境的搭建和配置
安装apache httpd-2.2.31.tar.gz rpm -qa|grep httpd ##卸载旧的httpd httpd--.el6.centos.x86_64 httpd-tools- ...
- Swift 学习笔记 (属性)
属性可以将值与特定的类 结构体 或者枚举联系起来. 存储属性会存储常量或者变量作为实例的一部分.反之计算属性会计算(而不是存储值)值. 计算属性可以由类 结构体 和枚举定义.存储属性只能由类和结构体定 ...
- Django—工程创建以及models数据库易错点
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- logback 配置详解(下)
logback 常用配置详解(二) <appender> <appender>: <appender>是<configuration>的子节点,是负责写 ...
- javascript控制样式表(不常用)
<html> <head> <title>Example XHTML page</title> <link href="css1.css ...
- git常用开发流程
我们在使用git进行项目管理时,远程仓库的分支情况一般是: master分支作为稳定版分支,用于直接发布产品,dev分支则用于日常开发 备注: 也可以只有一个master分支,这里只介绍第一种情况. ...
- SPOJ - PHRASES Relevant Phrases of Annihilation —— 后缀数组 出现于所有字符串中两次且不重叠的最长公共子串
题目链接:https://vjudge.net/problem/SPOJ-PHRASES PHRASES - Relevant Phrases of Annihilation no tags You ...
- SpringMVC拦截器的配置与使用详解
一.SpringMVC拦截器简介 Spring MVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.在springmvc中,定义拦截 ...
- listen 78
Struggling Young Readers Like Kindles Kindles, Nooks and other e-readers catch flack for threatening ...
- android自定义控件(二) 入门,继承View
转载请注明地址:http://blog.csdn.net/ethan_xue/article/details/7313788 ps: 可根据apidemo里LableView,list4,list6学 ...