【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 ...
随机推荐
- 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_自定义控件--主要用于画面拖拽效果)
如题,需求:在某个图片上用户可以手动指定位置. 如下: 中心思想:仿照Visual Studio工具中的控件的做法 如何仿照呢? 1.自定义的控件类继承System.Windows.Forms.Con ...
- c#网络通信框架networkcomms内核解析之三 消息同步调用
networkcomms.net 来自英国的网络通信框架 官方网址 www.networkcomms.net 中文网址www.networkcomms.cn 客户端发送消息给服务器,服务器计算结果返回 ...
- 9. Python函数
函数 函数能提高应用的模块性,和代码的重复利用率.定义一个函数比较简单,但是需要遵循以下几点规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 任何传入参数和自变量必须放在圆 ...
- PAT_A1048#Find Coins
Source: PAT A1048 Find Coins (25 分) Description: Eva loves to collect coins from all over the univer ...
- 获取hdfs集群信息(fs.defaultFS)
[root@hive-dp-7bd6fd4d55-wctjn hive-1.1.0-cdh5.14.0]# hdfs getconf -confKey fs.default.name19/12/04 ...
- 2019ICPC南京网络赛A The beautiful values of the palace
题意:蛇形填数超大版本,需要求出一些给定坐标的值的数位和,然后q次询问,一个矩形区域内值的和是多少 解题思路:二维偏序前缀和的经典题 二维偏序:求(x,y)左下角点的个数,思路是对x,y升序排序,用树 ...
- STL 队列模板实现
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u010016150/article/details/32715801 C++ Prime确实有点 ...
- pytest_按标记执行
import pytest@pytest.mark.webtestdef test_send_http(): pass @pytest.mark.apptestdef test_devide(): p ...
- 22-4-isarry
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用jQuery的datetimepicker插件
因为后台系统要使用年月日时分的设置,又因为使用的Bootstrap框架只有datepicker和timepicker控件.所以在jQuery库中找到轻量级的datetimepicker插件,很好地解决 ...