食物链

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;
}

最新文章

  1. 一分钟搞定AlloyTouch图片轮播组件
  2. 为什么类和接口不能使用private和protected?接口的方法不能使用private、protected、default
  3. CrashMonkey4IOS App测试
  4. Java 的设计模式之一装饰者模式
  5. 学习MySQL之单表操作(二)
  6. Mac无法找到摄像头问题解决
  7. 转】MyEclipse10安装Log4E插件
  8. linux 虚机增加硬盘大小 转自
  9. Gentoo Linux 学习笔记2
  10. (转)[置顶] Android APK反编译就这么简单 详解(附图) .
  11. 【mysql】Date和String的互相转换(DATE_FORMAT &amp; STR_TO_DATE)
  12. 【转】Android总结篇系列:Activity Intent Flags及Task相关属性
  13. 学习flying logic
  14. 06 Zabbix4.0系统CISCO交换机告警模板规划信息(基础)
  15. Linux下常用工具
  16. 拓展 NLog 优雅的输送日志到 Logstash
  17. 关于SQL语句中的distinct和group by
  18. MATLAB在三维坐标中显示图片 并 使得图片部分透明
  19. layer相关使用
  20. 【H5】ie8如何兼容html5标签(hack)

热门文章

  1. Codeforces Round #646 (Div. 2) 题解 (ABCDE)
  2. SpringBoot简单整合redis
  3. CF1459-C. Row GCD
  4. Nginx 四层负载均衡
  5. 牛客网-Beauty of Trees 【加权并查集】
  6. HDU 6390 GuGuFishtion(莫比乌斯反演 + 欧拉函数性质 + 积性函数)题解
  7. 记一次getshell
  8. Redis-第八章节-应用场景
  9. uni-app in action
  10. docker-compose All In One