题意:

三种动物A,B,C,A吃B,B吃C,C吃A,

有人用两种说法对这N个动物所构成的食物链关系进行描述:

第一种说法是”1 X Y”,表示X和Y是同类。

第二种说法是”2 X Y”,表示X吃Y。

此人对N个动物,用上述两种说法,一句接一句地说出K句话,这K句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。

1) 当前的话与前面的某些真的话冲突,就是假话;

2) 当前的话中X或Y比N大,就是假话;

3) 当前的话表示X吃X,就是假话。

求假话数

分析:

最初想到用三个并查集分别表示A,B,C,然后用数组表示之间的联系,可是操作起来很麻烦,过了样例也一直WA,后来看了《挑战程序设计竞赛》上的解法,用偏移量表示他们之间的关系,处理起来方便很多。即

X为A,X+N为B,X+2*N为C

代码:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 500055;
int pa[maxn], _rank[maxn];
int _find(int x)
{
if(pa[x]==x) return x;
else return pa[x]=_find(pa[x]);
}
void unite(int x, int y)
{
int rx = _find(x), ry = _find(y);
if(rx==ry) return;
if(_rank[rx]>_rank[ry]) pa[ry]=rx;
else {
pa[rx] = ry;
if(_rank[rx]==_rank[ry]) _rank[ry]++;
}
return;
}
bool same(int x, int y)
{
return _find(x)==_find(y);
}
int main (void)
{
int n, k ;scanf("%d%d",&n,&k);
for(int i = 1; i <= 3 * n; i++) pa[i] = i;
int x, y, f, cnt = 0;
int mod = 3 * n;
for(int i = 0; i < k; i++){
scanf("%d%d%d",&f,&x,&y);
if(x>n||y>n) {cnt++;continue;}
if(f==1){
if(same(x,y+n)||same(x,y+2*n)) cnt++;
else{
unite(x,y);
unite(x+n,y+n);
unite(x+2*n,y+2*n);
}
}else {
if(same(x,y)||same(x, y + 2*n)) cnt++;
else {
unite(x, y+n);
unite(x+n,y+2*n);
unite(x+2*n, y);
}
}
}
printf("%d\n",cnt);
}

POJ 1182_食物链的更多相关文章

  1. poj 1182 食物链 (带关系的并查集)

      食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...

  2. poj 1182:食物链(种类并查集,食物链问题)

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

  3. POJ 1182 食物链

    G - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  4. POJ 1182 食物链(种类并查集)

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

  5. 【原创】poj ----- 1182 食物链 解题报告

    题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  6. POJ 1182 食物链(经典带权并查集 向量思维模式 很重要)

    传送门: http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: ...

  7. POJ 1182——食物链——————【种类并查集】

    食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status P ...

  8. POJ 1182 食物链 [并查集 带权并查集 开拓思路]

    传送门 P - 食物链 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit  ...

  9. POJ 1182 食物链 【并查集】

    解题思路:首先是没有思路的----然后看了几篇解题报告 http://blog.csdn.net/ditian1027/article/details/20804911 http://poj.org/ ...

随机推荐

  1. hihocoder offer收割编程练习赛12 A 歌德巴赫猜想

    思路: 枚举. 实现: #include <iostream> #include <cstdio> #include <algorithm> using names ...

  2. php debug/phpstorm调试

    apache+phpstorm调试php代码,修改php.ini配置文件开启调试,没有以下代码加上即可, [XDebug]zend_extension="C:\php\php-7.0.12- ...

  3. File文件存储

    文件存储的核心是Context提供了一个openFileOutput()与openFileInput()俩个方法 课程demo public class MainActivity extends Ap ...

  4. vue热重载

    依据官网使用 webpack 的 Hot Module Replacement API,Vuex 支持在开发过程中热重载 mutation.module.action 和 getter.你也可以在 B ...

  5. CSS 实现毛玻璃效果

    Part.1 HTML结构 <!-- 最外层盒子 --> <div class="box"> <!-- 添加毛玻璃效果盒子 --> <di ...

  6. java中mkdir()和mkdirs()区别

    mkdirs()可以建立多级文件夹 mkdir()只会建立一级的文件夹 例如: new File("/file/one/two").mkdirs(); 可建立/file/one/t ...

  7. 网络共享服务器 samba

    之前给自己centos 服务器配置了一下samba网络共享,主要是在windwos上编程,然后方便代码同步到linux上进行编译,现在大概记录一下过程,免得下次又忘记了 首先获取root权限 :su ...

  8. oracle亲手安装过程

    适用于centos6 radhat6版本 1.检查依赖库: rpm -q binutils compat-libcap1 compat-libstdc++ compat-libstdc++.i686 ...

  9. js 技巧 (六)JavaScript[对象.属性]集锦

    JavaScript[对象.属性]集锦 SCRIPT 标记 用于包含javascript代码. 语法 属性 LANGUAGE 定义脚本语言 SRC 定义一个URL用以指定以.JS结尾的文件 windo ...

  10. android 如何从activity跳转到另一个activity下指定的fragment

    思路: 跳转到目标fragment所在的activity,并传递一个flag,来确定要到哪个fragment,根据该flag判断后,跳转到指定的fragment即可. 代码: 当前界面: intent ...