食物链 || 带权并查集

0:同类

1:吃

2:被吃

#include <cstdio>
using namespace std;
const int maxn=5e4+3; int f[maxn], w[maxn]; void init(int n)
{
for(int i = 1; i <= n; ++i) f[i] = i, w[i] = 0;
} int find(int x)
{
if(x == f[x]) return x;
int tmp = f[x];
f[x] = find(f[x]);
w[x] = (w[x] + w[tmp] + 3) % 3;//这里其实不用+3,因为w[i]为0-2的数
return f[x];
} int main()
{
int n, k, d, x, y, cnt = 0;
scanf("%d %d", &n, &k);
init(n);
for(int i = 0; i < k; ++i)
{
scanf("%d %d %d", &d, &x, &y);
if(x > n || y > n || (d == 2 && x == y))
{
++cnt; continue;
}
int xx = find(x), yy = find(y);
if(xx == yy)
{
if((w[x] - w[y] + 3) % 3 != d - 1) ++cnt;
}
else
{
f[xx] = yy;
w[xx] = (w[y] - w[x] + d - 1 + 3) % 3;
}
}
printf("%d\n", cnt);
}

最新文章

  1. 使用Zabbix官方模板监控Redis运行状况
  2. 概率DP
  3. Samba 4 Domain Controller on Ubuntu 14.04 LTS
  4. Git学习(三)——暂存区、远程仓库、增删改管理
  5. python之优雅处理套接字错误
  6. UVa 699 下落的树叶
  7. ADO.NET笔记——使用DataSet返回数据
  8. 股票中的数学:EMA的推导01
  9. GitHub使用教程for Eclipse
  10. 『重构--改善既有代码的设计』读书笔记----Remove Assignments to Parameters
  11. Java三大特征之封装(一)
  12. JavaWeb三层结构---课设02
  13. 弱校ACM奋斗史
  14. JAVAEE——BOS物流项目01:学习计划、搭建环境、主页设计(jQuery EasyUI)
  15. Flask 系列之 Blueprint
  16. php 常用正则
  17. [转]Redis配置文件详解
  18. update与select关联执行效率问题
  19. MKAnnotationView和MKPinAnnotationView的区别
  20. CMake Error: CMake was unable to find a build program corresponding to &quot;Ninja&quot;.

热门文章

  1. [Luogu P2831] 愤怒的小鸟 (状压DP)
  2. 你说一下对Java中的volatile的理解吧
  3. .netcore 简单使用ElasticSearch
  4. python开发基础(二)运算符以及数据类型之tuple(元组)
  5. spark内存管理这一篇就够了
  6. Jmeter-全局变量跨线程组使用
  7. 02模板渲染和参数(补充:URL传参到视图)
  8. kafka生产者数据可靠性保证
  9. 缩点Tarjan算法解析+[题解]受欢迎的牛
  10. WireShark——ARP 协议包分析