Problem Description
Alas, the children are more troublesome. In a kindergarten, the teacher will have a game class, there are N children to play games, do the game to use a small ball, but the kindergarten only m a small ball, and some children do not like to play with some children, and only like to play with other children, such as silly girls only like and fools, silly root, silly eggs to play together, Silly root and do not like to play with silly eggs, silly like to play with fools. So the teacher had to group them, each group at least one ball can play, and each group will not have two children, mutual dislike. Now give a description of the relationship between the children in such a kindergarten, as a teacher, whether it can be a good game class.
Input
Data has multiple case, each case first input 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 a small ball. Then there are n rows, the first line I enter a K (0<=k
This code piece is written #include <iostream> #include <cstdio> #include <cstring> using namespace std;
int n,m,map[20][20];
int Root[20],flag;
void DFS (int x,int y) {x number, Y-ball number if (flag)//satisfies scheme return;
if (y>m)//Ball quantity super return;
if (x==n) {//Ball quantity is not exceeding, the number achieves the target; flag=1;
return;
for (int i=0;i<x;i++) {if (root[i]!=i)////Find a collection to locate the root node continue;
int tag=1;
for (int j=i;j<x && tag;j++) if (root[j]==i)//Find the node under the root node to determine if and people like tag=map[j][x]; if (tag) {//Like root[x]=i;
x points to root I; DFS (X+1,y);
The number plus one, into the next recursion. Root[x]=x;
Restore the point to find a possible end point for the next, to ensure that all possible} DFS (x+1,y+1) can be considered;
int main () {while (~scanf ("%d%d", &n,&m)) {memset (map,0,sizeof (map));
flag=0;
int k,x;
for (int i=0;i<n;i++) {scanf ("%d", &k);
Root[i]=i;
while (k--) { scanf ("%d", &x);
Map[i][x]=1;
} DFS (1,1);
if (M>=n | | flag) cout<< "YES" <<endl;
else cout<< "NO" <<endl;
return 0; }