第一反应就是和那个搞基的虫子的题很像(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食物链 (并查集)的更多相关文章

  1. POJ-1182 食物链 并查集(互相关联的并查集写法)

    题目链接:https://cn.vjudge.net/problem/POJ-1182 题意 中文题目,就不写了哈哈 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃 ...

  2. [poj1182]食物链(并查集+补集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64841   Accepted: 19077 Description ...

  3. POJ-1182 食物链---并查集(附模板)

    题目链接: https://vjudge.net/problem/POJ-1182 题目大意: 中文题,不多说. 思路: 给每个动物创建3个元素,i-A, i-B, i-C i-x表示i属于种类x,并 ...

  4. poj1182食物链--并查集

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

  5. POJ1182 食物链 并查集

    #include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int ...

  6. 编程算法 - 食物链 并查集 代码(C)

    食物链 并查集 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有N仅仅动物, 分别编号为1,2,...,N. 全部动物都属于A,B,C中的一种 ...

  7. POJ1182:食物链(并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 94930   Accepted: 28666 Description ...

  8. 【poj1182】食物链--并查集扩展域

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 109341   Accepted: 33191 Description 动物 ...

  9. POJ-1182 分组并查集

    今天刚发现,之前做的并查集只是贴模板基本就能过,题意改变一点,自己还是不懂,其实我还没入门呢... 题意:食物链,A吃B,B吃C,C吃A,输入m组数据: 1 a b:a 和 b 是同一类 2 a b: ...

  10. POJ 1182 (经典食物链 /并查集扩展)

    (參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...

随机推荐

  1. css helper class

    应该习惯的css helper class .text-centered text-align: center; .text-right text-align: right; .small small ...

  2. 【ElasticSearch】

    ElasticSearch是基于Lucene开发的分布式搜索框架,包含如下特性: 分布式索引.搜索 索引自动分片.负载均衡 自动发现机器.组建集群 支持Restful 风格接口 配置简单等.

  3. C#微信登录-电脑版扫描二维码登录

    像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...

  4. To fix sql server 2008 r2 Evaluation period has expired by change the key

    PTTFM-X467G-P7RH2-3Q6CG-4DMYB 数据中心版:PTTFM-X467G-P7RH2-3Q6CG-4DMYB   测试可用 开 发者 版:MC46H-JQR3C-2JRHY-XY ...

  5. hadoop2.4.0 安装配置 (2)

    hdfs-site.xml 配置如下: <?xml version="1.0" encoding="UTF-8"?> <?xml-styles ...

  6. zepto源码学习-02 工具方法-详细解读

    上一篇:地址 先解决上次留下的疑问,开始看到zepto.z[0]这个东西的时候,我很是不爽,看着它都不顺眼,怎么一个zepto的实例对象var test1=$('#items');  test__pr ...

  7. 【NOIP 2014 DAY1 T3】飞扬的小鸟(DP)

    题目描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管或者掉在地上的话,便 ...

  8. Druid数据库连接池两种简单使用方式

    阿里巴巴推出的国产数据库连接池,据网上测试对比,比目前的DBCP或C3P0数据库连接池性能更好 简单使用介绍 Druid与其他数据库连接池使用方法基本一样(与DBCP非常相似),将数据库的连接信息全部 ...

  9. Ember.js demo4

    <!DOCTYPE html> <html> <head> <meta name="description" content=" ...

  10. wzplayer2 for windows ActiveX 试用地址

    提供wzplayer2 for windows ActiveX,测试地址:http://www.coolradio.cn/IE.htm 大家使用时候必须允许未签名ActiveX下载和运行,否则将无法正 ...