Links: https://ac.nowcoder.com/acm/contest/945/A
Source: Cattle-off network
Description
T-seed fillings, but some fillings that can not be put together. Some seek elected fillings program number is.
The first behavior file input two integers T and N (T≤20, N≤52), N represents the N next N rows will be restricted.
Next N rows, each row of the first number represents the number of the next number Z: a1, a2 ... az, which represents any one notch compound z can not both kinds of fillings.
If Z = 1, a1 indicates such fillings are not present in any combination.
If Z = 3 a1 = 3 a2 = 4 a3 = 6 represent 3,4,6 three kinds of fillings can not appear in any combination.
Input
Line 1: Two space-separated integers: T and N
Lines 2…N+1: Each line describes a constraint using space-separated
The first integer is the number of ingredients in constraint, Z (1 <= Z <= T). The subsequent Z integers (which are unique) list the ingredient(s) whose combination a pizza from consideration for the cows.
Output
Line 1: A single integer that is the total number of pizzas that can be created using the number of ingredients and constraints.
Input sample
6 5
1 1
2 4 2
3 3 2 6
1 5
3 3 4 6
Output sample
10
Direct dfs, due to the less restrictive, you can walk for a certain kind of limit again to check if the conflict
/*
* Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
* @Author: Ng Kimbing, HNU.
* @LastModified:2019-06-25 T 10:24:39.181 +08:00
*/
package ACMProblems.QianDaoTi;
import java.util.HashSet;
import java.util.Set;
import static ACMProblems.ACMIO.*;
/*
* 链接:https://ac.nowcoder.com/acm/contest/945/A
* 来源:牛客网
*
* ## Description
*
* 有T种馅料,但有些馅料不能被放在一起。求选出一些馅料的方案数是多少。
* 文件输入第一行为两个整数 T 和 N(T≤20,N≤52),N表示接下来N行会有N个限制。
* 接下来N行,每行的第一个数Z表示接下来数的个数:a1,a2......az,表示任意一种陷料中这z种馅料不能同时出现。
* 如果Z=1,则表示a1 这种陷料在任何一种组合中都不得出现。
* 如果Z=3 a1=3 a2=4 a3=6 表示3,4,6 三种馅料不能在任何一种组合中出现。
*
* ## Input
*
* Line 1: Two space-separated integers: T and N
* Lines 2..N+1: Each line describes a constraint using space-separated
* The first integer is the number of ingredients in constraint, Z (1 <= Z <= T). The subsequent Z integers (which are unique) list the ingredient(s) whose combination a pizza from consideration for the cows.
* ## Output
* Line 1: A single integer that is the total number of pizzas that can be created using the number of ingredients and constraints.
* ## Input sample
* >6 5
* 1 1
* 2 4 2
* 3 3 2 6
* 1 5
* 3 3 4 6
* ## Output sample
* >10
*/
public class Mix {
private static int pizzaNum;
private static Set<Integer>[] limits;
private static Set<Integer> currSet = new HashSet<>();
private static int index = 0;
private static int ans = 0;
/**
* input one limit
*
* @throws Exception EOF
*/
private static void addLimit() throws Exception {
int num = nextInt();
Set<Integer> tempSet = new HashSet<>();
for (int i = 0; i < num; ++i) {
int foo = nextInt();
tempSet.add(foo);
}
limits[index++] = tempSet;
}
/**
* try to add this stuff into the current set
*
* @param stuff the stuff going to be added
* @return returns whether the stuff can be added into the current set
*/
private static boolean canAdd(int stuff) {
currSet.add(stuff);
for (int i = 0; i < index; ++i) {
Set<Integer> limit = limits[i];
if (currSet.containsAll(limit)) {
currSet.remove(stuff);
return false;
}
}
return true;
}
private static void dfs(int curr) {
if (curr == pizzaNum + 1) {
++ans;
return;
}
if (canAdd(curr)) {
dfs(curr + 1);
currSet.remove(curr);
}
dfs(curr + 1);
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
setStream(System.in);
pizzaNum = nextInt();
int n = nextInt();
limits = new HashSet[n + 5];
for (int i = 0; i < n; ++i) {
addLimit();
}
dfs(1);
System.out.println(ans);
}
}