![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
并查集
文章平均质量分 68
无敌大饺子
这个作者很懒,什么都没留下…
展开
-
UVA 10227 - Forests
并查集的应用 用map保存每个人所听到的声音,如果两个人听到的树完全相等,归到同一个类中 注意没有听到任何声音的人也算是一种opnion #include #include #include #include using namespace std; #define MAX 101 int n,m,id[MAX],size[MAX],cnt; map >g; int find(原创 2013-03-15 16:00:22 · 814 阅读 · 0 评论 -
ZOJ 2334 Monkey King
学了左偏树. 这题用左偏树来合并集合,并查集判是否在同一集合,无敌组合. 以下内容转自: http://www.cppblog.com/guogangj/archive/2009/10/30/99833.html 树这个数据结构内容真的很多,上一节所讲的二叉堆,其实就是一颗二叉树,这次讲的左偏树(又叫“左翼堆”),也是树。 二叉堆是个很不错的数据结构,因为它非常便于理解,而且仅原创 2013-05-16 11:25:16 · 762 阅读 · 0 评论 -
ZOJ 1141 Closest Common Ancestors(LCA)
题目n的范围没说,格式坑人 再次膜拜tarjan #include #include #include #include #include using namespace std; const int maxn=900; int res[maxn],id[maxn],size[maxn],anc[maxn],vis[maxn],ind[maxn],n,q; vectorg[ma原创 2013-04-21 21:38:33 · 437 阅读 · 0 评论 -
HDU 1304 find the most comfortable road
并查集和枚举 #include #include #include #include using namespace std; const int maxn=1010; struct edge { int u,v,w; bool operator<(const edge &rhs)const{ return w<rhs.w; } }e[maxn]; int id[maxn],s原创 2013-04-11 10:07:58 · 585 阅读 · 0 评论 -
HDU 1811 Rank of Tetris(拓扑排序+并查集)
如果A==B把A和B归到同一个集合中,然后把这个集合当作一个单位顶点. 然后进行拓扑排序 #include #include #include #include #include using namespace std; const int maxn=10010; struct edge { int v,next; }e[maxn*2]; int head[maxn],ind[ma原创 2013-04-11 12:31:04 · 446 阅读 · 0 评论 -
HDU 1272 小希的迷宫
并查集判断连通和环路,注意输入如果是只有0 0 要输出yes 代码有点臭 #include #include using namespace std; const int maxn=100010; int id[maxn],size[maxn],n; bool in[maxn]; int find(int p){ return p==id[p]?p:id[p]=find(id原创 2013-04-09 14:14:28 · 447 阅读 · 0 评论 -
POJ 2492 A Bug's Life
并查集操作,用一个oid数组来存放虫子的对立面 如果两只虫子a,b的祖先相同那就是同性恋了. 每次合并a,b把a和b的对立面合并,把b和a的对立面合并 #include #include using namespace std; const int maxn=2010; int id[maxn],oid[maxn],size[maxn],n,m; void clear(){ for原创 2013-04-06 17:48:50 · 408 阅读 · 0 评论 -
POJ 2236 Wireless Network
普通的并查集操作 #include #include #include #include using namespace std; const int maxn=1010; int id[maxn],size[maxn],x[maxn],y[maxn],n,d; vectorg[maxn]; bool isRepaired[maxn]; double dis(int x1,int y1原创 2013-04-06 16:53:16 · 390 阅读 · 0 评论 -
POJ 1703 Find them, Catch them
并查集操作 查: 1.如果两人的祖先相等,则两人属于同一个帮派 2.如果一人的祖先等于对方祖先的敌对方,则属于不同的帮派 3.否则就是不确定 并: 1.如果某人的敌对方还没有,就把对方的祖先作为自己的敌对方 2.将自己和对方祖先的敌对方合并 #include #include using namespace std; const int maxn=100010; int id[原创 2013-04-06 11:43:57 · 415 阅读 · 0 评论 -
并查集详解 (转)
并查集是我暑假从高手那里学到的一招,觉得真是太精妙的设计了。以前我无法解决的一类问题竟然可以用如此简单高效的方法搞定。不分享出来真是对不起party了。(party:我靠,关我嘛事啊?我跟你很熟么?) 来看一个实例,杭电1232畅通工程 首先在地图上给你若干个城镇,这些城镇都可以看作点,然后告诉你哪些对城镇之间是有道路直接相连的。最后要解决的是整幅图的连通性问题。比如随意给你两个转载 2013-04-06 14:32:07 · 395 阅读 · 0 评论 -
POJ 1988 Cube Stacking
可以用并查集来做,不进行路径压缩的话直接TLE 用一个cnt数组来表示在X下面的方块个数,每次count的时候进行路径压缩而不是在find时候压缩: cnt[X]=cnt[id[X];//将X到其父亲之间的方块个数加上父亲到爷爷之间的方块个数 id[X]=id[id[x]];//然后把X当前的父亲改成爷爷 在find的时候按照路径找上去 #include #include usi原创 2013-04-06 14:16:23 · 439 阅读 · 0 评论 -
UVA 11228 - Transportation system.
MST,一条边长度大于t那么就连着两个州,州的数量等于 长度大于t的边的数量+1 #include #include #include #include #include #include using namespace std; struct edge { int u,v; double w; edge(int uu=0,int vv=0,double ww=0):u(uu原创 2013-03-30 09:54:46 · 1102 阅读 · 0 评论 -
ZOJ 2833 Friendship
裸并查集操作. #include int id[100001], size[100001], n, m; void readInt(int * x){ char ch; *x = 0; ch = getchar(); while(!(ch >= '0' && ch <= '9'))ch = getchar(); while(ch >= '0' && ch <= '9'){ *x原创 2013-05-16 11:59:51 · 570 阅读 · 0 评论