比基础的并查集有些进步。

在以下这个链接中有详解:

http://blog.csdn.net/ditian1027/article/details/20804911

对于每两个动物的关系,都是先推与终于的关系,在逆推与还有一个的关系;

num中存的都是与终于节点的关系;

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn=50000+10;
struct node{
int q,num;
}s[maxn];
void qq(int n)
{
for(int i=1;i<=n;i++)
{
s[i].q=i;
s[i].num=0;
}
} int find(int y)
{
if(y==s[y].q)
return y;
int t=s[y].q;
s[y].q=find(s[y].q);
s[y].num=(s[t].num+s[y].num)%3;
return s[y].q;
}
void show(int q,int w,int e)//合并集合
{
int tt=find(q);
int rr=find(w);
s[rr].q=tt;
s[rr].num=(s[q].num-s[w].num+3+(e-1))%3;
} int main()
{
int a,b,n,m,g;
scanf("%d %d",&a,&b);
qq(a);
int ans=0;
while(b--)
{
scanf("%d%d%d",&n,&m,&g);
if(m>a||g>a)
{ans++;continue;}
if(n==2&&m==g)
{ans++;continue;}
if(find(m)==find(g))
{
if(n==1&&s[m].num!=s[g].num) ans++;
if(n==2&&(s[m].num+1)%3!=s[g].num) ans++;
}
else
show(m,g,n);
}
printf("%d\n",ans);
return 0;
}

poj 食物链的更多相关文章

  1. poj 1703 Find them, Catch them(种类并查集和一种巧妙的方法)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36176   Accepted: ...

  2. HDU1829(种类并查集)

    ps:本来是想找个二分图判断的题来写,结果百度到这鬼题 Problem Description Background Professor Hopper is researching the sexua ...

  3. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集

    D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...

  4. 二分图判定+点染色/并查集 BestCoder Round #48 ($) 1002 wyh2000 and pupil

    题目传送门 /* 二分图判定+点染色:因为有很多联通块,要对所有点二分图匹配,若不能,存在点是无法分配的,no 每一次二分图匹配时,将点多的集合加大最后第一个集合去 注意:n <= 1,no,两 ...

  5. poj 1182:食物链(种类并查集,食物链问题)

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

  6. POJ 1182 食物链

    G - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  7. DisJSet:食物链(POJ 1182)

           动物王国的食物链 这一题有两种思路,先介绍第一种: 题目是中文的,我就不翻译了,也很好理解,就是一个A-B-C-A的一个循环的食物链,给定一些条件,问你哪些条件是错的 这一题,是一道比较 ...

  8. 食物链 poj 1182

    C - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  9. POJ 1182 食物链(种类并查集)

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

随机推荐

  1. 阿里云修改CentOS Linux服务器的主机名

    阿里云主机的默认主机名是为AY开头的随机名称,如何修改为易于区分的友好名称呢?请看下面的操作步骤: 1. vi /etc/hosts i键,修改主机名,esc键,:wq键保存退出 2. vi /etc ...

  2. react-native第一次开发记录

    1.安装指定版本 react-native init demo --verbose --version 0.41.0 2.更新依赖包 npm install -g npm-check-updates ...

  3. PHP编程基础学习(一)——数据类型

    PHP一共支持8种原始类型,其中: 4种标量类型: boolean(布尔型) integer(整型) float/double(浮点型) string(字符串型) 两种复合类型: array(数组) ...

  4. Laravel框架初学一路由(路由参数)

    必要参数 有时需要在路由中捕获到URI的一些参数.比如,需要捕获URI中的用户id,可以这样来定义路由 Route::get("user/{id}", function ($id) ...

  5. Standard NSD file

    %pool: pool=system blockSize=256K layoutMap=cluster allowWriteAffinity=no %pool: pool=datapool block ...

  6. luogu4407 [JSOI2009]电子字典 字符串hash + hash表

    暴力枚举,然后\(hash\)表判断 复杂度\(O(26 * 20 * n)\) 具体而言 对于操作1:暴力枚举删除 对于操作2:暴力添加,注意添加不要重复 对于操作3:暴力替换,同样的注意不要重复 ...

  7. 关于zip伪加密

    创建一个zip文件 然后用winhex打开 可以看到第二个PK头的地方对应hex区域有一场串0000000000 在这里的第四个0这里末尾修改成奇数 奇数为加密 偶数为不加密 再去打开就可以看到加密了

  8. python开发_tkinter_多级子菜单

    在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...

  9. Codeforces Round #257 (Div. 2) A. Jzzhu and Children

    A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...

  10. jquery 图片预加载

    /图片无序预加载 (function($){ function Preload(imgs,fns){ this.imgs=(typeof imgs==="string")?[img ...