Obviously, the biggest matching question. In addition, the question is SPJ. You can use the Hungary algorithm to calculate the maximum number of matches from row to column.
My code:
Source Code
Problem: 1719 User: bingshen
Memory: 1160 K Time: 16 MS
Language: C ++ Result: Accepted
Source Code
# Include <stdio. h>
# Include <string. h>
Bool map [2, 1005] [2, 1005
Bool used [1005];
Int link [1005];
Int n, m;
Bool dfs (int u)
{
Int I;
For (I = 1; I <= m; I ++)
{
If (map [u] [I] &! Used [I])
{
Used [I] = true;
If (link [I] =-1 | dfs (link [I])
{
Link [I] = u;
Return true;
}
}
}
Return false;
}
Int main ()
{
Int I, j, x, y, t, num;
Scanf ("% d", & t );
While (t --)
{
Num = 0;
Scanf ("% d", & n, & m );
Memset (map, 0, sizeof (map ));
Memset (link,-1, sizeof (link ));
If (n> m)
{
Printf ("NO ");
Continue;
}
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & x, & y );
Map [x] [I] = true;
Map [y] [I] = true;
}
For (I = 1; I <= n; I ++)
{
Memset (used, 0, sizeof (used ));
If (dfs (I ))
Num ++;
}
If (num <n)
{
Printf ("NO ");
Continue;
}
Else
{
For (I = 1; I <= m; I ++)
{
If (link [I]! =-1)
Printf ("% d", link [I]);
Else
{
For (j = 1; j <= n; j ++)
If (map [j] [I])
{
Printf ("% d", j );
Break;
}
}
}
Printf ("");
}
}
Return 0;
}