Mr. Kitayuta's Colorful Graph

并查集不仅可以用于一维,也可以用于高维。

此题的大意是10W个点10W条边(有多种颜色),10W个询问:任意两个节点之间可以由几条相同颜色的路径连通。

这里要用到高维的并查集,定义fa[u][c]=v表示节点u的颜色c属于集合v,由于无法开出这么大的二维数组,且实际边的数量很少,可以考虑使用map。

每次加边的时候,如果该节点u的颜色c不属于任何集合,则将u作为当前集合的根。每次加入一条边,相当于合并两个不同的集合。

询问的时候可以暴力查找,同时记录哪些被查找过,下次不再重复查找。枚举u的每一种颜色集合,验证v的这个颜色是否与u属于同一集合,即u,v能否由同一颜色的路径连通。

这题有个坑点,就是普通的map会超时,要使用unordered_map。

#include<bits/stdc++.h>
#define rep(i,n) for(i=1;i<=n;i++)
using namespace std;
const int maxn=;
unordered_map<int,int>fa[maxn],ou[maxn];
int n,m;
int getfa(int v,int c)
{
if(fa[v][c]==v)return v;
return fa[v][c]=getfa(fa[v][c],c);
}
void work(int x,int y,int c)
{
if(fa[x].find(c)==fa[x].end())fa[x][c]=x; //root
if(fa[y].find(c)==fa[y].end())fa[y][c]=y;
int f1=getfa(x,c);
int f2=getfa(y,c);
if(f1!=f2)
fa[f1][c]=f2;
}
int main()
{
scanf("%d%d",&n,&m);
int i;
rep(i,m)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
work(x,y,c);
}
int q;
scanf("%d",&q);
rep(i,q)
{
int x,y;
int ans=;
scanf("%d%d",&x,&y);
if(ou[x].find(y)!=ou[x].end())
{
printf("%d\n",ou[x][y]);
continue;
}
int s1=fa[x].size(); //total color
int s2=fa[y].size();
if(s1>s2)swap(x,y);
for(auto u: fa[x])
if(fa[y].find(u.first)!=fa[y].end())
if(getfa(x,u.first)==getfa(y,u.first)) //be in the same set
++ans;
ou[x][y]=ou[y][x]=ans;
printf("%d\n",ans);
}
return ;
}

Mr. Kitayuta's Colorful Graph 多维并查集的更多相关文章

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

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

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

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

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

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

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

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

  5. 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 ...

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

  7. B. Mr. Kitayuta's Colorful Graph

     B. Mr. Kitayuta's Colorful Graph  time limit per test 1 second Mr. Kitayuta has just bought an undi ...

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

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

  9. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph

    D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...

随机推荐

  1. java下的第一个redis

    Redis支持很多编程语言的客户端,有C.C#.C++.Clojure.Common Lisp.Erlang.Go.Lua.Objective-C.PHP.Ruby.Scala,甚至更时髦的Node. ...

  2. Hadoop高可用平台搭建

    文章概览: 1.机器规划和预配置 2.软件安装 3.集群文件配置 4.启动集群 5.HA验证 6.注意事项 7.小结   机器规划和预配置 主机/进程 NN DN RM NM ZK(QP) ZKFC ...

  3. mysql分库分表总结<转>

    单库单表 单库单表是最常见的数据库设计,例如,有一张用户(user)表放在数据库db中,所有的用户都可以在db库中的user表中查到. 单库多表 随着用户数量的增加,user表的数据量会越来越大,当数 ...

  4. Java Load Properties 文件,定义message信息

    初始化Properties对象,load properties文件: private static final Properties MESSAGERESOURCES = new Properties ...

  5. [linux]linux下开启wifi热点

    网上说用 ap-hotspot可以,但是我测试的时候总是在start的时候卡主,试了以前的版本也是,最后找到了下面的方法. 第一步:安装 plasma-nm 可以使用下面的命令 sudo apt-ge ...

  6. Hough Transform直线检测

    本文原创,如转载请注明出处. Hough Transform 是一种能提取图像中某种特定形状特征的方法,可以将其描述成一种把图像空间中的像素转换成Hough空间中直线或曲线的一种映射函数.通过利用Ho ...

  7. android添加edittext后布局就不好用

    在布局添加控件手动添加还是拖的添加,添加edittext后布局就不好用,其他控件好用,然后就说下面这段话 Exception raised during rendering: java.lang.Sy ...

  8. hdu 3047 Zjnu Stadium

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 带权并差集 #include <cstdio> #include <cstring> ...

  9. java.lang.OutOfMemoryError: GC overhead limit exceeded 问题分析和解决(转)

    在项目历史数据导入过程中,出现了应用无法访问的情况.立刻对Weblogic进行分析,发现Weblogic的内存.线程等性能良好,Server也是Running的状态.随后查看了Weblogic日志,在 ...

  10. C# 文件夹操作

    追加文件    StreamWriter sw = File.AppendText(Server.MapPath(] != Path.DirectorySeparatorChar)         a ...