【POJ】1182 食物链
这是《挑战设计程序竞赛》中的例题。
题目链接: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 食物链的更多相关文章
- poj 1182 食物链 (带关系的并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
- POJ 1182 食物链
G - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- POJ 1182 食物链(种类并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63592 Accepted: 18670 Description ...
- POJ 1182 食物链(经典带权并查集 向量思维模式 很重要)
传送门: http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- POJ 1182——食物链——————【种类并查集】
食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status P ...
- POJ 1182 食物链 [并查集 带权并查集 开拓思路]
传送门 P - 食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit ...
- 【原创】poj ----- 1182 食物链 解题报告
题目地址: http://poj.org/problem?id=1182 题目内容: 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- poj 1182 食物链 (并查集)
http://poj.org/problem?id=1182 关于并查集 很好的一道题,开始也看了一直没懂.这次是因为<挑战程序设计竞赛>书上有讲解看了几遍终于懂了.是一种很好的思路,跟网 ...
- 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 ...
随机推荐
- spring 对JDBC的支持 (8)
目录 一.jdbc的简介 二.jdbcTemplate 的使用 2.1 maven 引入spring - jdbc ,c3p0 ,数据库mysql驱动 2.2 配置 数据源以及jdbcTemplate ...
- Openstack 中的消息总线 & AMQP
目录 目录 消息总线 消息总线的原理 AMQP 消息总线 Openstack 采用了面向服务的开发模式(有别于面向对象和面向过程),需要我们去考虑各个服务之间和各项目之间是如何传递消息的. Restf ...
- 【C++】清空一个C++栈的快速方法
来源:https://stackoverflow.com/questions/40201711/how-can-i-clear-a-stack-in-c-efficiently/40201744 传统 ...
- Selenium3 + Python3自动化测试系列七——多窗口切换
多窗口切换 在页面操作过程中有时候点击某个链接会弹出新的窗口,这时就需要主机切换到新打开的窗口上进行操作. WebDriver提供了switch_to.window()方法,可以实现在不同的窗口之间切 ...
- openssl编译方法
受不了了,终于编译成功了openssl,写一下编译方法吧 准备: 0:要编译openssl,必不可少的是代码,去下载 https://www.openssl.org/source/ 1:要有一个VS系 ...
- display default HeapSize of Java VM
window OS: java -XX:+PrintFlagsFinal -version | findstr HeapSize Linux OS java -XX:PrintFlagsFinal - ...
- PHP72w安装
PHP72w # rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # rpm ...
- AES加密php,java,.net三种语言同步实现加密、解密
话不多数上代码: java::: /* * To change this license header, choose License Headers in Project Properties. * ...
- VS 2019企业版激活码
Visual Studio 2019 EnterpriseBF8Y8-GN2QH-T84XB-QVY3B-RC4DF
- Delphi里面弹出对话框的方法
1.procedure ShowMessage(const Msg: string); 单元:Dialogsor QDialogs 例子:showmessage( 'hello '); ...