poj 1182
http://poj.org/problem?id=1182
一个利用并查集的经典题目。
思路:在网上看到别人的思路,觉得方法还是挺不错的。
首先,开辟一个3*n的数组belg,用来存b和c的关系,在belg[c+m]存,c是被谁吃的,belg[c+2*m]存b是吃谁的。
bool judge(int x,int y) {return (Find(x)==Find(y));}
如果judge(b,c+m)和 judge(b,c+2*m)都为假的话,那么可以说明,b和c是在同类。
然后合并b,c。b+m,c+m。b+2m,c+2m。
第一个judge可以判断,b不会吃c,第二个Judge可以判断c也不会吃b,所以,b,c肯定是同类。
如果judge(b,c)和judge(b,c+2*m)都为假的话,那么可以说明,c是被b吃的,然后合并b,c+m。b+m,c+2*m。b+*2m,c;
第一个judge可以判断b和c不是同一类,第二个judge可以判断c不会吃b,那么就可以说明c是被b吃的。c是被b吃的,那么说明,c+m存的就是b,所以c+m和b合并。之后的也是同样的意思。
#include <stdio.h>
#include <string.h> int belg[]; int Find(int x) //Find 的迭代写法。
{
int _x=x,_b;
while(_x!=belg[_x])
{
_x=belg[_x];
}
while(x!=belg[x])
{
_b=belg[x];
belg[x]=_x;
x=_b;
}
return _x;
} bool judge(int x,int y) {return (Find(x)==Find(y));} void unite(int x,int y)
{
int root1=Find(x);
int root2=Find(y);
if(root1!=root2) belg[root1]=root2;
} int main()
{
int m,n,a,b,c,ans=;
// freopen("in.txt","r",stdin);
scanf("%d%d",&m,&n);
for(int i=;i<=m*;i++)
belg[i]=i;
while(n--)
{
scanf("%d%d%d",&a,&b,&c);
if(b>m||c>m) ans++;
else if(a==)
{
if(judge(b,c+m)||judge(b,c+*m)) ans++;
else { //b,c是同类时,合并所有的信息。
unite(b,c);
unite(b+m,c+m);
unite(b+*m,c+*m);
}
}
else {
if(judge(b,c)||judge(b,c+*m)) ans++;
else { //b,c不是同类时,合并那些是同类的信息。
unite(b,c+m);
unite(b+m,c+*m);
unite(b+*m,c);
}
}
}
printf("%d\n",ans);
return ;
}
poj 1182的更多相关文章
- 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
C - 食物链 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 食物链 (带关系的并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...
- 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 关于并查集 很好的一道题,开始也看了一直没懂.这次是因为<挑战程序设计竞赛>书上有讲解看了几遍终于懂了.是一种很好的思路,跟网 ...
- POJ 1182 (经典食物链 /并查集扩展)
(參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...
随机推荐
- Lucene.Net+盘古分词->开发自己的搜索引擎
//封装类 using System;using System.Collections.Generic;using System.Linq;using System.Web;using Lucene. ...
- Kindeditor 代码审计
<?php /** * KindEditor PHP * * 本PHP程序是演示程序,建议不要直接在实际项目中使用. * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置. * */ r ...
- 别老扯什么Hadoop了,你的数据根本不够大
本文原名“Don't use Hadoop when your data isn't that big ”,出自有着多年从业经验的数据科学家Chris Stucchio,纽约大学柯朗研究所博士后,搞过 ...
- HITtrainning20140417题解
题目列表: ID Origin Title 10 / 15 Problem A FZU 2152 文件系统 0 / 16 Problem B FZU 2153 A simple geome ...
- django book
一.安装配置 1.下载地址: https://www.djangoproject.com/download/ 2.安装: tar zxvf Django-1.6.1.tar.gz && ...
- 【转载】android中.9png
在Android的设计过程中,为了适配不同的手机分辨率,图片大多需要拉伸或者压缩,这样就出现了可以任意调整大小的一种图片格式“.9.png”.这种图片是用于Android开发的一种特殊的图片格式,它的 ...
- svn还原到指定版本
svn还原到指定版本 1,选中文件夹,右健,show log 2,选中指定版本,右健,Revert to this revision 3,svn commit 4,ok
- 文字的多列布局--column
- 【codevs1163】访问艺术馆
题目描述 皮尔是一个出了名的盗画者,他经过数月的精心准备,打算到艺术馆盗画.艺术馆的结构,每条走廊要么分叉为二条走廊,要么通向一个展览室.皮尔知道每个展室里藏画的数量,并且他精确地测量了通过每条走廊的 ...
- ajax异步提交数据动态更改select选项
<!DOCTYPE html> <html> <head> <title></title> <script src="../ ...