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. linux实现自动检测进程是否存活的脚本

    可以在性能测试过程中.定期检测startAgent和nmon的状态 #!/bin/sh while true do pnmon=`ps aux | grep nmon | grep -v grep`; ...

  2. Codeforces 380D Sereja and Cinema (看题解)

    Sereja and Cinema 首先我们可以发现除了第一个人, 其他人都会坐在已入坐人的旁边. 难点在于计算方案数.. 我们可以从外往里把确定的人用组合数算上去,然后缩小范围. #include& ...

  3. Python 携程

    一.协程 1.又称微线程,纤程.英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程(相当于操作系统不知道它的存在,是用户控制的). 2.协程拥有自己的寄存器上下文和栈(代码的 ...

  4. HTML5拖放牛刀小试

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. 51Nod1518 稳定多米诺覆盖 动态规划 插头dp 容斥原理

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1518.html 题目传送门 - 51Nod1518 题意 51Nod真是个好OJ ,题意概括的真好, ...

  6. Practice| 类型转换| 逻辑运算

    类型转换 class Pratice1{ public static void main(String[] args){ int a = 3; //float f = 4.5;//TypeChange ...

  7. SSL/TLS

    為 授权计算机为 SSL/TLS 安全通道建立信任关系. ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, e ...

  8. Socket 网络通信

    Socket 网络通信 1.OSI (Open System Interconnect Reference Model)(开放系统互联参考模型) 从下低到高 :物理层.数据链路层.网络层.传输层.会话 ...

  9. Err.number错误号和可捕获的 Microsoft access 数据库引擎和 DAO错误说明

    错误码        信息2420        数字语法错误2421        日期语法错误2422        字符串语法错误2423        ‘.’.‘!’.或 ‘()’的使用无效2 ...

  10. web前端知识大纲:系列三 html篇

    web前端庞大而复杂的知识体系的组成:html.css和 javascript 三.HTML 1.BOM BOM 是 Browser Object Model的缩写,即浏览器对象模型,当一个浏览器页面 ...