poj1182 带权并查集
来源:互联网 发布:广西公务员网络考试 编辑:程序博客网 时间:2024/06/07 20:51
/** * poj1182 带权并查集 * 运用并查集进行分类、合并的操作,最开始大家都是自己为一个集合,随着小集合逐渐结成,小集合向另一个集合合并时,就可以将吃和被吃的关系统一起来,找到统一的参照系 * 这就是带权并查集存在的意义 * 在unionset函数中,负责合并的时候,将被合并一个集合的根的类型进行相应翻转,而在getfather函数中,自上而下对该树枝进行了修正 */#include <cstdio>const int MAX_NUM = 50005;int n,k;struct animal{ int father; char type;}a[MAX_NUM];int getfather(int x){ int oldfx = a[x].father; if(oldfx == x){ return x; } else{ a[x].father = getfather(a[x].father); a[x].type = (a[x].type + a[oldfx].type)%3; return a[x].father; }}void unionset(int r,int x,int y){ int fx = getfather(x),fy=getfather(y); if(fx == fy){ return ; } else{ a[fy].father = fx; //如果原来的y吃自己祖先,x吃自己祖先,现在fy归类在fx下面,就要和x一致,所以+a[x].type //-a[y].type是为了获得自己,即fy的状态 //r反映的是x 与 y之间的关系,现在就要反过来,所以要-(r-1) a[fy].type = (1 - r + a[x].type - a[y].type + 6) % 3;//fy下面的状态在getfather函数内进行转换 }}bool istrue(int relation,int x,int y){ if(x > n || y > n || ( (x == y) && (relation==2)) ){ return false; } else{ if(getfather(x)!=getfather(y)){ return true; } else{ if(a[x].type == ((relation - 1 + a[y].type)%3) ){ return true; } else{ return false; } } }}int main(){ int relation,x,y,lies=0; scanf("%d%d",&n,&k); for(int i=1;i<=n;++i){ a[i].father = i; a[i].type = 0; } while(k--){ scanf("%d%d%d",&relation,&x,&y); if(!istrue(relation,x,y)){ ++lies; } else{ unionset(relation,x,y); } } printf("%d\n",lies); return 0;}
0 0
- poj1182 带权并查集
- poj1182(带权并查集)
- poj1182食物链(带权并查集)
- POJ1182带权并查集
- 带权并查集 poj1182
- poj1182(食物链)----带权并查集
- POJ1182-食物链(带权并查集)
- poj1182 食物链【带权并查集】
- POJ1182 食物链 带权并查集
- POJ1182 食物链 (带权并查集)
- poj1182 食物链 (带权并查集)
- 带权并查集模板(poj1182食物链验证)
- poj1182食物链(带权并查集+路径压缩)
- POJ1182——带权并查集
- 食物链 POJ1182(带权并查集)
- POJ1182 食物链(带权并查集,非常经典)
- 【带权并查集】POJ1182 [NOI2001]食物链
- poj1182 食物链 带权并查集 偏移量
- LCD驱动调试 注意事项
- GA谷歌分析 如何设置网络跟踪
- 如何组织帐户、媒体资源和视图 GA谷歌分析技巧
- Javascript的调试利器:Firebug使用详解
- 如何设置跟踪代码 GA谷歌分析技巧
- poj1182 带权并查集
- 账户、媒体资源、视图简介 GA谷歌分析
- Displaying status line always
- 环境不同,心情也许就不同
- byte[]转换为Drawable
- xampp下遇到的问题
- poj1231 模拟?简单几何?
- Java在线编译
- view的属性