
并查集
synapse7
这个作者很懒,什么都没留下…
展开
-
POJ 3522 Slim Span(MST)
Slim Spanhttp://poj.org/problem?id=3522Time Limit: 5000MSMemory Limit: 65536KDescriptionGiven an undirected weighted graph G, you should find one of spanning trees specified as fol原创 2013-08-07 11:21:14 · 1003 阅读 · 0 评论 -
LightOJ 1009 Back to Underworld (种类并查集)
http://lightoj.com/volume_showproblem.php?problem=1009种类并查集实现(当然用二分图染色也可以)/*0.204s,2820KB*/#includeusing namespace std;const int mx = 20000;int fa[mx * 2 + 5], rk[mx * 2 + 5], x[100005原创 2014-03-24 16:35:47 · 1730 阅读 · 0 评论 -
POJ 1182 / Noi 01 食物链 (并查集&代码优化)
http://poj.org/problem?id=1182/*235ms,776KB*/#includeconst int mx = 50000 + 5;const int mxadd = 3 * 50000;int fa[mx], rk[mx];int find(int x){ if (x != fa[x]) { int tmp = fa[x]; fa原创 2014-01-19 21:36:49 · 1579 阅读 · 0 评论 -
UVa 11987 Almost Union-Find (加权并查集&删除结点的技巧)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3138如何删除?增加一个新结点,标号为所有结点(包括新结点)的最大编号+1然后将旧结点的影响变为0,同时初始化新结点:void delet(int x){ int tx = find(原创 2014-03-07 14:23:50 · 977 阅读 · 0 评论 -
UVa 1329 Corporative Network (加权并查集)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=497&page=show_problem&problem=4075由于维护的是树上某一点到根节点的距离,所以可以在find函数中添加一pushdown函数以便更新某点到根的距离。完整代码:/*0.062s*/#原创 2014-03-06 08:30:41 · 950 阅读 · 0 评论 -
UVa 1160 X-Plosives (并查集)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=497&page=show_problem&problem=3601要安全,这个“图”至多是一颗树(n个点,n-1条边),所以每次判断新加入的两个元素a,b是否都在树上,若是,则加入后树变图(n个点,n条边),此时应拒绝,否则合并a原创 2014-03-04 12:51:32 · 847 阅读 · 0 评论 -
并查集(union-find)模板
#includeusing namespace std;const int mx = 100005;int fa[mx], rk[mx];int find(int x) {return fa[x] = (fa[x] == x ? x : find(fa[x]));}void union(int x, int y){ x = find(x), y = find(y); if (原创 2014-01-13 08:43:49 · 1173 阅读 · 0 评论 -
UVa 10608 Friends (求并查集中最大的集合的基数)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1549思路:先合并,再遍历每个元素,对其父亲元素计数+1for (i = 1; i <= n; ++i) maxcnt = max(maxcnt, ++cnt[f原创 2014-02-16 18:43:52 · 1006 阅读 · 0 评论 -
POJ 1703 Find them, Catch them (并查集&利用异或的性质优化)
http://poj.org/problem?id=1703思路和食物链那题一样的。完整代码:/*297ms,1156KB*/#include#includeint fa[100005], rk[100005];int find(int x){ if (fa[x]) { int tmp = fa[x]; fa[x] = find(fa[x])原创 2014-01-20 10:49:22 · 1574 阅读 · 0 评论 -
POJ 2236 Wireless Network (并查集&用set进行优化)
http://poj.org/problem?id=2236思路:用set保存已修好的电脑(使用set是为了去重),每次修电脑p时就把set中的与p距离完整代码:/*766ms,640KB*/#include#includeusing namespace std;int x[1005], y[1005];int fa[1005], rk[1005];原创 2014-01-20 10:54:06 · 1260 阅读 · 0 评论 -
POJ 2524 Ubiquitous Religions (并查集)
Ubiquitous ReligionsTime Limit: 5000MSMemory Limit: 65536KDescriptionThere are so many different religions in the world today that it is difficult to keep track of them all. You are interest原创 2013-07-25 11:34:56 · 1421 阅读 · 0 评论 -
WuHan 2009 / UVa 12232 / HDU 3234 Exclusive-OR (异或的性质&加权并查集&合并时保持根结点不变)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=33841. 对于第一种命令I p v,我们可以虚拟出一个点Xn = 0,那么p^Xn = v,故第一和第二种命令我们可以统一成p^q = v的模式。2. 记val[i]原创 2014-03-07 16:22:41 · 1272 阅读 · 0 评论