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


题目链接: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. JEECG树(TreeGrid)字段的扩展

    转载:https://blog.csdn.net/huangzirong822/article/details/38400817 之前使用jeecg集成框架做过一个项目 这里说说框架中树(tree)字 ...

  2. 洛谷 P1613 跑路 (倍增 + DP + 最短路)

    题目链接:P1613 跑路 题意 给定包含 \(n\) 个点和 \(m\) 条边的有向图,每条边的长度为 \(1\) 千米.每秒钟可以跑 \(2^k\) 千米,问从点 \(1\) 到点 \(n\) 最 ...

  3. 专题:OpenSSH tunneling

    SSH tunneling 相关 参考資料:http://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/ 本地端口转发: 适用场景:发起端可以 ...

  4. XVIII Open Cup named after E.V. Pankratiev Stage 5: Eastern Grand Prix

    contest link easy: EI medium-easy: BCDGK medium: L medium-hard: AFH A. Artifacts B. Brackets and Dot ...

  5. 怎样在Cocos2d-x中使用Lua脚本

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013321328/article/details/25699545 笔者使用的是Cocos2d-x ...

  6. react-swiper 如何实现滑动小卡片的移动?

    1.引入插件 import ReactSwipes from 'react-swipes'; import './swiperCard.css'; 2.代码构成 export default clas ...

  7. windows平板软件开机自启动+霸屏的操作方法

        转载(忘了地址) 很好很强大.成功亲测 使用你自己的账号(最好是管理员权限的账号)登录Windows,然后添加一个给其他人使用的账户(假设为other),注意一定要为other设置密码. 运行 ...

  8. ES7/ES8 语法学习

    作为一个前端开发者,感觉需要学习的东西贼多,ES6刚学会用没多久,又得学习了解ES7/ES8新增的东西,这里是看了大佬们文章的一点点总结以及摘抄的内容,给自己当笔记使用 内容转载自:https://w ...

  9. look at me

    I would bet my life, like I bet my heart我以生命与真心担保That you were the one, baby你就是我的命中注定I've never been ...

  10. Bootstrap快速入门并且建立常用模板

    对于移动端,能够快速搭建网站,操作相对简单 ,更容易掌握,这篇文章就介绍一下BootStrap 一  什么是Bottstrap 一个前端开发的框架,Bootstrap,来自 Twitter,是目前很受 ...