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这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...
随机推荐
- 【JPA】query新对象 需要 构造函数
构造函数 @Query("select g from Note g where id=?1" ) Note findById(Long id); @Query("sele ...
- TCO 2014 Round 1A
顺利搞出 A B 两题,然后压线晋级了,手速场. A 题 , 求排列最小的,肯定从后往前来做,维护一个最小的set,只是第一个字母要特判一下. 1: #line 5 "EllysSorti ...
- C# - 中断模式下的调试
1. 设置断点 选中需要设置断点的行,右键选择断点插入断点,此行左侧显示红色圆形标志.或者F9 有几个条件断点类型: a. 条件断点 b. 命中次数,大于,几倍于,大于等于你设置的断点次数此时中断 c ...
- 1010 [HNOI2008]玩具装箱toy
斜率优化dp: 推荐学习http://www.cnblogs.com/perseawe/archive/2012/05/12/bz1010.html 看着别人的题解自己学着推,终于理解了 #inclu ...
- jquery dom ready, jqery2.1.1实现-源码分析
本文链接http://www.cnblogs.com/Bond/p/4178311.html jquery document ready的实现其很很简,虽说简单,其很很多人还是没去关注过它的实现.我 ...
- ***PHP多线程pthreads 实现QQ号码爬虫
通过空间历史浏览,爬出查看你空间的人(一般限制20人,除非开通黄钻),然后在爬出这20人的浏览记录,依次向下爬,你可以控制爬行深度.这里仅仅给出怕中代码片段,你可以进一步优化,将QQ分类存储.通过QQ ...
- php PDO连接mysql以及字符乱码处理
<?php //mysql 的 PDO $dsn = "mysql:dbname=cqkx;host:localhost"; $username = "root&q ...
- restful理解
越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时(high latency).高 ...
- uva 11646 - Athletics Track
题意:如图,体育场的跑道一圈是400米,其中弯道是两段半径相同的圆弧.已知矩形的长宽比例为a:b,求长和宽的具体数值. 注意:圆弧的圆心在纵轴线上! #include<iostream> ...
- Medium上关于git的文章
rebase和merge的正确使用时机 https://medium.com/@porteneuve/getting-solid-at-git-rebase-vs-merge-4fa1a48c53aa ...