LeeBlog

导航

2011年2月26日 #

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 阅读(154) 评论(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 阅读(133) 评论(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 阅读(165) 评论(0) 推荐(0) 编辑

hdu 1863 畅通工程

摘要: 这题是刚学最小生成树的一入门题,其实就是求最小生成树,用结构体定义这棵树,到时候便于权值排序,输入后进行排序,让后再讲这些顶点连成一个集合,并在这过程中把各权值相加(因为这些权值已按递增排列,所以得出的集合时权值最小的),至于是否能保证畅通则只要看有几个根结点就可以了#include<stdio.h>#include<stdlib.h>#include<string.h>int set[1000],n,m,x,y,val;struct Ed{ int x,y,val; }e[10000];int cmp( const void *a,const void * 阅读全文

posted @ 2011-02-26 09:34 LeeBlog 阅读(212) 评论(0) 推荐(0) 编辑