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这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...
随机推荐
- DB天气app冲刺第三天
昨天很郁闷而且烦躁的的过了一天 什么也没弄.今天其实也没有怎么做..进度非常慢.. 因为个人的问题 所以这两天的效率非常慢. 但今天还是做了一些东西.把listview做出来了.做出了一个按钮的效果. ...
- 基于strpos()函数的判断用户浏览器方法
$_SERVER['HTTP_USER_AGENT'],超全局变量,用来读取客户用的什么浏览器及其版本. strpos(),指定一个字符并搜索是否包含该字符. <html> <hea ...
- 随机产生字母a--z, A-Z 的任意组合
VERSION 1.0 引自: http://www.coderanch.com/t/134491/Security/generating-secure-tokens package demo; ...
- What Can I Do With This Major?
What Can I Do With This Major? Majors Don’t see your major? Accounting Advertising Africana Studies ...
- apt-get用法
转自apt-get语法- - 对于debian来说,安装软件大多都是通过apt-get来实现的. 1.apt-get update 更新软件包信息库.在Debian中,软件包是通过一个数据库来管理的, ...
- 想弄清楚LIBEVENT和EPOLL之类的关系,不要因为走得太急而迷失了方向
要一步一步构架起整个体系, 来日方长. 系统调用级别的EPOLL和函数库LIBEVENT之间,大体关系应该是如何呢? 网上有说法: ~~~~~~~~~~~~~~ libevent对底层多路复用机制的封 ...
- UVALive 6602 Counting Lattice Squares
给定一个n*m的网格,求面积为奇数的正方形有多少个. 首先是n*m个面积为1的,然后剩下的要么是边长为奇数,要么被这样一个奇数边长所包围. 原因如下: 对于一个边长不平行于坐标抽的正方形,其边长一定是 ...
- ssh 密钥详解
ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例. 有机器A(192.168.1.155),B(192.168.1.181).现想 ...
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-007-定义切面的around advice
一.注解@AspectJ形式 1. package com.springinaction.springidol; import org.aspectj.lang.ProceedingJoinPoint ...
- Altium Designer 常用快捷键总结
原理图:1:按住shift 拖动某个元件,可快速复制.2:按住鼠标滚轮 鼠标上下滑动 放大缩小.3:按住Ctrl 按住鼠标右键 鼠标上下滑动也放大缩小.4:按住Ctrl 拖动某个元件 可以移动位置 ...