博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
P2024 食物链
阅读量:7216 次
发布时间:2019-06-29

本文共 1231 字,大约阅读时间需要 4 分钟。

并查集的题

关于带有多个相对集合的全集,我们可以多开几倍的空间。每一倍的元素表示这个当前里的相对元素


#include
#include
#include
using namespace std;int f[600000];int find(int x){ if(f[x]==x) return x; return f[x]=find(f[x]);}int _union(int x,int y){ int f1=find(x),f2=find(y); f[f1]=f2;}int main(){ int n,k; scanf("%d%d",&n,&k); int a,b,c; for(int i=1;i<=3*n;i++) f[i]=i; int t=0; for(int i=1;i<=k;i++) { scanf("%d%d%d",&a,&b,&c); if(b>n||c>n) { t+=1; continue; } if(a==1) { if(find(b+n)==find(c)||find(b+2*n)==find(c)) { t+=1; continue; } _union(b,c); _union(b+n,c+n); _union(b+2*n,c+2*n); } if(a==2) { if(b==c) { t+=1; continue; } if(find(b+n)==find(c)||find(b)==find(c)) { t+=1; continue; } _union(b+2*n,c); _union(b,c+n); _union(b+n,c+2*n); } } printf("%d",t);}

肯定有一些自恃NB的不会看的

转载于:https://www.cnblogs.com/Lance1ot/p/8643628.html

你可能感兴趣的文章
不要if else的编程
查看>>
rn.ShowDialog() == DialogResult.OK
查看>>
20160519
查看>>
SCU 3132(博弈)
查看>>
正则表达式
查看>>
delete archivelog all 无法彻底删除归档日志?
查看>>
Redis五大数据类型
查看>>
大型分布式网站架构技术总结
查看>>
矩阵求导与投影梯度相关问题
查看>>
SVN
查看>>
C语言编程写的一个http下载程序(王德仙)2012-04-08
查看>>
CCF201409-3 字符串匹配(100分)
查看>>
UVALive2203 UVa10042 Smith Numbers【质因数分解+素数判定+数位之和】
查看>>
Project Euler Problem 9: Special Pythagorean triplet
查看>>
HDU5701 中位数计数【中位数】
查看>>
Python 深浅拷贝 (Shallow copy and Deep copy in Python)
查看>>
Axure
查看>>
屏幕截取工具
查看>>
C语言第七次作业---要死了----
查看>>
Jquery事件绑定冲突
查看>>