POJ1182食物链 (并查集)
第一反应就是和那个搞基的虫子的题很像(poj2492 http://www.cnblogs.com/wenruo/p/4658874.html),不过是把种类从2变成了3。
错在很白痴的地方,卡了好久……
代码:
/*********************************************
Problem: 1182 User: G_lory
Memory: 972K Time: 266MS
Language: G++ Result: Accepted
*********************************************/
#include <cstdio>
#include <cstring> using namespace std; const int N = 50005; int par[N];
int rank[N];
int rel[N]; // 0: same, 1: father eat son, 2: son eat father void init(int n)
{
for (int i = 0; i <= n; ++i) {
par[i] = i;
rank[i] = 0;
rel[i] = 0;
}
} int find(int x)
{
if (par[x] == x) return x;
return find(par[x]);
} // return 1 means root eat x, return 2 mean x eat root, return 0 mean same
int rel_root(int x)
{
int p, ans = 0;
for (p = x; p != par[p]; p = par[p]) {
ans = (ans + rel[p]) % 3;
}
return ans;
} void unite(int x, int y, int c)
{
int rootx = find(x);
int rooty = find(y);
if (rootx == rooty) return ;
int relx = rel_root(x);
int rely = rel_root(y);
if (rank[rootx] < rank[rooty]) {
par[rootx] = rooty;
rel[rootx] = ((rely - relx - c) % 3 + 3) % 3;
} else {
par[rooty] = rootx;
rel[rooty] = ((relx - rely + c) % 3 + 3) % 3;
}
if (rank[rootx] == rank[rooty]) ++rank[rootx];
} bool same(int x, int y)
{
return find(x) == find(y);
} int main()
{
int n, k;
int ch, x, y;
scanf("%d%d", &n, &k);
int ans = 0;
init(n);
for (int i = 0; i < k; ++i) {
scanf("%d%d%d", &ch, &x, &y);
if (x > n || x <= 0 || y > n || y <= 0) {
++ans;
continue;
}
if (ch == 1) {
if (same(x, y)) {
if (rel_root(x) != rel_root(y)) ++ans;
} else unite(x, y, 0);
} else {
if (same(x, y)) { // x eat y
if ( !((rel_root(x) == 0 && rel_root(y) == 1) ||
(rel_root(x) == 1 && rel_root(y) == 2) ||
(rel_root(x) == 2 && rel_root(y) == 0)) )
++ans;
} else unite(x, y, 1);
}
}
printf("%d\n", ans);
return 0;
}
并查集模板:
const int N = 300005; int par[N]; // father
int rank[N]; // height void init(int n)
{
for (int i = 0; i < n; ++i) {
par[i] = i;
rank[i] = 0;
}
} int find(int x)
{
if (par[x] == x) return x;
return par[x] = find(par[x]); // 压缩路径
} void unite(int x, int y)
{
x = find(x); y = find(y);
if (x == y) return ;
if (rank[x] < rank[y]) par[x] = y;
else par[y] = x;
if (rank[x] == rank[y]) ++rank[x];
} bool same(int x, int y)
{
return find(x) == find(y);
}
POJ1182食物链 (并查集)的更多相关文章
- POJ-1182 食物链 并查集(互相关联的并查集写法)
题目链接:https://cn.vjudge.net/problem/POJ-1182 题意 中文题目,就不写了哈哈 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃 ...
- [poj1182]食物链(并查集+补集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64841 Accepted: 19077 Description ...
- POJ-1182 食物链---并查集(附模板)
题目链接: https://vjudge.net/problem/POJ-1182 题目大意: 中文题,不多说. 思路: 给每个动物创建3个元素,i-A, i-B, i-C i-x表示i属于种类x,并 ...
- poj1182食物链--并查集
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...
- POJ1182 食物链 并查集
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int ...
- 编程算法 - 食物链 并查集 代码(C)
食物链 并查集 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有N仅仅动物, 分别编号为1,2,...,N. 全部动物都属于A,B,C中的一种 ...
- POJ1182:食物链(并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 94930 Accepted: 28666 Description ...
- 【poj1182】食物链--并查集扩展域
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 109341 Accepted: 33191 Description 动物 ...
- POJ-1182 分组并查集
今天刚发现,之前做的并查集只是贴模板基本就能过,题意改变一点,自己还是不懂,其实我还没入门呢... 题意:食物链,A吃B,B吃C,C吃A,输入m组数据: 1 a b:a 和 b 是同一类 2 a b: ...
- POJ 1182 (经典食物链 /并查集扩展)
(參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...
随机推荐
- 8位灰度图在LCD上显示
一.概述 1.灰度 灰度使用黑色调表示物体,即用黑色为基准色,不同的饱和度的黑色来显示图像.每个灰度对象都具有从 0%(白色)到灰度条100%(黑色)的亮度值. 使用黑白或灰度扫描仪生成的图像通常以灰 ...
- 2D UI和3D UI的工作原理
2D UI的工作原理 UI控件的位置在UI Root 的红框(视窗)上,也就是UI控件的z轴,相机的z轴,UI Root的z轴都是0,因为2D UI都是纯粹的2D图片按层次显示,不会不出现三维立体效果 ...
- SendKeys总结
1.SendKeys中特殊字符的键代码BACKSPACE {BACKSPACE}.{BS} 或 {BKSP} BREAK {BREAK} CAPS LOCK {CAPSLOCK} DEL 或 DELE ...
- SDUT 1646 Complicated Expressions
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1646 题意 : 话说我根本没读题,,,因为实在 ...
- android 小米手机连接到电脑adb无法识别 解决方案
下载并安装小米手机助手 它会自动帮你安装驱动程序 安装成功后重启一下adb服务 应该就可以了
- linux-0.11内核 任务的堆栈切换
http://blog.163.com/di_yang@yeah/blog/static/86118492201212534924900/ 一直缠绕的两个问题:怎样标识的内核栈与用户栈?如何在内核态堆 ...
- C# Json处理日期和Table
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ru ...
- oprofile使用方法
安装oprofile,然后加载内核模块.#modprobe oprofile,模块加载后开始使用oprofile. 1. 首先设置监视内核,使用debuginfo提供的内核,/boot下面的内核无法使 ...
- IP隧道基础研究
static char banner[] __initdata = KERN_INFO "IPv4 over IPv4 tunneling driver\n"; static st ...
- 存储过程系列之存储过程具体操作过程及sql数据库调用
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. 存 ...