zoukankan      html  css  js  c++  java
  • HDU2094 产生冠军

        题中先用并查集判定是否所有点都有联系,即能够拼成一个连通的无向图。 再判定入度为零的点是否为1即可。

    代码如下:

    #include <stdio.h>
    #include <string.h>
    
    char name[2010][50];
    
    int cnt, N, dg[2010], hash[2010], set[2010];
    
    int find( char *n )
    {
    	int i;
    	for( i= 0; i< cnt; ++i )
    	{
    		if( strcmp( n, name[i] )== 0 )
    		{
    			return i;
    		}
    	}
    	strcpy( name[cnt], n );
    	return cnt++;    // 在return的时候把cnt自增
    }
    
    int find( int x )
    {
        return set[x]= x== set[x]? x: find( set[x] );
    }
    
    void merge( int x, int y )
    {
        int a= find( x ), b= find( y );
        set[a]= b;
    }
    
    int main(  )
    {
    	while( scanf( "%d", &N ), N )
    	{
    		char n1[50], n2[50];
    		cnt= 0;
    		int edge= 0;
    		memset( dg, 0, sizeof( dg ) );
    		memset( hash, 0 ,sizeof( hash ) );
    		for( int i= 0; i< 2010; ++i )
    		{
    		    set[i]= i;
    		}
    		for( int i= 1; i<= N; ++i )
    		{
    			scanf( "%s %s", n1, n2 );
    			int a= find( n1 ), b= find( n2 );
    			if( find( a )!= find( b ) )
    			{
    			    merge( a, b );
    			    edge++;
    			} 
    			dg[b]++;
    		}
    		if( edge!= cnt- 1 )
    		{
    		    printf( "No\n" );
    		    continue;
    		}
    		int shit= 0;
    		for( int i= 0; i< cnt; ++i )
    		{
    		    if( dg[i]== 0 )
    		    {
    		        shit++;
    		    }
    		}
    		printf( shit== 1? "Yes\n": "No\n" );
    	}
    }
    
     
    
  • 相关阅读:
    NTFS FAT FAT32
    天才经常浏览的15个网站
    手机软件测试总结
    常见文件格式总结
    Tcp三次握手
    Http请求响应机制
    C/S测试
    软件异常测试
    跟我一起学Oracle 11g【8】SQL 基础学习2[连接查询]
    跟我一起学Oracle 11g【7】SQL 基础学习
  • 原文地址:https://www.cnblogs.com/Lyush/p/2108593.html
Copyright © 2011-2022 走看看