【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 ...
随机推荐
- DevOps到底是什么鬼?DevOps介绍及工具推荐。
什么是DevOps DevOps是Development和Operations的组合,是一组过程.方法与系统的统称,用于促进开发(应用程序/软件工程).技术运营和质量保障(QA)部门之间的沟通.协作与 ...
- ps--窗口配置
移动工具设置 v 选择图层自动选择不勾 图层ctrl + 左键 视图设置智能参考线标尺 Ctrl+r 窗口设置 关闭 库 颜色 打开 信息 字符 图层 历史记录 信息 面板选项 鼠标单位 像素 rgb ...
- zabbix--源码安装部署zabbix3.2
zabbix运行在lamp环境或者lnmp环境都是可以的,如果是新系统推荐使用lamp或者lnmp一键安装包, 或者可以向下面这种方式: PHP安装 源码安装 rpm -ivh php55w-comm ...
- Samza系统架构
- nginx : OpenEvent Faied access is denied
主要是naginx做成服务后, 服务运行的用户权限不够(他是system的),而cmd运行的是(Administrator) 所以我们去服务里,修改成以下就好了
- 笔记34 Spring MVC的高级技术——处理multipart形式的数据
一.需求介绍: Spittr应用在两个地方需要文件上传.当新用户注册应用的时候,我 们希望他们能够上传一张图片,从而与他们的个人信息相关联.当用 户提交新的Spittle时,除了文本消息以外,他们可能 ...
- one-hot encoding与哑变量的区别
one-hot encoding与哑变量的区别 one-hot比哑变量的特征位多一位,即哑变量是精简版的one-hot,即在线性回归中用截距项来表示最后一维,但由于最初很难分辨特征的主次关系,且机器学 ...
- WordPress .gitignore
# ----------------------------------------------------------------- # .gitignore for WordPress @salc ...
- 使用sql对比Mysql中数据库2个表结构
比较两个数据表的结构 SELECT column_name, max( CASE WHEN table_name = 'table1' AND table_schema = 'db1' THEN 'Y ...
- List之取不同元素
1.工具-->NuGet包管理器-->管理解决方案的NuGet包程序包 2.添加System.Linq包 3.引用 using System.linq List<int> li ...