这是《挑战设计程序竞赛》中的例题。


题目链接:http://poj.org/problem?id=1182

题意:中文题面。不赘述。

题解:

代码:

 //带权并查集
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = ; int f[maxn];
int dep[maxn];//深度 void init(int n){
for(int i = ; i <= n ;i++){
f[i] = i;
dep[i] = ;
}
} int find(int x){
if(x == f[x])
return x;
return f[x] = find(f[x]);
} void join(int x,int y){
x = find(x);
y = find(y);
if(x == y) return ;
if(dep[x] < dep[y]){
f[x] = y;
}
else{
f[y] = x;
if(dep[x] == dep[y])
dep[x]++;
}
} bool same(int x,int y){
return find(x) == find(y);
} int main(){
int n,k;
int d,x,y;
int ans = ;
scanf("%d%d",&n,&k);
init(n*);
for(int i = ; i < k; i++){
scanf("%d%d%d",&d,&x,&y);
if(x <= || x > n || y <= || y > n || ( d == && x == y)){
ans++;
continue;
}
if(d == ){
//xy同类
if( same(x,y+n) || same(x,y+*n)){
ans++;
continue;
}else{
join(x,y);
join(x+n , y+n);
join(x + *n, y + *n);
}
}else{
//x吃y
if(same(x,y)||same(x,y + *n)){
ans++;
continue;
}else{
join(x,y+n);
join(x + n, y + *n);
join(x + *n,y);
}
}
}
printf("%d",ans);
return ;
}

【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 Submissions: ...

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

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

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

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

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

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

  9. poj 1182 食物链 (并查集)

    http://poj.org/problem?id=1182 关于并查集 很好的一道题,开始也看了一直没懂.这次是因为<挑战程序设计竞赛>书上有讲解看了几遍终于懂了.是一种很好的思路,跟网 ...

  10. POJ 1182 食物链(并查集拆点)

    [题目链接] http://poj.org/problem?id=1182 [题目大意] 草原上有三种物种,分别为A,B,C A吃B,B吃C,C吃A. 1 x y表示x和y是同类,2 x y表示x吃y ...

随机推荐

  1. php开发面试题---1、php常用面试题一(PHP有哪些特性)

    php开发面试题---1.php常用面试题一(PHP有哪些特性) 一.总结 一句话总结: ①.混合语法:php独特混合了C,Java,Prel以及PHP自创的语法. ②.为动态网页而生:可以比CGI或 ...

  2. FM算法组合估计

    #include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h> # ...

  3. LeetCode 相交链表&环形链表II

    题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ 题目连接:https://leetcode-cn.com ...

  4. PHPStorm IDE 快捷键(MAC版)

    ⌘——Command ⌃ ——Control ⌥——Option/Alt ⇧——Shift ⇪——Caps Lock fn——功能键就是fn 编辑 Command+alt+T 用 (if..else, ...

  5. java-day18

    函数式接口在java中指:有且仅有一个抽象方法的接口 @FunctionalInterface注解:可以检测接口是否是一个函数式接口 函数式接口的使用:一般可以作为方法的参数和返回值类型 自定义函数式 ...

  6. python+tushare获取股票每日停复牌信息

    接口:suspend 更新时间:不定期 描述:获取股票每日停复牌信息 注:tushare模块下载和安装教程,请查阅我之前的文章 输入参数 名称      |      类型      |      必 ...

  7. 39-Ubuntu-用户管理-04-usermod设置主组和附加组

    主组:通常在新建用户时指定,在/etc/passwd的第4列GID显示主组名. 附加组:在/etc/group中最后一列显示该组的用户列表,用于指定用户的附加权限. 1.修改用户的主组 sudo us ...

  8. Switch控制器

    比如目前有qq和百度两个网站,switch可以指定选择一个 Switch value是填写索引值,从0开始.也可以书去请求的名词比如,qq或者百度 填写0就是访问qq 填写1就是访问百度

  9. 反编译字节码角度分析synchronized关键字的原理

    1.synchronized介绍 synchronized是java关键字.JVM规范中,synchronized关键字用于在线程并发执行时,保证同一时刻,只有一个线程可以执行某个代码块或方法:同时还 ...

  10. vue+elementui 中 @keyup 键盘上下左右移动聚焦

    <template> <el-table :data="CreditUnclearOutlineList" border style="width: 1 ...