食物链

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

POJ-1182(经典带权并查集)的更多相关文章

  1. poj 1182 食物链 带权并查集

    食物链是并查集的进阶运用的一道非常经典的题目. 题目如下: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A, ...

  2. POJ 1182 食物链 (带权并查集 && 向量偏移)

    题意 : 中文题就不说题意了…… 分析 : 通过普通并查集的整理归类, 能够单纯地知道某些元素是否在同一个集合内.但是题目不仅只有种类之分, 还有种类之间的关系, 即同类以及吃与被吃, 而且重点是题目 ...

  3. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  4. K - Find them, Catch them POJ - 1703 (带权并查集)

    题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...

  5. HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  6. A Bug's Life POJ - 2492 (带权并查集)

    A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...

  7. poj1182食物链,经典带权并查集

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种 ...

  8. POJ - 2912 Rochambeau (带权并查集+枚举)

    题意:有N个人被分为了三组,其中有一个人是开了挂的.同组的人的关系是‘=’,不同组的人关系是‘<’或'>',但是开了挂的人可以给出自己和他人任意的关系.现在要根据M条关系找出这个开了挂的人 ...

  9. POJ 1182 食物链 (经典带权并查集)

    第三次复习了,最经典的并查集 题意:动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们 ...

  10. poj 1703(带权并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31840   Accepted: ...

随机推荐

  1. 【原创】k8s之job和Cronjob

    1.失败任务 apiVersion: batch/v1 kind: Job metadata: name: bad spec: template: metadata: name: bad spec: ...

  2. MySQL 主从复制(上)

    目录 主从复制前提 主从复制原理 涉及的文件 & 线程 Master 主库 Slave 从库 主从复制的搭建 主库配置 配置 /etc/my.cnf 建立专用复制用户 查看 Binlog 信息 ...

  3. Failed to start LSB 网络服务启动失败

    有些时候 VMare虚拟机挂起,重新运行后,XShell远程连接不上,检查VMare虚拟机,重启网卡会报错,查看日志,会有 Failed to start LSB .... CentOS7 会有一个网 ...

  4. ++i和i++的区别

    它们两个的数值变化的区别,我这里就不多说了 这里主要说明两者在效率上的区别 (1)首先如果是自带的数据类型,比如int型,++i和i++,编译器的实现方式是相同的,两者并没有效率上的区别,虽然也有副本 ...

  5. hdu 5883

    Alice is planning her travel route in a beautiful valley. In this valley, there are NN lakes, and MM ...

  6. C++ part9

    1.静态多态和动态多态 静态多态:函数重载,模板.编译期间完成. 动态多态:虚函数.运行期间实现. 2.模板的实现和优缺点 函数模板的代码并不能直接编译成二进制代码,而是要实例出一个模板实例.写了模板 ...

  7. POJ 2288 Islands and Bridges(状压DP)题解

    题意:n个点,m有向边,w[i]表示i的价值,求价值最大的哈密顿图(只经过所有点一次).价值为:所有点的w之和,加上,每条边的价值 = w[i] * w[j],加上,如果连续的三个点相互连接的价值 = ...

  8. shiro<1.2.4反序列化分析

    0x01.环境搭建 下载地址:https://codeload.github.com/apache/shiro/zip/shiro-root-1.2.4 环境:Tomcat 8.5.27 + idea ...

  9. webpack-cli bugs All In One

    webpack-cli bugs All In One Error: Cannot find module 'webpack-cli/bin/config-yargs' webpack version ...

  10. Apple iPhone 12 Pro 数据迁移方式 All In One

    Apple iPhone 12 Pro 数据迁移方式 All In One iPhone 12 Pro https://mp.weixin.qq.com/s/US1Z_69zVQIhV-cNW1E6A ...