UVa10243 - Fire! Fire!! Fire!!!(dp)

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();
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值