orz,我真的不知道哪里出错了,,,今天状态不是很好,就这一道题都没做完 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; char id[ 20 ][ 20 ]; char name[ 20 ][ 20 ]; bool visit[ 20 ]; int link_name[ 20 ]; bool graph[ 20 ][ 20 ]; int order[ 20 ]; int match_id[ 20 ]; int n ; int inHideout[ 20 ]; bool cmp( int i , int j ){ if (memcmp(name[i],name[j],sizeof name[i]) <= 0) return 1; else return 0; } //加深为匈牙利求最大二分 int dfs( int k ){ // k 是左边点集 遍历右边点集i for( int i = 0 ; i < n ; i ++ ){ if( graph[ k ][ i ] && !visit[ i ] ){ visit[ i ] = true; if( link_name[ i ] == -1 || dfs( link_name[ i ] ) ){ link_name[ i ] = k; return 1; } } } return 0; } int hungry( ){ int res = 0; memset( link_name , -1 , sizeof( link_name )); for( int i = 0 ; i < n ; i ++ ){ memset( visit , 0 , sizeof( visit )); res += dfs( i ); } return res; } int main(){ // freopen( "in.txt" , "r" , stdin ); scanf("%d" , &n ); for( int i = 0 ; i < n ; i ++ )scanf( "%s", id[ i ] ); int nameCnt = 0; memset( graph , true , sizeof( graph )); memset( match_id , -1 , sizeof( match_id )); memset( inHideout , 0 , sizeof( inHideout )); while( true ){ char type; cin>>type; if( type == 'Q')break; char tmpName[ 20 ]; cin>>tmpName; nameCnt++; int i; switch (type){ case 'E':{ for( i = 0 ; i < nameCnt && strcmp( name[ i ] , tmpName ) != 0 ; i ++ ); if( i == nameCnt )strcpy( name[ nameCnt++ ] , tmpName ); inHideout[ i ] = 1 ; break; } case 'L':{ for( i = 0 ; i < nameCnt && strcmp( name[ i ] , tmpName ) != 0 ; i ++ ); inHideout[ i ] = 0; break; } case 'M':{ for( i = 0 ; i < n && strcmp( id[ i ] , tmpName ) != 0 ; i ++ ) for( int j = 0 ; j < n ; j ++ ){ if( !inHideout[ j ])graph[ j ][ i ] = false; } break; } } } for( int i = 0 ; i < n ; i ++ ){ for( int j = 0 ; j < n ; j ++ ){ if( graph[ i ][ j ] ){ graph[ i ][ j ] = false; if( hungry() != n ){ graph[ i ][ j ] = true; match_id[ i ] = j; break; } graph[ i ][ j ] = true; } } } for( int i = 0 ; i < n ; i ++ )order[ i ] = i ; sort( order , order + n , cmp ); for( int i = 0 ; i < n ; i ++ ){ printf( "%s" , name[ order[ i ] ]); printf(":"); if( match_id[ order[ i ] ] != -1 )printf("%s" , id[ match_id[ order[ i ] ]]); else printf("???"); printf("\n"); } return 0; }
1043 What's In A Name?
最新推荐文章于 2020-12-16 10:41:22 发布