树形dp
数的最小点覆盖问题
对于树形dp我的理解是:在一棵树上做动态规划
HDU1054-Strategic Game(题目链接点击题目即可)
- 题意:给出n个节点 节点编号0~n-1 ,每个节点站一个哨兵,每个哨兵观察到与自己相邻的两条边,问最少需要多少个哨兵才能观察到所有边
- 题面:
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
The input file contains several data sets in text format. Each data set represents a tree with the following description:
the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifier
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.
For example for the tree:
the solution is one soldier ( at the node 1).
The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following table:
- 样例:
Sample Input 4 0:(1) 1 1:(2) 2 3 2:(0) 3:(0) 5 3:(3) 1 4 2 1:(1) 0 2:(0) 0:(0) 4:(0) Sample Output 1 2
- 可以把所有的点黑白染色,然后连边求最大匹配。以下是我用了dp过的。
- 开的数组:dp[N][2]:该数组:针对当前节点 选或者不选
dp[i][0]:不选
dp[i][1]:选
- 状态转移方程(是关键):
for(int i=0;i) { dp[root][0]+=dp[v[root][i]][1];//若当前节点不选,则子节点必须选 dp[root][1]+=min(dp[v[root][i]][1],dp[v[root][i]][0]);//当前节点选,子节点(选/不选) }
AC代码:
1 //给出n个节点 节点编号0~n-1 2 //每个节点站一个哨兵,每个哨兵观察到与自己相邻的两条边 3 //问最少需要多少个哨兵才能观察到所有边 4 5 #include<string.h> 6 #include7 #include 8 #include 9 #include 10 #include 11 #include
二分图匹配(匈牙利算法)练习
1、HDU - 2444 The Accomodation of Students
题面:
There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room. Calculate the maximum number of pairs that can be arranged into these double rooms. Input For each data set: The first line gives two integers, n and m(1200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs. Proceed to the end of file. Output If these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms. Sample Input 4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6 Sample Output No 3
题意:给出n个顶点,m个对应情况,问该图是否是二分图。是,输出最大匹配数;否,输出No。
思路:bfs判断是否是二分图,匈牙利算法dfs判断最大匹配数。
AC代码:
1 #include<string.h> 2 #include3 #include 4 #include 5 #include 6 #include 7 #include
2、HDU - 1083 Courses
题面:
Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions: . every student in the committee represents a different course (a student can represent a course if he/she visits that course) . each course has a representative in the committee Your program should read sets of data from a text file. The first line of the input file contains the number of the data sets. Each data set is presented in the following format: P N Count1 Student1 1 Student1 2 ... Student1 Count1 Count2 Student2 1 Student2 2 ... Student2 Count2 ...... CountP StudentP 1 StudentP 2 ... StudentP CountP The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses . from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you'll find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N. There are no blank lines between consecutive sets of data. Input data are correct. The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line. An example of program input and output: Input 2 3 3 3 1 2 3 2 1 2 1 1 3 3 2 1 3 2 1 3 1 1 Output YES NO
题意:有p门课程,n个学生,每个学生在一个committee代表一门课程,求学生和课程的最大匹配,若最大匹配等于p,输出YES,否则输出NO。
思路:匈牙利dfs模板套一下即可。
注意:单向边
AC代码:
1 #include<string.h> 2 #include3 #include 4 #include 5 #include 6 #include 7 #include
3、HDU4185 Oil Skimming
题面:
Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water. Input The input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell. Output For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted. Sample Input 1 6 ...... .##... .##... ....#. ....## ...... Sample Output Case 1: 3
题意:最多有多少个##,竖着也可以
思路:还是老样子,套dfs匈牙利算法的模板
注意:里面需要给每个#进行编号,目的是方便存边,每一个#对应一个编号,便于配对,否则每一个#都得跟着x和y坐标,不利于存边
具体实现步骤:
- 找到所有的#给它们编号
int num=0; for(int i=0; i) { scanf("%s",a[i]); for(int j=0; j ) { if(a[i][j]=='#') id[i][j]=++num; } }
- bfs去寻找上下左右的 #
void bfs(int x,int y,int idd) { for(int i=0; i<4; i++) { int tx=x+to[i][0]; int ty=y+to[i][1]; if(tx>=0&&tx=0&&ty 0) { v[idd].push_back(id[tx][ty]); v[id[tx][ty]].push_back(idd); } } }
- 对每一个#进行寻找
for(int i=0; i) { for(int j=0; j ) { if(id[i][j]!=0) { bfs(i,j,id[i][j]); } } }
AC代码:
1 #include<string.h> 2 #include3 #include 4 #include 5 #include 6 #include 7 #include
二分图匹配代码注意点:
- 存边的时候需要判断是否需要存入双向边,如果是双向边的话最后的结果需要除以2,因为会有重边;
- vector数组存边的时候需要进行清空,根据题目判断是从1~N还是0~N-1,要是不确定直接从0~N好了;
- f 数组记得清空。
待解决
- 字体设置的代码出问题了,颜色就这个样子了。。乱糟糟的。。
- 匈牙利算法:不知道为什么下面不带注释的是正确的,改成带注释的版本就是错误的???
bool dfs(int x) { for(int i=0; i) { int s=v[x][i]; if(book[s]==0) { book[s]=1; if(f[s]==-1||dfs(f[s])) { f[s]=x; return 1; } } // if(v[x][i]&&!book[i]) // { // book[i]=1; // if(f[i]==-1||dfs(f[i])) // { // f[i]=x; // return 1; // } // } } return 0; }
- HDU4185中,看下面代码,不知道为什么都等于3???
if(tx>=0&&tx=0&&ty 0) { v[idd].push_back(id[tx][ty]); v[id[tx][ty]].push_back(idd);//加和不加都得/2且第一组数据都==3 }
- HDU4185中,找两个#不知道为什么双向边除以二可以过,但是单向边不除以2不能过???
- HDU1083中,只能用单向边过的原因,emm,我觉得是因为一个学生可以对应多门课程,可是这样说的话一个课程也可以对应多个学生,为什么不可以双向边???