文章标题 POJ 1182 : 食物链 (带权并查集)
来源:互联网 发布:日本知乎 编辑:程序博客网 时间:2024/06/05 07:33
传送门
思路参考自 http://blog.csdn.net/niushuai666/article/details/6981689
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <queue>#include <set>#include <map>#include <algorithm>#include <math.h>#include <vector>using namespace std;typedef long long ll;const int mod=1e9+7;const int maxn=50000+10;int n,k;//relation[i]为0表示当前i与其根节点直接的关系是同类,1 表示被根节点吃,2表示吃根节点int fa[maxn],relation[maxn]; int find(int x){ if (x==fa[x])return x; int tmp=fa[x]; fa[x]=find(fa[x]); relation[x]=(relation[x]+relation[tmp])%3; return fa[x];} void init(){ for (int i=0;i<=n;i++){ fa[i]=i; relation[i]=0; }}int main(){ scanf ("%d%d",&n,&k); init(); int ans=0; int d,x,y; while (k--){ scanf ("%d%d%d",&d,&x,&y); if (x>n||y>n){ ans++;continue; } if(d==2){ if (x==y){ ans++;continue; } } int fx=find(x); int fy=find(y); if (fx!=fy){ fa[fy]=fx;//fx->fy //relation[fy]=fx->fy ==> fx->x + x->y + y->fy ==> relation[x]+(d-1)+3-relation[y] relation[fy]=((relation[x]+d-1+3-relation[y])%3+3)%3; }else { int tmp=((3-relation[x]+relation[y])%3); if (d==1&&relation[x]!=relation[y]){ ans++; continue; } if (d==2&&tmp!=d-1){ ans++; continue; } } } printf ("%d\n",ans); 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 食物链(并查集 带权路径)
- POJ 1182 食物链 (带权并查集)
- kuangbin专题五 并查集 POJ 1182食物链(带权并查集)重要
- poj 1182 食物链(带权并查集)
- poj 1182 食物链(带权并查集)
- POJ 1182 食物链(带权并查集)
- 【在线支付】在线支付代码详解
- window cmd编码改变
- 指令 AngularJs
- JSTL fn 标签库
- 解决There is no Action mapped for namespace for****异常
- 文章标题 POJ 1182 : 食物链 (带权并查集)
- 一个简单的C++程序(给17级新生的入门引导)
- hdu 4738(双联通缩点)
- Unity 实现鼠标拖拽查看地图
- 计算机网络:什么是套接字(Socket)?
- 阿里云服务器linux环境,从启动tomcat服务器外网访问--->绑定域名--->Navicat远程连接mysql--->阿里云服务器部署项目过程中,一定会遇到的五大问题;
- bzoj1597 [Usaco2008 Mar]土地购买(变形+斜率优化)
- 本地SQL查询
- Android——将每三个数字加上逗号处理(通常使用金额方面的编辑)