poj1182
来源:互联网 发布:考勤系统数据读不出 编辑:程序博客网 时间:2024/06/05 12:49
带权并查集。
#include<cstdio>#include<cstring>#include<string.h>#include<iostream>#include<math.h>#include<algorithm>#include<vector>#include<set>using namespace std;int father[50005];int rank[50005];void sent(int x){father[x]=x;rank[x]=0;}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 xf=find(x);int yf=find(y);father[xf]=yf;rank[xf]=(rank[y]-rank[x]+2+d)%3;}int main(){int total=0;int i,n,k,x,y,d,xf,yf;cin>>n>>k;for(i=1;i<=n;i++)sent(i);while(k--){cin>>d>>x>>y;if(x>n||y>n||(d==2&&x==y))total++;else{xf=find(x);yf=find(y);if(xf==yf){if((rank[y]-rank[x]+3)%3!=d-1)total++;}elseUnion(x,y,d-1);}}cout<<total<<endl;return 0;}
阅读全文
0 0
- poj1182
- poj1182
- poj1182
- POJ1182
- POJ1182
- poj1182
- poj1182
- poj1182
- poj1182
- poj1182
- poj1182
- POJ1182
- poj1182
- POJ1182 食物链
- POJ1182食物链
- poj1182 食物链
- POJ1182--食物链
- poj1182-食物链
- RFC说明
- 创建随机长度链表并显示
- RFC20 用于网络交换的ASCII格式
- mysql 分表 分区
- 虚拟机虚拟网卡断网问题排查处理
- poj1182
- Windows 下 PyQt4 的安装
- How to Upgrade Linux Kernel to Stable 3.18.4 on CentOS 7.x
- Chapter fourteen ARM概述(1)
- 排序算法-----冒泡排序简单介绍
- 7.1 Hibernate:内置生成器 -- assigned
- mysql 分区
- Java final 变量,类,方法
- MathWorks机器学习简介-2