Hdu 2208 Oh, lovely little friend.

Source: Internet
Author: User

Problem Description

Alas, the children are more troublesome. In a kindergarten, the teacher to a game class, there are N children to play games, do the game with a small ball, but only m small ball in kindergarten, and some children do not like to play with some children, and only like to play with other children, such as silly girl only like and fools, Silly root, silly to play together, Silly root and do not like to play with the silly eggs, silly like to play with fools. So the teacher had to group them, each group at least a small ball can play, and each group will not have two children, mutual dislike. Now give you a description of the relationship between the children in kindergarten, as a teacher, whether you can good game class.

Input

Data has multiple case, each case entered two values N (1<=n<=10) and M (1<=m<=10), indicating that there are N children (from 0 to N-1 label), and M small ball. Then there are N rows, line I first enter a K (0<=k<n), indicating that I have children like to play with the number of other children, and then there is a K-integer, indicating the mark of K children (do not repeat). If a likes to play with B, then B also likes to play with a, this data is guaranteed in the input. There is a blank line between the two case

Output

For each case, if the teacher can do a good lesson, output yes, otherwise no.

Sample Input

3 22 1 22 2 02 0 1

Sample Output

YES


Reference ideas: http://www.cnblogs.com/jackge/p/3228479.html

Code:

#include  <iostream> #include  <string.h>int root[15];int map[15][15];int  Children = 0, balls = 0;bool isok = false;bool dfs (Int x,  int y)     // x Number of Delegates (number: x person), Y represents the number of balls used {    if  (y  > balls)     //  The number of balls used has exceeded budget     {         return false;    }    if  (x  == children)     {        return  true;    }    for  (int i = 0; i <  x; i++)     {        if  (Root[i]  != i)     //  a set that is combined with a root to represent         {             continue;        }         bool flag = true;         for  (int j = 0; j < x; j++)          {            if  (Root[j]  == i)             {                 if  (map[x][j] !=  1)                 {                      flag = false;                     break;                 }            }         }        if  (flag)          {            root[x]  = i;            if  (Dfs (x + 1,  y)     //  fall into an existing collection              {                 return true;            }             root[x] = x;         } &nbsP;  }    return dfs (x + 1, y + 1);     //  allocated a new collection, has used 1 balls, the number increased 1}int main () {    while  (scanf ("%d%d",  &children, &balls)  != eof)     {         memset (map, 0, sizeof (map));         for   (int i = 0; i < children; i++)          {            root[i] = i;         }        for  (int  i = 0; i < children; i++)          {            int cnt;          &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d",  &cnt);             for  (int j = 0; j < cnt; j++)              {                 int t;          &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d",  &t);                 map[i][t] = map[t][i] = 1;             }         }        if  (Balls >= children)          {             printf ("yes\n");         }        else         {            if   (DFS (0, 0))             {                 printf ("YES\n");             }             else            {                 printf ("NO\n") ;            }         }    }    system ("pause");     return  0;}


Hdu 2208 Oh, lovely little friend.

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.