algorithm - bipartite matching in C++, what is wrong in my code? -


i have worked on bipartite matching problem, , got trouble solve it.

let me tell problem about.

as input form, gives me n, m number of people @ work , number of different kind of jobs in work. in following n lines, tells s tells how many kind of job ith person can do. in following s numbers, tell jobs can ith person can do. number of jobs si satisfy 1<=si<=m. then, if each person can work on 1 or less job @ time, how many jobs can done @ most?

this problem, , hope made sense you;) apologize poor english skill. returning point, code.

#include <stdio.h>  int n, m, dfscheck[1003], check[1003], input[1003][1003], back[1003], tmp, count=0;  void dfs(int num) {     int i, j;     if(check[num]==0)     {         tmp=-1;         check[num]=1;         return;     }     if(dfscheck[num]==1)         return;     dfscheck[num]=1;     for(j=1; j<=input[back[num]][0]; j++)     {         dfs(input[back[num]][j]);         if(tmp==-1)         {             back[input[back[num]][j]]=back[num];             return;         }     } }  int main(void) {     int i, j, flag ,k;     scanf("%d %d", &n, &m);     for(i=1; i<=n; i++)     {         scanf("%d", &input[i][0]);         for(j=1; j<=input[i][0]; j++)             scanf("%d", &input[i][j]);     }     for(i=1; i<=n; i++)     {         flag=0;         for(j=1; j<=input[i][0]; j++)             if(check[input[i][j]]==0)             {                 check[input[i][j]]=1;                 count++;                 flag=1;                 back[input[i][j]]=i;                 break;             }          if(flag==0)             for(j=1; j<=input[i][0]; j++)             {                 for(k=0; k<=1001; k++)                     dfscheck[k]=0;                 tmp=0;                 dfs(input[i][j]);                 if(tmp==-1)                 {                     back[input[i][j]]=i;                     count++;                 }             }     }     printf("%d", count);     return 0; } 

the name of tmp bad understand code, so... tmp works flag in function dfs. tells whether has found matching.

i got wrong answer, neither runtime error nor time exceed. have read several different codes others have written, , couldn't find part wrong in code.

i have found code distinct others' in part don't define array, a[i], showing job matched ith person, opposite array in code. far understood codes, purpose of find out ith person has been matched before dfs. don't think case can happened. since no flow has come out of node of ith person, impossible flow go node of jobs node in max flow.

i wanted thoughts if useless, in case helps give me advices. anyway, let me have opinions!!! thank time.

one thing looks weird is:

            if(tmp==-1)             {                 back[input[i][j]]=i;                 count++;                 // expect see "break;" here             } 

if find way assign person job increment count, , carry on trying see if can find alternative way assign same person. means may end assigning same person multiple jobs!

i recommend inserting "break;" statement once have found job person.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -