POJ-1182(经典带权并查集)
食物链
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(经典带权并查集)的更多相关文章
- poj 1182 食物链 带权并查集
食物链是并查集的进阶运用的一道非常经典的题目. 题目如下: 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A, ...
- POJ 1182 食物链 (带权并查集 && 向量偏移)
题意 : 中文题就不说题意了…… 分析 : 通过普通并查集的整理归类, 能够单纯地知道某些元素是否在同一个集合内.但是题目不仅只有种类之分, 还有种类之间的关系, 即同类以及吃与被吃, 而且重点是题目 ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...
- 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 ...
- A Bug's Life POJ - 2492 (带权并查集)
A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...
- poj1182食物链,经典带权并查集
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种 ...
- POJ - 2912 Rochambeau (带权并查集+枚举)
题意:有N个人被分为了三组,其中有一个人是开了挂的.同组的人的关系是‘=’,不同组的人关系是‘<’或'>',但是开了挂的人可以给出自己和他人任意的关系.现在要根据M条关系找出这个开了挂的人 ...
- POJ 1182 食物链 (经典带权并查集)
第三次复习了,最经典的并查集 题意:动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们 ...
- poj 1703(带权并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31840 Accepted: ...
随机推荐
- B. Modular Equations
Last week, Hamed learned about a new type of equations in his math class called Modular Equations. L ...
- centos 7下安装配置Supervisor
1.安装Supervisor centos下安装yum install supervisor 2. systemctl enable supervisord 开机自启 systemctl start ...
- SpringBoot 启动慢的解决办法
项目集成了很多内容,有 700 多个类,IDEA 中启动一次需要 70 秒,非常影响开发效率. 研究问题原因发现有以下几种情况会导致启动速度慢,优化后启动只需 26 秒左右了: 1. 和网卡有关,禁用 ...
- kubernetes进阶(三)服务发现-coredns
服务发现,说白了就是服务(应用)之间相互定位的过程. 服务发现需要解决的问题: 1.服务动态性强--容器在k8s中ip变化或迁移 2.更新发布频繁--版本迭代快 3.支持自动伸缩--大促或流量高峰 我 ...
- woj1002-Genesis woj1003-birthofnoah woj1004-noah's ark
title: woj1002-Genesis date: 2020-03-05 categories: acm tags: [acm,woj] 输入输出考虑一下.easy #include <i ...
- Leetcode(3)-无重复字符的最长子串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. 给定 &q ...
- cs实时系统之网关设计
今天给大家讲一下client-server系统(cs)设计,基本结构 1.client 客户端,插件式开发,负责对应ui的展示 2.gateway 网关层,管理客户端通信连接,负载后端集群服务 3.s ...
- Linux 应用开发----socket编程笔记
Linux socket编程 套接字定义描述 套接字的域 AF_INET ====>IPv4 AF_INET6 ====>IPv6 AF_UNIX ====>unix 域 AF_UP ...
- ES6 Map All In One
ES6 Map All In One Map 字典/地图 Set 集合 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Referenc ...
- ES-Next @Decorator All In One
ES-Next @Decorator All In One @装饰器 import { logged } from "./logged.mjs"; class C { @logge ...