poj1182 食物链(并查集 好题)
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 食物链(并查集 好题)的更多相关文章
- POJ-1182 食物链 并查集(互相关联的并查集写法)
题目链接:https://cn.vjudge.net/problem/POJ-1182 题意 中文题目,就不写了哈哈 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃 ...
- POJ-1182 食物链---并查集(附模板)
题目链接: https://vjudge.net/problem/POJ-1182 题目大意: 中文题,不多说. 思路: 给每个动物创建3个元素,i-A, i-B, i-C i-x表示i属于种类x,并 ...
- [poj1182]食物链(并查集+补集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64841 Accepted: 19077 Description ...
- poj1182食物链--并查集
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...
- POJ1182 食物链 并查集
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int ...
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- 编程算法 - 食物链 并查集 代码(C)
食物链 并查集 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 有N仅仅动物, 分别编号为1,2,...,N. 全部动物都属于A,B,C中的一种 ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
随机推荐
- springboot中使用拦截器、监听器、过滤器
拦截器.过滤器.监听器在web项目中很常见,这里对springboot中怎么去使用做一个总结. 1. 拦截器(Interceptor) 我们需要对一个类实现HandlerInterceptor接 ...
- java数组知识点总结
数组是一个用来存储同一个数据类型多个元素的一个容器(数组长度是固定的,数组中存储的元素的数据类型要求一致) 1.格式: 格式1: 数据类型[] 数组名 = new 数据类型[数组长度]; 格式2: 数 ...
- 【loj6142】「2017 山东三轮集训 Day6」A 结论题+Lucas定理
题解: 当奇数 发现答案就是C(n,1)^2+C(n,3)^2+...C(n,n)^2 倒序相加,发现就是C(2n,n) 所以答案就是C(2n,n)/2 当偶数 好像并不会证 打表出来可以得到 2.当 ...
- Javascript \x 反斜杠x 16进制 编解码
js 里 \x 开头的通常是16进制编码的数据,下面代码实现编解码: 解码 function decode(str){ return str.replace(/\\x(\w{2})/g,functio ...
- 【AtCoder】AGC013
AGC013 A - Sorted Arrays 直接分就行 #include <bits/stdc++.h> #define fi first #define se second #de ...
- 2016-06-19 exshop第5天
昨天对grails3和spring-security进行了全面的调研并进行了试验,试用下来发现grails3的启动速度.代码修改后刷新速度.内存占用以及架构的设计上更加合理,asset-pipelin ...
- 【转】通过 INotifyPropertyChanged 实现观察者模式
通过 INotifyPropertyChanged 实现观察者模式 原博客地址 http://frankdzu.blog.sohu.com/117654536.html 普通观察者模式存在的问题 我们 ...
- 高性能之js
alloyteam团队的总结: 链接在:http://www.alloyteam.com/2012/10/high-performance-front-end-high-performance-jav ...
- 左连接不能与or否则in连用
select z.sjssny,z.XXSE,z.JXSE,z.nsrsbh, nsr.zgswskfj_dm,nsr.nsrmc,nsr.zgswj_dm,nsr.SSGLY_DM,nsr.nsr ...
- Dataset:利用Python将已有mnist数据集通过移动像素上下左右的方法来扩大数据集为初始数据集的5倍—Jason niu
from __future__ import print_function import cPickle import gzip import os.path import random import ...