随笔 - 87  文章 - 279  trackbacks - 0
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

潜心看书研究!

常用链接

留言簿(19)

随笔分类(81)

文章分类(89)

相册

ACM OJ

My friends

搜索

  •  

积分与排名

  • 积分 - 214753
  • 排名 - 116

最新评论

阅读排行榜

评论排行榜

Girls and Boys

Time limit: 10 Seconds   Memory limit: 32768K  
Total Submit: 628   Accepted Submit: 188  

the second year of the university somebody started a study on the romantic relations between the students. The relation “romantically involved” is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been “romantically involved”. The result of the program is the number of students in such a set.

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

the number of students
the description of each student, in the following format
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
or
student_identifier:(0)

The student_identifier is an integer number between 0 and n-1, for n subjects.
For each given data set, the program should write to standard output a line containing the result.

An example is given in Figure 1.


Input

7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0


Output

5
2

#include  < iostream >  
using   namespace  std;

const   int  MAXN  =   1001 ;
int  uN, vN;   // u,v数目 
bool  g[MAXN][MAXN]; // g[i][j] 表示 xi与yj相连 
bool  p[MAXN][MAXN];
int  xM[MAXN], yM[MAXN];  //  输出量 
bool  chk[MAXN];  // 辅助量 检查某轮 y[v]是否被check 

int  sign[MAXN];
int  N;

bool  SearchPath( int  u)
{
    
int  v;
    
for  (v = 0 ; v < vN; v ++ )
    
{
        
if  (g[u][v]  &&   ! chk[v])
        
{
            chk[v] 
=   true ;
            
if  (yM[v]  ==   - 1   ||  SearchPath(yM[v])) 
            
{
                yM[v] 
=  u;
                xM[u] 
=  v;
                
return   true ;
            }

        }

    }

    
return   false ;
}



int  MaxMatch()
{
    
int  u;
    
int  ret  =   0 ;
    memset(xM, 
- 1 sizeof (xM));
    memset(yM, 
- 1 sizeof (yM));
    
for  (u = 0 ; u < uN; u ++ )
    
{
        
if  (xM[u]  ==   - 1 )
        
{
            memset(chk, 
false sizeof (chk));
            
if  (SearchPath(u)) ret ++ ;
        }

    }

    
return  ret;
}


void  SetSign( int  v,  int  s)
{
    
int  i;
    sign[v] 
=  s;
    
for  (i = 0 ; i < N; i ++ )
        
if  (sign[i]  ==   - 1   &&  p[v][i])
            SetSign(i, 
1 - s);     
}


void  Solve()
{
    
int  i, j; 
    
int  tU, tV;
    
int  num;
    memset(g, 
false sizeof (g));
    memset(p, 
false sizeof (p));
    memset(sign, 
- 1 sizeof (sign));
    
for  (i = 0 ; i < N; i ++ )
    
{
        scanf(
" \n%d: (%d) " & tU,  & num);
        
for  (j = 0 ; j < num; j ++ )
        
{
            scanf(
" %d " & tV);
            p[tU][tV] 
=   true ;
        }

    }
 
    
    
// -------------DFS标号法(划分二分图)-------------------- 
     /* ******************************************
    邻接表的DFS标号:
    void setmark(int v,int sign)
    {
        sig[v]=sign;
        int i;
        for (i=0;i<nu[v];i++)
            if (!sig[d[v][i]])
                setmark(d[v][i],sign^3);
    } 
    for (v=0;v<n;v++)
        if (!sig[v]) setmark(v,1);
    *******************************************
*/

    
for  (i = 0 ; i < N; i ++ )
        
if  (sign[i]  ==   - 1 ) SetSign(i,  1 );
    
// ------------------------------------------    
        
    
for  (i = 0 ; i < N; i ++ )
    
{
        
if  (sign[i]  ==   1 )
        
{
            
for  (j = 0 ; j < N; j ++ )
                
if  (p[i][j]) g[i][j]  =   true ;
        }

    }
            
    uN 
=  vN  =  N;
    printf(
" %d\n " , N - MaxMatch()); 
}
 

int  main()
{
    
while  (scanf( " %d " & N)  !=  EOF)
    
{
        Solve();
    }

    
return   0 ;
}

posted on 2006-10-01 22:53 阅读(569) 评论(0)  编辑 收藏 引用 所属分类: ACM题目

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理