博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1829 并查集up
阅读量:4709 次
发布时间:2019-06-10

本文共 1391 字,大约阅读时间需要 4 分钟。

/*

        说到底,还是并查集;

        当然这题比较特殊;

*/

一般的并查集应该这么写:

for(int i = 1; i <= m; i++) //########		{			scanf("%d%d",&x,&y);			Union(x,y);  //合并		}                         //########
一般并查集合并的是 两个有关系的;

题目输入的是x y  

如果x  y 的根都想等,说明他们是同性恋,

如果不相等,则需要合并,

  需要合并的是同性的;sex[x]  存储的是:x  的性别相反的 符号;

#include 
#include
#include
#include
#include
#include
#include
#define maxn 2005int father[maxn],sex[maxn]; using namespace std;int find(int x) //查找根{ if(father[x]==x) return x; else return father[x] = find(father[x]);}void Union(int x, int y) //合并{ int f1 = find(x); int f2 = find(y); if(f1 != f2) father[f1] = f2;}int main(int argc, char *argv[]){ int T,n,m,x,y,Case = 1; scanf("%d",&T); while(T--) { int flag = 0; scanf("%d%d",&n,&m); memset(sex,0,sizeof(sex));//***** for(int i = 1; i <= n; i++) father[i] = i; for(int i = 1; i <= m; i++) { scanf("%d%d",&x,&y); if(flag) continue; if(find(x)==find(y)) //########## { flag = 1; } else { if(sex[x] == 0) sex[x] = y; else Union(sex[x],y); if(sex[y] == 0) sex[y] = x; else Union(sex[y],x); } //########### } if(!flag) { printf("Scenario #%d:\n",Case++); puts("No suspicious bugs found!"); printf("\n"); } else { printf("Scenario #%d:\n",Case++); puts("Suspicious bugs found!"); printf("\n"); } } return 0;}

转载于:https://www.cnblogs.com/i-fuqiang/archive/2013/04/18/3189482.html

你可能感兴趣的文章
注册用户
查看>>
20145302张薇 20145308刘昊阳 《信息安全系统设计基础》实验三 实时系统的移植
查看>>
xCode中怎样保存自己的代码块
查看>>
AABB包围盒、OBB包围盒、包围球的比較
查看>>
Jquery获取select选中的option的文本信息
查看>>
XP的定时关机命令?
查看>>
Android开发技术周报 Issue#22
查看>>
201521123023《Java程序设计》第6周学习总结
查看>>
会话状态在此上下文中不可用HttpModule中无法访问Session原因
查看>>
从大一到大四英语怎么说
查看>>
某地小区,疑因喷泉漏电,殃及三个小孩子被电死
查看>>
HTML--使用下拉列表框,节省空间
查看>>
vue案例todolist备忘录
查看>>
mybatis缓存
查看>>
Eclipse下启动安卓应用错误:Failed to initialize Monitor Thread: Unable to establish loopback connection...
查看>>
第五章:基础构建模块——java并发编程实战
查看>>
Ubuntu Mysql开通外网访问权限
查看>>
javascript判断是否按回车键
查看>>
201671010135 《面向对象程序设计课程学习进度条》
查看>>
input 框改变 placeholder里面的字体颜色 (控制placeholder里面的元素) 去掉点击select出现的边框...
查看>>