开3*n的并查集,其中x用来连接与x同类的,x+n用来连接x吃的,x+2*n用来连接x被吃的。

1 x y时,如果 x吃y 或 x被y吃,那么为假话,

否则x与y同类,x吃的y也吃,x被吃的y也被吃;

2 x y时,如果 x与y同类(x与x自然也是同类) 或 y吃x,那么为假话,

否则x吃y,y被x吃,y吃x被吃的。

代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
#include<map>
#include<stdlib.h>
using namespace std; int f[]; int find(int x)
{
if(f[x]==x)return x;
return f[x]=find(f[x]);
} void he(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)return;
f[y]=x;
} int main()
{
int n,m,ans=;
cin>>n>>m; for (int i=;i<=*n;i++)f[i]=i; for (int i=;i<=m;i++)
{
int op,x,y;
cin>>op>>x>>y;
if (x<||y<||x>n||y>n||(x==y&&op==))ans++;
else
{
if (op==)
{
if (find(x)==find(y+n)||find(x)==find(y+*n))ans++;
else
{
he(x,y);
he(x+n,y+n);
he(x+*n,y+*n);
}
}
else
{
if (find(x)==find(y)||find(y+*n)==find(x))ans++;
else
{
he(x,y+n);
he(x+n,y+*n);
he(x+*n,y);
}
}
}
}
cout<<ans;
}

code1074 食物链的更多相关文章

  1. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  2. NOI2001|POJ1182食物链[种类并查集 向量]

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

  3. tyvj1202 数数食物链

    描述 TsyD学习了生物的生态环境那一张后,老师留了一项作业,就是给一张食物网,求所有食物链的总数.(从最低营养级生物(它不能吃任何其他的生物)开始到最高营养级(它不能被任何其他生物吃) 叫做一条食物 ...

  4. vijos1531 食物链

    背景 安徽省芜湖市第二十七中学测试题 NOI 2001 食物链(eat) Description:OfficialData:OfficialProgram:JackDavid127 描述 动物王国中有 ...

  5. poj1182食物链_并查集_挑战程序设计竞赛例题

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

  6. BZOJ4562: [Haoi2016]食物链

    Description 如图所示为某生态系统的食物网示意图,据图回答第1小题 现在给你n个物种和m条能量流动关系,求其中的食物链条数. 物种的名称为从1到n编号 M条能量流动关系形如 a1 b1 a2 ...

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

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

  8. [并查集] POJ 1182 食物链

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

  9. 【BZOJ-4562】食物链 记忆化搜索(拓扑序 + DP)

    4562: [Haoi2016]食物链 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 133  Solved: 112[Submit][Status] ...

随机推荐

  1. Nginx优化指南

    大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了!而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...

  2. 在Mac下配置php开发环境:Apache+php+MySql (卡在 给mysql 设置不了账号密码)

    https://my.oschina.net/joanfen/blog/171109#OSC_h4_3 cmd 进入mysql的方法

  3. Thread pools & Executors

    Thread pools & Executors Run your concurrent code in a performant way All about thread pools # H ...

  4. JAVA构造函数在超类和子类调用注意事项

    1.构造函数:        当子类继承一个父类时,构造子类时需要调用父类的构造函数,存在三种情况        (1),父类无构造函数或者一个无参数构造函数,子类若无构造函数或者有无参数构造函数,子 ...

  5. Masking operations

    Using a mask, multiple bits in a nibble, byte, words can be set either on, off or inverted from on t ...

  6. apache DOCUMENT_ROOT

    问题描述:本地页面错误,+1上正常 本地及+1apache配置 <VirtualHost *:> ServerAdmin webmaster@dummy-host.example.com ...

  7. python开发_function annotations

    在看python的API的时候,发现了一个有趣的东东,即:python的方法(函数)注解(Function Annotation) 原文: 4.7.7. Function Annotations Fu ...

  8. leetcode162

    class Solution { public: int findPeakElement(vector<int>& nums) { int n = nums.size(); ) { ...

  9. 华为路由器pppoe拨号

    一.概要 模拟pppoe 拨号 , 软件 ensp  , 简单拓扑: 二.配置 1.基本环境配置 pc1 AR1 int gi 0/0/0 ip add 192.168.10.254 24 2.开始 ...

  10. 获取lable选中时触发事件

    通常做网页时不会用radio和checkbox的原有样式, 会进行样式美化. 如何在点击checkbox时触发一个事件呢? <div class="main-checkbox" ...