文章分类 -  并查集

摘要:hdu1856: http://acm.hdu.edu.cn/showproblem.php?pid=1856题意:输入n对朋友,求最多的朋友集合(直接或间接为朋友)解法:并查集:集合的合并及查询。code:#include<iostream>#include<cstdio>#include<cstdlib>const int maxn=10000002;int fa[maxn],ans[maxn];int find(int x) //路径压缩,复杂度为常数{ int fx=fa[x]; if(x!=fx)fa[x]=find(fx); return fa[ 阅读全文

posted @ 2012-07-25 20:54 acmer-jun 阅读(197) 评论(0) 推荐(0)

摘要:hdu1232: http://acm.hdu.edu.cn/showproblem.php?pid=1232题意:给几条边(可能重复给),求至少还需几条边能使各点直接或者间接联系解法:并查集:把已知的有联系的边减掉。code:#include<iostream>#include<cstdio>#include<cstdlib>int fa[1005],n,m;void solve(int a,int b){ for(int i=1;i<=n;i++) if(fa[i]==a) fa[i]=b;}int main(){ int ans,a,b; ... 阅读全文

posted @ 2012-07-25 17:17 acmer-jun 阅读(100) 评论(0) 推荐(0)

导航