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. Bootstrap 字形图标(Glyphicons)

    http://w3c.3306.biz/bootstrap/eg/bootstrap--glyphicons-list.html

  2. IT编程培训,线上线下,孰优孰劣

    现在Java培训机构确实参差不齐,主要有在线培训和线下培训两大类: 1,虚拟和现实的区别:不论视觉,听觉,体验上在线教学都不如线下教学. 2,学费问 题:在线教学由于成本低,不受地域,教学设备限制一般 ...

  3. ORA-01722: invalid number,ORA-12801

    SQL: SELECT /*+ parallel(a,32) */ a.id ,a.data_date ,a.mobile_num ,a.mobile_code ,b.prov AS mobile_p ...

  4. weblogic启动问题

    昨天测试环境上网银系统突然出现启动weblogic控制台出错问题,执行startWebLogic.sh脚本后tail到nohup文件时没有反应,nohup.out文件一直没有反应.对于此问题同事想re ...

  5. open_basedir restriction in effect. File() is not within the allowed path(s)

    目前发现eaccelerator安装之后如果php.ini中设置open_basedir将导致open_basedir的一些报错(open_basedir restriction in effect. ...

  6. python文件_写入

    1.输入的数据写入到一个文本: #这个写入操作不会将原来的数据覆盖掉 n=raw_input('请输入写入的文件数据:') fl2=open('g:/2.txt','a') fl2.write('\n ...

  7. 临时表妙用、连表更新、sqlserver group contant

    一.临时表妙用 -- 1.将老库中的mediaid和新库中的regionid对应上,然后插入到临时表中 SELECT * INTO #TempMediaRegion FROM (SELECT ww.C ...

  8. #include<iostream>与#include<iostream.h>的区别

                                           转载于祝长洋的BLOG:http://blog.sina.com.cn/s/blog_514b5f600100ayks.h ...

  9. Oracle BigFile

    http://blog.chinaunix.net/uid-20779720-id-3078273.html

  10. win8、win8.1官方版本、及安装密钥

    云盘地址:Windows 8 简体中文专业版+核心版 MSDN 正式版(32位)http://pan.baidu.com/s/1eQgiAiQSHA1:0C4A168E37E38EFB59E88443 ...