poi 1182
2024-08-31 03:45:41
食物链 || 带权并查集
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);
}
最新文章
- 使用Zabbix官方模板监控Redis运行状况
- 概率DP
- Samba 4 Domain Controller on Ubuntu 14.04 LTS
- Git学习(三)——暂存区、远程仓库、增删改管理
- python之优雅处理套接字错误
- UVa 699 下落的树叶
- ADO.NET笔记——使用DataSet返回数据
- 股票中的数学:EMA的推导01
- GitHub使用教程for Eclipse
- 『重构--改善既有代码的设计』读书笔记----Remove Assignments to Parameters
- Java三大特征之封装(一)
- JavaWeb三层结构---课设02
- 弱校ACM奋斗史
- JAVAEE——BOS物流项目01:学习计划、搭建环境、主页设计(jQuery EasyUI)
- Flask 系列之 Blueprint
- php 常用正则
- [转]Redis配置文件详解
- update与select关联执行效率问题
- MKAnnotationView和MKPinAnnotationView的区别
- CMake Error: CMake was unable to find a build program corresponding to ";Ninja";.