LeeBlog

导航

随笔分类 -  并查集

HDU 1879 继续畅通工程
摘要:跟畅通工程差不多,我的代码就在那上面改的#include<stdio.h>#include<stdlib.h>struct e{ int x,y,v;}val[10500];int n,m,set[105],sum;int cmp( const void *a,const void *b ){ return ( ( e * )a ) -> v - ( ( e * )b ) -> v;}int find( int i ){ return i == set[i] ? i :set[i] = find( set[i] );}void Kustra( ){ for( 阅读全文

posted @ 2011-04-30 09:02 LeeBlog 阅读(158) 评论(0) 推荐(0)

HDU 1272 小希的迷宫
摘要:这题数据真是恶心,开始敲出来了,一直WA,后来重敲一遍还是WA,最后看小白的代码才知道,还有( 0 0 ) 这种恶心的数据#include<stdio.h>#include<string.h>int set[100005],des[100005],f;int find( int x ){ return set[x] == x ? x : set[x] = find( set[x] );}void merge( int x,int y ){ int a = find( x ),b = find( y ); if( a != b ) set[a] = b; else if( 阅读全文

posted @ 2011-04-29 22:34 LeeBlog 阅读(207) 评论(0) 推荐(0)

hdu 1856 More is better
摘要:这题分为四个步骤 压缩,合并完全,压缩查找最大集合include<stdio.h>int set[10000000],count[10000000];int find( int x ){ return set[ x ] == x ? x : set[ x ] = find( set[ x ] );} void merge( int x,int y ){ int a = find( x ),b = find( y ); set[ a ] = b; }int main( ){ int n,i,a,b; while( scanf( "%d",&n ) !=EO 阅读全文

posted @ 2011-02-26 09:55 LeeBlog 阅读(160) 评论(0) 推荐(0)

hdu 1213 How Many Tables
摘要:并查集基础#include<stdio.h>int set[100000];int find( int i ){ return set[ i ] == i ? i : set[ i ] = find( set[ i ] );//路径压缩}//查找跟结点void merge( int x,int y ){ int a = find( x ), b = find( y ); if( a != b ) set[ a ] = b;//合并 }int main( ){ int T,n,m,a,b; scanf( "%d",&T ); while( T-- ) { 阅读全文

posted @ 2011-02-26 09:48 LeeBlog 阅读(141) 评论(0) 推荐(0)

hdu 1232 畅通工程
摘要:这题就是把所有的村庄看它们是否畅通来分成n个集合,然后只要修建 n - 1 条道路就可以把他们连起来了#include<stdio.h>int set[ 1024 ];int find( int i ){ return set[ i ] == i ? i : set[ i ] = find( set[ i ] );}void merge( int x, int y ){ int a = find( x ),b = find( y ); set[ a ] = b; }int main( ){ int n,m,a,b; while( scanf( "%d",& 阅读全文

posted @ 2011-02-26 09:39 LeeBlog 阅读(167) 评论(0) 推荐(0)