POJ 1182 食物链(带权并查集)
来源:互联网 发布:notepad python插件 编辑:程序博客网 时间:2024/05/21 23:00
题目链接:点击
//rank[x]==0 表示Father[x]与x同类;//1 表示Father[x]吃x;//2 表示x吃Father[x];#include<stdio.h>#include<string.h>#include<stdlib.h>#include<ctype.h>#include<math.h>#include<stack>#include<queue>#include<map>#include<set>#include<vector>#include<string>#include<iostream>#include<algorithm>#include<utility>#include<iomanip>#include<time.h>typedef long long ll;const double Pi = acos(-1.0);const int N = 1e6+10, M = 1e3+20, mod = 1e9+7, inf = 2e9+10;const double e=2.718281828459 ;const double esp=1e-9;using namespace std;int n,k;int Father[50005];int rank[50005];int Find(int x){ int t; if(x!=Father[x]) { t=Father[x]; Father[x]=Find(Father[x]); rank[x]=(rank[x]+rank[t])%3; } return Father[x];}void Union(int x,int y,int d){ int p1=Find(x); int p2=Find(y); if(p1!=p2) Father[p1]=p2; rank[p1]=(rank[y]-rank[x]+3+d)%3;}int main(){ scanf("%d%d",&n,&k); for(int i=1; i<=n; i++) { Father[i]=i; rank[i]=0; } int sum=0; for(int i=0; i<k; i++) { int d,x,y; scanf("%d%d%d",&d,&x,&y); if(x>n||y>n||(d==2&&x==y)) { sum++; continue; } int p1=Find(x); int p2=Find(y); if(p1==p2) { if((rank[x]-rank[y]+3)%3!=d-1) sum++; } else Union(x,y,d-1); } printf("%d\n",sum); return 0;}
0 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 食物链 (带权并查集)
- kuangbin专题五 并查集 POJ 1182食物链(带权并查集)重要
- poj 1182 食物链(带权并查集)
- poj 1182 食物链(带权并查集)
- POJ 1182 食物链(带权并查集)
- poj 1182 食物链 //带权并查集
- java基础
- 命令行参数:关于java程序中main函数的String[] args参数的用法
- 关于java io知识的初步了解
- PHP服务器 支付宝 对接出现 【交易订单处理失败 请稍后再试 ALI38173】
- 【MyBatis学习09】SQL片段
- POJ 1182 食物链(带权并查集)
- 做视频的第三方
- Hbase Api 基于1.0.3版本
- android蓝牙开发---与蓝牙模块进行通信
- 带你理解beta分布
- JAVA HttpClient使用
- 【转】C++ 异常处理
- FZU2150 fire 双搜bfs
- 静态变量与动态变量的定义与区别