poj 1182 食物链
来源:互联网 发布:睡眠仪 知乎 编辑:程序博客网 时间:2024/04/27 23:54
题目链接:http://poj.org/problem?id=1182
题目大意:
中文,题意不解释...
题目思路:
并查集---食物链问题.
dis[i]=0 : i与根同类.
dis[i]=1 : i吃根.
dis[i]=2 : i被根吃.
ps:比较喜欢用非递归的方法写并查集...个人爱好.
代码:
#include <stdlib.h>#include <string.h>#include <stdio.h>#include <ctype.h>#include <math.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <string>#include <iostream>#include <algorithm>using namespace std;#define ll long long#define ls rt<<1#define rs ls|1#define lson l,mid,ls#define rson mid+1,r,rs#define middle (l+r)>>1#define eps (1e-8)#define type int#define clr_all(x,c) memset(x,c,sizeof(x))#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))#define MOD 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define _max(x,y) (((x)>(y))? (x):(y))#define _min(x,y) (((x)<(y))? (x):(y))#define _abs(x) ((x)<0? (-(x)):(x))#define getmin(x,y) (x= ((x)<0 || (y)<(x))? (y):(x))#define getmax(x,y) (x= ((y)>(x))? (y):(x))template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}int TS,cas=1;const int M=50000+5;int n,k;int fa[M],dis[M];int find(int x){int rt,i,j,sum=0;for(rt=x;rt!=fa[rt];rt=fa[rt]) sum+=dis[rt];for(i=x;i!=rt;i=j){sum-=dis[i];dis[i]=(dis[i]+sum)%3;j=fa[i];fa[i]=rt;}return rt;}void run(){scanf("%d%d",&n,&k); int i,j;for(i=1;i<=n;i++) fa[i]=i,dis[i]=0;int cnt=0,o,x,y,xf,yf;while(k--){scanf("%d%d%d",&o,&x,&y);if(x>n || y>n || (o==2 && x==y)) cnt++;else{xf=find(x),yf=find(y);if(o==1) o=0;if(xf==yf){if((dis[x]+o)%3!=dis[y]) cnt++;}else{fa[yf]=xf;dis[yf]=(o-dis[y]+3+dis[x])%3;}}}printf("%d\n",cnt);}void preSof(){}int main(){ //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); preSof(); run(); //while(~scanf("%d%d",&n,&k)) run(); //for(scanf("%d",&TS);cas<=TS;cas++) run(); return 0;}
- poj 1182 食物链
- poj 1182 食物链
- poj 1182 食物链(转)
- poj 1182 食物链
- poj 1182 食物链
- poj 1182 食物链
- POJ-1182 食物链
- POJ 1182 食物链
- poj 1182食物链
- POJ 1182 食物链
- POJ 1182 食物链
- poj 1182 食物链
- POJ 1182 食物链
- POJ 1182 食物链
- poj 1182 食物链
- POJ 1182 - 食物链
- poj 1182 食物链
- poj 1182 食物链
- 大端与小端
- Sybase的安装、配置及使用(一)
- weblogic 内存OutOfMemoryError
- linux 下解压命令大全
- js实现一个贪吃蛇的小游戏(已经没有Bug了,已经完善)
- poj 1182 食物链
- Android 开机自启动应用
- hdu 2068 RPG的错排(错排)
- python小惊喜
- com.sun.mail.smtp.SMTPSendFailedException: 554 MI:STC 0,smtp5
- Linux中JDK1.6的安装和配置方法
- VC中在VS2008编译环境下创建属性表单中出现“所属资源不存在的错误”
- bmp图像复制,剪切,粘贴等
- Android Json的使用(1) 使用jsonschema2pojo生成class文件