Problem H
Fire! Fire!! Fire!!!
Input: standard input
Output: standard output
Time Limit: 15 seconds
Memory Limit: 32 MB
The ACM (Asian Cultural Museum) authority is planning to install fire exits in its galleries in order to handle the emergency situation arising in case of a sudden fire. The museum is a collection of numerous interconnected galleries. The galleries are connected by corridors in such a way that from any gallery there is exactly one path to reach any other gallery without visiting any intermediate gallery (a gallery that is on that path) more than once.
However, in order to reduce installation cost, it has been decided that not every gallery will have a fire exit. Fire exits will be installed in such a way that if any gallery does not have a fire exit then at least one of its adjacent galleries must have one and for each corridor at least one of the two galleries it connects must have a fire exit.. You are hired to determine where to put the fire exits under this constraint.
However, as a first step, you are expected to determine the minimum number of fire exits required.
Input
The input file may contain multiple test cases. The first line of each test case contains an integer N (1 £ N £ 1,000) indicating the number of galleries in this test case. Then follow N lines where the i-th (1 £ i £ N) line is the adjacency list of the i-th gallery (Each gallery is given a unique identification number from 1 to N for convenience). The adjacency list for gallery i starts with an integer ni (0 £ ni £ N - 1) indicating the number of galleries adjacent to this gallery, followed by ni integers giving the identification numbers of those galleries.
A test case containing a zero for N terminates the input.
Output
For each test case in the input file print a line containing the minimum number of fire exits required to meet the given constraint.
Sample Input
4
3 2 3 4
1 1
1 1
1 1
16
4 6 12 15 16
3 3 8 10
4 2 4 6 9
1 3
1 6
3 1 3 5
1 15
1 2
1 3
1 2
1 16
1 1
1 15
1 15
4 1 7 13 14
2 1 11
0
Sample Output
1
6
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Main
{
private static final boolean DEBUG = false;
private BufferedReader cin;
private PrintWriter cout;
private StreamTokenizer tokenizer;
private int[] cnt;
private int[][] adj;
private int n;
private int[][] memo;
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
tokenizer = new StreamTokenizer(cin);
} catch (Exception e) {
e.printStackTrace();
}
}
public String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER)
return String.valueOf((int)tokenizer.nval);
else if (tokenizer.ttype == StreamTokenizer.TT_WORD)
return tokenizer.sval;
else return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean input()
{
n = Integer.parseInt(next());
if (n == 0) return false;
adj = new int[n][n];
cnt = new int[n];
for (int i = 0; i < n; i++) {
cnt[i] = Integer.parseInt(next());
for (int j = 0; j < cnt[i]; j++) {
adj[i][j] = Integer.parseInt(next()) - 1;
}
}
return true;
}
private int dfs(int u, int p, int flag)
{
if (memo[u][flag] >= 0) return memo[u][flag];
int res;
if (flag == 1) {
res = 1;
for (int i = 0; i < cnt[u]; i++) {
int v = adj[u][i];
if (v != p) {
res += Math.min(dfs(v, u, 0), dfs(v, u, 1));
}
}
} else {
res = 0;
for (int i = 0; i < cnt[u]; i++) {
int v = adj[u][i];
if (v != p) {
res += dfs(v, u, 1);
}
}
}
memo[u][flag] = res;
return res;
}
public void solve()
{
memo = new int[n][2];
for (int i = 0; i < n; i++) {
Arrays.fill(memo[i], -1);
}
int ans = dfs(0, -1, 1);
if (n > 1) {
ans = Math.min(ans, dfs(0, -1, 0));
}
cout.println(ans);
cout.flush();
}
public static void main(String[] args)
{
Main solver = new Main();
solver.init();
while (solver.input()) {
solver.solve();
}
}
}