Hdu1498 -- 50 years, 50 colors (Binary match, meaning ...)

Source: Internet
Author: User
50 years, 50 colors Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1617 accepted submission (s): 881


Problem descriptionon octorber 21st, HDU 50-Year-celebration, 50-color balloons floating around the campus, it's so nice, isn' t it? To celebrate this meaningful day, the ACM team of HDU hold some fuuny games. Especially, there will be a game named "crashing color balloons ".

There will be a n * n matrix board on the ground, and each grid will have a color balloon in it. and the color of the Ballon will be in the range of [1, 50]. after the referee shouts "Go! ", You can begin to crash the balloons. every time you can only choose one kind of balloon to crash, we define that the two balloons with the same color belong to the same kind. what's more, each time you can only choose a single row or column of balloon, and crash the balloons that with the color you had chosen. of course, a lot of students are waiting to play this game, so we just give every student K times to crash the balloons.

Here comes the problem: Which kind of balloon is impossible to be all crashed by a student in K times.


 
Inputthere will be multiple input cases. each test case begins with two integers n, k. n is the number of rows and columns of the balloons (1 <= n <= 100), and K is the times that ginving to each student (0 <k <= N ). follow a matrix A of N * n, where AIJ denote the color of the ballon in the I row, J column. input ends with N = k = 0.
 
Outputfor each test case, print in ascending order all the colors of which are impossible to be crashed by a student in K times. If there is no choice, print "-1 ".
 
Sample Input
1 112 11 11 22 11 22 25 41 2 3 4 52 3 4 5 13 4 5 1 24 5 1 2 35 1 2 3 43 350 50 5050 50 5050 50 500 0
 
Sample output
-1121 2 3 4 5-1
 

The meaning of the question is given a matrix of N * n. The color of a balloon is different on each vertex. K times can be used for each color, you can drop a color ball of the same color in a row or column each time. If it can be wiped out, it will not be output; otherwise, the color of the output balloon will be wiped out.

It is to perform a binary match for each color and determine whether it can be eliminated within K times.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int mm[120][120] , c[100] ;struct node{    int v ;    node *next ;} *head[120];int link[120] , temp[120] , top , p[120];int f(int i){    for( node *q = head[i] ; q != NULL ; q = q->next )    {        int v = q->v ;        if( !temp[v] )        {            temp[v] = 1 ;            if( link[v] == -1 || f( link[v] ) )            {                link[v] = i ;                return 1;            }        }    }    return 0;}int main(){    int i , j , k , n , m , ans  ;    while(scanf("%d %d", &n, &m) && n+m != 0)    {        memset(c,0,sizeof(c));        for(i = 1 ; i <= n ; i++)            for(j = 1 ; j <= n ; j++)            {                scanf("%d", &mm[i][j]);                c[ mm[i][j] ]++ ;            }        top = 0 ;        for(i = 1 ; i <= 50 ; i++)        {            ans = 0 ;            memset(head,NULL,sizeof(head));            memset(link,-1,sizeof(link));            if( !c[i] )                continue ;            for(j = 1 ; j <= n ; j++)                for(k = 1 ; k <= n ; k++)                {                    if( mm[j][k] == i )                    {                        node *q = new node ;                        q->v = k ;                        q->next = head[j] ;                        head[j] = q ;                    }                }            for(j = 1 ; j <= n ; j++)            {                memset(temp,0,sizeof(temp));                if(  f(j) )                    ans++ ;            }            if( ans > m )                p[top++] = i ;        }        if(top == 0)            printf("-1\n");        else        {            for(i = 0 ; i < top ; i++)                if(i == top-1)                    printf("%d\n", p[i]);                else                    printf("%d ", p[i]);        }    }    return 0;}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.