Getting Started with deep search

Source: Internet
Author: User
Tags cmath

Problem description

alas! A set of D (1 <= D <=) diseases (numbered 1..D) is running through the farm. Farmer John would like to milk as many of he n (1 <= n <=) cows as possible. If the milked cows carry more than K (1 <= K <= D) different diseases among them and then the milk would be too contamin Ated and would has to be discarded in its entirety. Please help determine the largest number of cows FJ can milk without have to discard the milk.

Input

* Line 1:three space-separated integers:n, D, and K

* Lines 2..n+1:line I+1 describes the diseases of cow I with a list of 1 or more space-separated integers. The first integer, D_i, is the count of cow I ' s diseases; The next d_i integers enumerate the actual diseases. Of course, the list is an empty if D_i is 0.

Output

* Line 1:m, the maximum number of cows which can is milked.

Sample input

6 3 201 11 21 32 2 12 2 1

Sample output

5

Tips

OUTPUT DETAILS:

If FJ Milks Cows 1, 2, 3, 5, and 6, then the milk would has only a diseases (#1 and #2), which is no greater than K (2). Test instructions: There are n cows d in the virus if the cow is infected with more than K virus is considered unhealthy to find out how many cows are healthy (give you some set N, let you take some set to seek and then set the number of elements can not exceed K.) Ask you how many collections to take. )
#include <cstdio>#include<queue>#include<cmath>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespacestd;Const intOO =0x3f3f3f3f;Const intN = $;Const intM =6000; typedefLong LongLL;structda{intK; intdie[ -]; BOOL operator< (ConstDa &a)Const    {        returnK <A.K; }}cow[1111];intvis[1111], N, D, K, ans, dis[1111];voidDfsintXintSumintmaxs) {    if(Sum > K)return ; Ans=Max (ans, maxs); //if (x = = n+1 && ans < maxs) ans = maxs;    if(x > N)return ;  for(inti = x; I <= N; i++)    {        if(Vis[i] = =0) {Vis[i]=1; intCNT =0;  for(intj =1; J <= Cow[i].k; J + +)            {                if(Dis[cow[i].die[j]] = =0) CNT++; DIS[COW[I].DIE[J]]++; }           //if (sum+cnt <= K && maxs+1 > ans) ans = maxs + 1; //sum + = cnt;DFS (i+1, sum+cnt, maxs+1); Vis[i]=0; //sum-= cnt;             for(intj =1; J <= Cow[i].k; J + +) Dis[cow[i].die[j]]--; }} DFS (x+1, Sum, MAXS);}intMain () {inti;  while(~SCANF (" %d%d%d", &n, &d, &K) {memset (Vis,0,sizeof(VIS)); memset (DIS,0,sizeof(DIS));  for(i =1; I <= N; i++) {scanf ("%d", &COW[I].K);  for(intj =1; J <= Cow[i].k; J + +) scanf ("%d", &Cow[i].die[j]); } ans=0; Sort (cow, cow+N); DFS (1,0,0); printf ("%d\n", ans); }    return 0;}
tle!!!

TLE then borrowed the idea of the great God to change the way of the search is very ingenious I did not think of (when the number of viruses selected to the K, will traverse the N cattle, if there are cows carrying the virus contained in k these viruses inside m++)

#include <cstdio> #include <queue> #include <cmath> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm>using namespace std;const int oo = 0x3f3f3f3f;const int N = 200;const I    NT M = 6000;typedef long long ll;struct da{int k;    int DIE[50];    BOOL operator < (const da &a) const {return K < A.K;    }} cow[1111];int vis[1111], N, D, K, ans;void solve () {int I, j, mini = 0; for (i = 1; I <= N, i++) {for (j = 1; J <= Cow[i].k; j + +) {if (!vis[cow[i].die[j]])/**&lt ;        If the cow has not been infected with the virus has not appeared this cow can not choose * * break; if (j = = cow[i].k+1) mini++;/**< The cow's virus has been selected on this cow * *} ans = max (ans, mini);}    void Dfs (int x, int sum) {if (sum = = K) solve ();    if (x > D) return;/**< recursive end boundary */vis[x] = 1;/**< Tag array tag This virus has occurred */dfs (x+1, sum+1);    Vis[x] = 0; DFS (x+1, sum);}    int main () {int i; while (~SCANF ("%d%d%d", &n, &d, &amP        K) {memset (Vis, 0, sizeof (VIS));            for (i = 1; I <= n; i++) {scanf ("%d", &AMP;COW[I].K);        for (int j = 1; J <= Cow[i].k; j + +) scanf ("%d", &cow[i].die[j]);        } ans = 0;        Sort (cow, cow+n);/**< seemingly can be unsorted */DFS (1, 0);    printf ("%d\n", ans); } return 0;}

  

Getting Started with deep search

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.