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 多维并查集的更多相关文章
- CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集
Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...
- Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)
题目链接 Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$ 涉及的点的个数 $<= ...
- 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 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 ...
- 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/ ...
- 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 ...
- codeforces 505B Mr. Kitayuta's Colorful Graph(水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
随机推荐
- 浅谈js闭包
相信很多人只知道闭包这个词但是具体是怎么回事就不太清楚了,最近在群里有很多小伙伴讨论这个问题但还是蒙眬眬的赶脚.索性就写了这篇文章来帮助大家一起理解闭包. 变量作用域 闭包其实想明白了很简单,但是在理 ...
- 费马小定理&欧拉定理
在p是素数的情况下,对任意整数x都有xp≡x(mod p).这个定理被称作费马小定理其中如果x无法被p整除,我们有xp-1≡1(mod p).利用这条性质,在p是素数的情况下,就很容易求出一个数的逆元 ...
- js页面加载进度条(这个就比较正式了,改改时间就完事儿)
不废话,直接上代码 思路不难,就是一个animate方法配合随机数 duration内个三秒钟,是自定义的,可以改成页面加载时间,这样就完美了 <!doctype html> <ht ...
- photoSlider-html5原生js移动开发轮播图-相册滑动插件
简单的移动端图片滑动切换浏览插件 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css" hre ...
- Python核心编程(第九章)--文件和输入输出
文件内建函数: open()函数提供了初始化输入/输出操作的通用接口 open()基本语法:file_object = open(filename,access_mode='r',buffering= ...
- mysql在关闭时的几个阶段
mysql关闭的大致过程 1.The shutdown process is initiated 初始化关机过程有许多种方法1.mysqladmin shutdown ; 2.kill pid_of_ ...
- UIViewController的生命周期及iOS程序执行顺序
UIViewController的生命周期及iOS程序执行顺序 当一个视图控制器被创建,并在屏幕上显示的时候. 代码的执行顺序1. alloc ...
- HTTP 协议简介
HTTP 协议简介 博客分类: acl开发--HTTP协议篇 网络协议http协议 一.TCP/IP 协议介绍 在介绍 HTTP 协议之前,先简单说一下TCP/IP协议的相关内容.TCP/IP协议是 ...
- 【给你几个使用Xamarin的理由】
写在开篇前 这种代理操作,绑定影射的机制,有些极端的开发者确实难以接受.追求完美,总感觉原生的各种优点. 如果你非得较这个真,那您还是感觉补习下 Java Eclipse ,买一台Mac 恶补Obj ...
- Android导航菜单横向左右滑动并和下方的控件实现联动
这个是美团网个人订单的效果,找了很多地方都没找到,自己研究了两天终于弄出来了^_^,有什么问题希望大家指出来,谢谢. 实现原理是上方使用HorizontalScrollView这个可以水平横向拖动的控 ...