上一页 1 ··· 26 27 28 29 30 31 32 33 34 ··· 61 下一页
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4263思路:将红边和蓝边单独求一次生成树,求的红边最多可以加入的边数cntr,蓝边最多可以加入的边数cntb,只要k满足条件k>=(n-1-cntr)&&k<=cntb,就说明可以生成这样的spanning tree. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 阅读全文
posted @ 2013-06-10 16:36 ihge2k 阅读(286) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818思路:Count[i]表示i下面的积木个数,路径压缩的时候更新一下即可,sum[i]表示以i为根的积木的个数。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define MAXN 30000+30 7 int parent[MAXN]; 8 int sum[MAXN],Coun 阅读全文
posted @ 2013-06-10 11:07 ihge2k 阅读(373) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3047思路:冲突的条件是:两个人坐在同一行,同时他们到根节点的差值等于他们之间的差值,这时就产生冲突了。于是我们可以用一个dist数组来保存节点到根的距离,而这个距离在路径压缩的时候更新一下就可以了,dist[x]+=dist[parent[x]]。然后就是合并后的距离,令r1=Find(u),r2=Find(v),于是合并时就有parent[r2]=r1,dist[r2]=dist[u]+w-dist[v]。盗用一张图: 1 #include<iostream> 2 #include& 阅读全文
posted @ 2013-06-10 09:55 ihge2k 阅读(1952) 评论(0) 推荐(1) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558思路:主要是判断线段相交,如果相交,就是要合并了,并且修改一下sum值就可以了。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 #define MAXN 1111 8 #define eps 1e-7 9 int parent[MAX 阅读全文
posted @ 2013-06-10 00:01 ihge2k 阅读(258) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2144思路:合并的过程中多了一个条件,而这个条件的判断可以通过求最长公共连续子序列得到,别的和普通的并查集没什么区别,最后就是简单地判断一下集合的个数即可。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define MAXN 111 7 char str[MAXN][MAXN]; 8 阅读全文
posted @ 2013-06-09 16:30 ihge2k 阅读(222) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2860思路:多了一个记录每个集合最小值的value数组而已,然后判断的时候小心一点就可以了。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define MAXN 100000+100 7 int parent[MAXN]; 8 int value[MAXN]; 9 int n,m,k 阅读全文
posted @ 2013-06-09 15:52 ihge2k 阅读(454) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4126思路:我们可以先求最小生成树,对于最小生成树的每一条边,我们要找到它的最佳替代边,使其价值最小。具体实践方法:假设两个各自连通的部分分别为树A,树B,dp[i][j]表示树A中的点i到树B(点j所在的树的最近距离),这个很容易用dfs实现,然后通过求出的dp[i][j],再用一个dfs求出树B到树A的最近距离(就是枚举树A中的所有点 到 树B的最近距离,取其中的最小值),这个求出来的值其实就是我们要求的最佳替代边,将它保存到一个数组中即可。总的时间复杂度为O(n^2)。 1 #include. 阅读全文
posted @ 2013-06-09 09:20 ihge2k 阅读(690) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3938思路:对于边进行从小到大进行排序,题目要求a、b两点之间所有的路径中的最大边的最小值,在满足边的大小<=len的条件下,令r1=Find(a),r2=Find(b),若r1==r2,此时就是简单的合并,并且更新一下集合的个数(abs(parent[i])即为以i为根结点的集合的大小),若r1!=r2,此时a,b两点之间的路径的条数即为parent[r1]*parent[r2]。显然对于那些边>=len的,都要加上原先的路径数目。 1 #include<iostream> 阅读全文
posted @ 2013-06-08 21:05 ihge2k 阅读(372) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2120思路:简单的并查集判环应用,即如果当前的两个节点在同一个集合中,那么说明存在环了,ans++。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define MAXN 1000+10 7 int parent[MAXN]; 8 int n,m; 9 10 int Find(int 阅读全文
posted @ 2013-06-08 18:49 ihge2k 阅读(321) 评论(0) 推荐(0) 编辑
摘要: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038思路:sum[x]表示区间[x,f[x]]的和,这个可以在路径压缩的时候更新,对于一组数据(u,v,w),令r1=Find(u),r2=Find(v),于是若r1==r2,此时u,v就有了相同的参考点,而sum[u]为区间[u,r1(r2)]的和,sum[v]为区间[v,r2(r1)]的和,于是只需判断w==sum[v]-sum[u]即可;若r1<r2,此时可以分为两种情况,(u,v,r1,r2)或者(u,r1,v,r2),对于情况1来说,此时father[r1]=r2,sum[r1]= 阅读全文
posted @ 2013-06-08 11:07 ihge2k 阅读(620) 评论(0) 推荐(0) 编辑
上一页 1 ··· 26 27 28 29 30 31 32 33 34 ··· 61 下一页