https://vjudge.net/problem/POJ-1182

并查集经典题

对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类)。

把两个元素放在一个组代表他们同时发生。

被不合法数据卡了几次。

 #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f
typedef unsigned long long ll;
using namespace std;
const int N = ;
int n, k, d, x, y, ans=;
int pre[*];
int find(int x)
{
while(x != pre[x]){
x = pre[x];
}
return x;
}
int is_same(int a, int b)
{
int tx = find(a);
pre[a] = tx;
int ty = find(b);
pre[b] = ty;
if(tx == ty) return ;
else return ;
}
void join(int a, int b)
{
int tx = find(a);
int ty = find(b);
pre[tx] = ty;
}
void solve()
{
if(d == ){//x和y等价
if(x > n||x<=||y > n||y<=){//不合法
ans++;
return ;
}
else if(is_same(x, y+N)||is_same(x, y+*N))//冲突情况,B吃A,C吃A
ans++;
else{
join(x, y);
join(x+N, y+N);
join(x+*N, y+*N);
}
}
else if(d == ){//x吃y
if(x > n||x<=||y > n||y<=){//不合法
ans++;
return ;
}
else if(is_same(x, y)||is_same(x, y+*N)) //冲突情况,A同B,C吃A
ans++;
else{
join(x, y+N);
join(x+N, y+*N);
join(x+*N, y);
}
}
}
int main()
{
for(int i = ; i <= N*; i++){
pre[i] = i;
}
scanf("%d%d", &n, &k);
for(int i = ; i < k; i++){
scanf("%d%d%d", &d, &x, &y);
solve();
}
printf("%d\n", ans);
return ;
}

poj1182 食物链(并查集 好题)的更多相关文章

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

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

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

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

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

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

  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. 【HDU1231】How Many Tables(并查集基础题)

    什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...

  7. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

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

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

  9. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

随机推荐

  1. mongodb实现自增的方法

    前面操作看菜鸟教程 function getNextSequenceValue(sequenceName){ var sequenceDocument = Counter.findOneAndUpda ...

  2. move_uploaded_file中文乱码

    move_uploaded_file($_FILES['file']['tmp_name'],iconv("UTF-8","gb2312",$filname)

  3. mysql把查询结果集插入到表理

    把表B的内容插入到表A INSERT INTO 1111_0 SELECT*FROM report_0 把查询结果集插入到表中 insert into A(a,b,c) select from B(a ...

  4. bzoj 5099: [POI2018]Pionek

    题解: 还是比较简单的一道题 考虑现在有一个向量,当且仅当下一个向量与它夹角<90度这个向量的模长才会增加 接下来怎么做呢 如果我们去枚举初始向量,向量方向会随着新增向量而变化 随着不断顺时针的 ...

  5. [转]docker安装elk

    来源 https://www.jianshu.com/p/f549017c4b60   问题:virtual memory areas vm.max_map_count [65530] likely ...

  6. python全栈开发day67--字段类型、字段属性、ORM回顾

    一.回顾总结 https://www.cnblogs.com/maple-shaw/articles/9323320.html 1. 视图 1. CBV 和 FBV from django.views ...

  7. day3.python字符串格式化

    字符串格式化 注意:若在格式化输出中还需要再加%,需要两个%来代替 方法1:%控制格式化输出 例1: print("I'm %s. I'm %d year old" % ('Vam ...

  8. pip安装其他包报错

    pip安装时报错  Unknown or unsupported command 'install 一.是否配置了路径 配置了看下面的方法. 二.有多个pip系统不知道调用哪个. 1.where pi ...

  9. BZOJ2142 礼物 扩展lucas 快速幂 数论

    原文链接http://www.cnblogs.com/zhouzhendong/p/8110015.html 题目传送门 - BZOJ2142 题意概括 小E购买了n件礼物,送给m个人,送给第i个人礼 ...

  10. java读取配置文件方法以及工具类

    第一种方式 : java工具类读取配置文件工具类 只是案例代码  抓取异常以后的代码自己处理 import java.io.FileNotFoundException; import java.io. ...