Problem E: Cinema
Description
There are N universities in the city of ZhuHai, numbered from 1 to n. These universities are connected by N-1 roads and any two universities reachable. A part of universities connected by a single road are called “adjacent”. You’re the manager of a nationwide-famous cinema -- Joy Luck Union Cinema, who is planning to build a series of cinemas in the universities in Zhu Hai to serve the College students. However, college students are busy, so they only want to watch movies in their own campus or “adjacent” campus. As we all know,building a cinema is costly. You want to save expense buy you also want to serve all students. Therefore you have to decide what is the minimum number of JLU cinemas to be built, so that for a student of any campuses, at least one JLU cinema is accessible?
Input
There are multiple test cases in the input. Each test case starts with a line containing an integer N (0<N<=10000), indicating the number of universities. Then n-1 lines follows, each of which contains two integers A and B, representing A and B are connected by a road.
Output
For each test case, print the minimum amount of cinemas you should built in a single line.
Sample Input
6
1 2
2 4
3 6
5 3
3 2
3
2 1
1 3
Sample Output
2
1
这道题挺悲剧的,当时比赛的时候卡死在上面了...
这道题大概的题意是,在有N个节点的树 G(V,E) 上选择m个节点v属于V,设为集合S,使得V中任意一个节点可以通过<=1 的边到达S中的节点。求m的最小值。
这道题可以用贪心来解决。
这道题是一道典型的树型DP的题目,其原型是 Party at Hali-Bula :原型的贪心解法如下
1、找到一个编号最小的叶子结点(对于这题也就是度为0的点),在其父结点放置一个电影院, 删除所有与其父节点相连的边
2、重复1直到删光所有的边
其树型DP的做法为:
dproot[i]表示以 i 为根的子树,在 i 上面设置一个士兵, 完成整个子树需要多少个士兵
p[i] 表示看守整个以 i 为根的子树需要设置多少个士兵;
状态转移方程:
叶子节点 : dproot[i] = 1 , p[i] = 0;
非叶子节点:dproot[i] = 1 + Σp[j] (其中 j 是 i 的儿子)
对于本题而言,将一个点看守边变成了看守相邻的点,可以用类似的方法建立方程。