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. 常用js正则表达式大全

    常用js正则表达式大全.一.校验数字的js正则表达式 1 数字:^[0-9]*$ 2 n位的数字:^\d{n}$ 3 至少n位的数字:^\d{n,}$ 4 m-n位的数字:^\d{m,n}$ 5 零和 ...

  2. element-ui上传文件带token

    template> <el-upload action="test" :headers="myHeaders"></el-upload& ...

  3. python基础——函数

    1.内置函数的调用: 可以在官方网站查找内置函数及说明,也可以通过help(abs)函数查看相应的信息. https://docs.python.org/3/library/functions.htm ...

  4. spring开发需要的配置文件

    1,applicationContext-dao.xml <?xml version="1.0" encoding="UTF-8"?><bea ...

  5. JavaEE 之 Spring Data JPA(二)

    1.JPQL a.定义:Java持久化查询语言(JPQL)是一种可移植的查询语言,旨在以面向对象表达式语言的表达式,将SQL语法和简单查询语义绑定在一起·使用这种语言编写的查询是可移植的,可以被编译成 ...

  6. php mysql 编码问题

    php mysql 编码问题 问题: PHP从数据库中读取数据,并echo出来,数据中文显示正常:但是echo出新定义的中文字符串,新定义的字符串会乱码. 由此可能是数据库中提取出来的中文编码和php ...

  7. Android动画曲线库AndroidEasingFunctions

    Android动画曲线库AndroidEasingFunctions AndroidEasingFunction是基于Easing Function(缓动函数)的Android动画曲线库.它提供了九大 ...

  8. jmeter时间格式化

    jmeter时间格式化 #17位时间取到毫秒 ${__time(yyyyMMdd-HHmmssSSS,)} #10位时间戳 ${__time(/1000,)} yyyyMMddHHmmss yyyy年 ...

  9. 理解JSON对象:JSON.parse、 JSON.stringify

    何时是JSON,何时不是JSON? JSON就是一个有特殊规则的字符串,按照这个规则我们就可以把这个字符串解析成JS对象. JSON是设计成描述数据交换格式的,他也有自己的语法,这个语法是JavaSc ...

  10. VC6.0学习C语言入门SDK

    度网盘链接 VC6.0    密码:t6bd VS2010 密码:3of2 C语言入门教程 在线视频地址(PS此链接摘抄至博主lellansin) Acfun.tv:http://www.acfun. ...