Problem - 1054
一开始有一种贪心思路是,将点以度数进行排序,因为取度数较大的点上放一个哨兵的收益最大,因此需要动态维护一个堆,使得这个堆堆顶始终是度数最大的点,然后从堆顶取点,并将点对应的相邻点度数一次减1,然后将度数为0的点直接踢出堆外以减小复杂度。而这个堆可以是二叉堆,修改、删除、插入、取堆顶元素的操作复杂度均为
所以从思路二继续尝试其他思路,发现从相反反向考虑,度数较大的点放哨兵收益最大,而度数最小的点(二分图无孤立点)即度数为1的点放哨兵度数收益最小,因此从反方向考虑这个问题的话,就是尽量不要再度数为1的点上放哨兵,而是用它的邻点取代它。因此我们维护一个队列,放置所有度数为1的点,每次取出队首
Read full article from Problem - 1054
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:
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 2http://blog.csdn.net/u014141559/article/details/38125679题目分析:用最少的士兵将所有的道路都监控起来这题是最小顶点覆盖,可以用二分匹配,但是今天学树形DP,走起~~基础模板:1、建图,用邻接表2、dfs,遍历整个图,直到叶子,然后子问题一层一层往上合并成整个问题状态转移 dp[i][0] i节点不放士兵所需要的最少方案数,dp[i][1] i节点放士兵的最少方案数每个节点都有两个状态,放士兵或者不放士兵1.如果该节点不放士兵,那么它的所有子节点必须放2.如果该节点放士兵,那么它的子节点有两种决策,放或不放,去最小的思路:1:结点状态可以分为取和不取,所以用二维数组表示结点的状态。
2:如果当前结点选取了,子结点可以选取也可以不选取,但是如果当前结点没有选取,那么子节点必须选取。
http://blog.csdn.net/scnu_jiechao/article/details/40455667
一棵n个结点的无根树(0 < n <= 1500),在一个结点放一个士兵,可以守护与这个点相邻的所有边,问最少需要多少个士兵,可以守护所有边。
X. DP - Tree
dp[i][1]表示以 i 为结点的子树,在 i 放一个士兵,可以守护所有边的最少士兵数。dp[i][0]表示以 i 为结点的子树,在 i 不放士兵,可以守护所有边的最少士兵数。状态转移方程(结点 j 是结点 i 的儿子):dp[i][1] += min(dp[j][1], dp[j][0]);(如果结点 i 放了士兵,那么 i 连向其儿子的边已被守护,所以其儿子可放可不放士兵)dp[i][0] += dp[j][1];(如果结点 i 不放士兵,那么 i 连向其儿子的边必须由其儿子守护)对于叶子结点i:dp[i][1] = 1;(虽然以叶子为根的子树无边,但因放了一个士兵在根,所以是1)dp[i][0] = 0;
- int dp[2][MAXN]; //dp[0][i] 节点i放士兵的最小消耗方案 dp[1][i] 节点i不放士兵的最小方案
- int n;
- int min(int a,int b)
- {
- return a<b?a:b;
- }
- void init()
- {
- int root,to;
- char a;
- int num;
- memset(dp,0,sizeof(dp));
- memset(visited,0,sizeof(visited));
- for(int i=0;i<=n;i++)
- node[i].clear();
- for(int i=1;i<=n;i++)
- {
- cin>>root>>a>>a>>num>>a;
- for(int j=0;j<num;j++)
- {
- cin>>to;
- node[root].push_back(to);
- node[to].push_back(root);
- }
- }
- }
- void dfs(int root)
- {
- int temp;
- visited[root]=true;
- for(int i=0;i<node[root].size();i++)
- {
- temp=node[root][i];
- if(visited[temp])
- continue;
- dfs(temp);
- dp[1][root]+=dp[0][temp];
- dp[0][root]+=min(dp[1][temp],dp[0][temp]);
- }
- dp[0][root]+=1;
- }
一开始有一种贪心思路是,将点以度数进行排序,因为取度数较大的点上放一个哨兵的收益最大,因此需要动态维护一个堆,使得这个堆堆顶始终是度数最大的点,然后从堆顶取点,并将点对应的相邻点度数一次减1,然后将度数为0的点直接踢出堆外以减小复杂度。而这个堆可以是二叉堆,修改、删除、插入、取堆顶元素的操作复杂度均为
O(logn)
,然而我这块儿也学得不好,直接敲模板又比较麻烦。所以从思路二继续尝试其他思路,发现从相反反向考虑,度数较大的点放哨兵收益最大,而度数最小的点(二分图无孤立点)即度数为1的点放哨兵度数收益最小,因此从反方向考虑这个问题的话,就是尽量不要再度数为1的点上放哨兵,而是用它的邻点取代它。因此我们维护一个队列,放置所有度数为1的点,每次取出队首
u
,并取出第一个邻点v
,将邻点v
的所有邻点度数更新,若出现度数为1的点则再加入队列。int main(){
int n;
// freopen("in.txt","r",stdin);
while(~scanf("%d",&n)){
memset(deg,0,sizeof(deg));
memset(vis,0,sizeof(vis));
memset(edge,0,sizeof(edge));
for(int i = 0 ; i < n ; i++){
int m,u;
scanf("%d:(%d)",&u,&m);
if(m > 0){
for(int j = 0 ; j < m; j++){
int v;
scanf("%d",&v);
edge[u].push_back(v);
edge[v].push_back(u);
deg[u]++;
deg[v]++;
}
}
}
if(n == 1){
puts("1");
continue;
}
queue <int> q;
for(int i = 0 ; i < n; i++){
if(deg[i] == 1)
q.push(i);
}
int ans = 0;
while(!q.empty()){
int u = q.front();
q.pop();
if(vis[u])
continue;
for(int i = 0; i < edge[u].size(); i++){
int v = edge[u][i];
if(!vis[v]){
vis[v] = 1;
ans++;
//选1个放烧饼
for(int j = 0; j < edge[v].size(); j++){
int w = edge[v][j];
deg[w] --;
if(deg[w] == 1 && !vis[w]){
q.push(w);
}
}
break;
}
}
}
cout<<ans<<endl;
}
}
X. 最小顶点覆盖,匈牙利算法+链式前向星Read full article from Problem - 1054