POJ-1182(经典带权并查集)
2024-09-05 20:07:17
食物链
POJ-1182
一个很好的分析博客:https://blog.csdn.net/niushuai666/article/details/6981689
三种关系:两者同类,吃父节点,被父节点吃,所以权值可以用0,1,2表示
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
using namespace std;
const int maxn=50004;
int n,m;
int set[maxn];
int sum[maxn];
int ans;
int find(int x){
if(x==set[x])
return set[x];
else{
int parent=set[x];
set[x]=find(set[x]);
sum[x]=(sum[x]+sum[parent])%3;
return set[x];
}
}
void merge(int type,int a,int b){
int ta=find(a);
int tb=find(b);
if(ta==tb){
if((sum[a]-sum[b]+3)%3!=type-1)
ans++;
}else{
set[ta]=tb;
sum[ta]=(sum[b]-sum[a]+type-1+3)%3;
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++){
sum[i]=0;
set[i]=i;
}
int d,a,b;
for(int i=0;i<m;i++){
scanf("%d%d%d",&d,&a,&b);
if(a>n||b>n){
ans++;
continue;
}else if(a==b&&d==2){
ans++;
continue;
}else merge(d,a,b);
}
cout<<ans<<endl;
return 0;
}
最新文章
- 一分钟搞定AlloyTouch图片轮播组件
- 为什么类和接口不能使用private和protected?接口的方法不能使用private、protected、default
- CrashMonkey4IOS App测试
- Java 的设计模式之一装饰者模式
- 学习MySQL之单表操作(二)
- Mac无法找到摄像头问题解决
- 转】MyEclipse10安装Log4E插件
- linux 虚机增加硬盘大小 转自
- Gentoo Linux 学习笔记2
- (转)[置顶] Android APK反编译就这么简单 详解(附图) .
- 【mysql】Date和String的互相转换(DATE_FORMAT &; STR_TO_DATE)
- 【转】Android总结篇系列:Activity Intent Flags及Task相关属性
- 学习flying logic
- 06 Zabbix4.0系统CISCO交换机告警模板规划信息(基础)
- Linux下常用工具
- 拓展 NLog 优雅的输送日志到 Logstash
- 关于SQL语句中的distinct和group by
- MATLAB在三维坐标中显示图片 并 使得图片部分透明
- layer相关使用
- 【H5】ie8如何兼容html5标签(hack)