POJ 1463 树状dp

Strategic game

Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 6629   Accepted: 3058

Description

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.

For example for the tree:

the solution is one soldier ( at the node 1).

Input

The input 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_identifiernumber_of_roads

    or

    node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n
nodes (0 < n <= 1500);the number_of_roads in each line of input
will no more than 10. Every edge appears only once in the input data.

Output

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:

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

Source

Southeastern Europe 2000

POJ
1463:

题目意思:

在树的节点上放士兵,使得树的每一条边都有士兵看守,求所需要的最少士兵数;

解题思路:

树状dp,状态转移为:

如果这个点放了士兵,则该点的dp等于所有儿子节点放士兵或者不放士兵的最小值之和;

如果这个点不放士兵,则该点的dp等于所有儿子节点必须放士兵的和;

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cstdio>
 6 #include<vector>
 7 using namespace std;
 8 const int maxn=1507;
 9 const int inf=0x3f3f3ff;
10 struct node
11 {
12     int to,next;
13 };
14 node tree[15007];
15 vector<int> G[maxn];
16 int dp[maxn][2];
17 int n;
18 void init()
19 {
20     for(int i=0;i<maxn;i++) G[i].clear();
21     memset(tree,0,sizeof(tree));
22     for(int i=0;i<maxn;i++)
23     {
24         dp[i][0]=dp[i][1]=inf;
25     }
26 }
27 void dfs(int point)
28 {
29     int res1=0,res2=0;
30     for(int i=0;i<G[point].size();i++)
31     {
32         dfs(G[point][i]);
33         res1+=dp[G[point][i]][1];
34         res2+=min(dp[G[point][i]][1],dp[G[point][i]][0]);
35     }
36     dp[point][0]=res1;
37     dp[point][1]=min(dp[point][1],res2+1);
38 }
39 int main()
40 {
41    //freopen("in.txt","r",stdin);
42     while(~scanf("%d",&n)){
43         init();
44         int a,b,c,root=-1;
45         for(int i=1;i<=n;i++)
46         {
47             scanf("%d:(%d)",&a,&b);
48             if(root==-1) root=a;
49             for(int j=0;j<b;j++)
50             {
51                 scanf("%d",&c);
52                 G[a].push_back(c);
53              }
54         }
55         dfs(root);
56         int ans=min(dp[root][1],dp[root][0]);
57         printf("%d\n",ans);
58     }
59     return 0;
60 }
时间: 2024-10-12 20:40:46

POJ 1463 树状dp的相关文章

POJ 1155 树状dp

TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3856   Accepted: 2054 Description A TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tre

POJ 2342 树状dp

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4606   Accepted: 2615 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure

POJ 1463 树型DP

链接: http://poj.org/problem?id=1463 题意: 求一棵树的最小点覆盖 题解: dp[i][0].dp[i][1]分别表示不在i结点上和在i结点上放置士兵时整个以i结点为根的子树被覆盖用到用到目标的最少数量 状态转移: 对叶子结点,有dp[i][0]=0,dp[i][1]=1 (也是递归的出口) 对非叶子结点,有 dp[i][0]=∑(dp[i][1]) dp[i][1]=∑(min(dp[j][0],dp[j][1]))+1 (j为i的子结点) 代码: 31 int

POJ 2378 树状dp

Tree Cutting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3849   Accepted: 2304 Description After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible

树状DP (poj 2342)

题目:Anniversary party 题意:给出N各节点的快乐指数,以及父子关系,求最大快乐指数和(没人职员愿意跟直接上司一起玩): 思路:从底向上的树状DP: 第一种情况:第i个员工不参与,F[i][0] += max(F[k][1], F[k][0]);(k为i的儿子) 第二种情况:第i个员工参与,F[i][1] += F[k][0]; F[i][j]表示第i个员工是否参与: 边界:F[i][0] = 0:F[i][1] = 其快乐指数: #include <iostream> #in

洛谷P1122 最大子树和 (树状dp)

题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿时想到了一个有关修剪花卉的问题.于是当日课后,小明就向老师提出了这个问题: 一株奇怪的花卉,上面共连有N 朵花,共有N-1条枝干将花儿连在一起,并且未修剪时每朵花都不是孤立的.每朵花都有一个“美丽指数”,该数越大说明这朵花越漂亮,也有“美丽指数”为负 数的,说明这朵花看着都让人恶心.所谓“修剪”,意为:去掉其中的一条枝条,这样一株花就成了两株,扔掉

poj1947--Rebuilding Roads(树状dp)

Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9496   Accepted: 4316 Description The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The

洛谷P2015 二叉苹果树(树状dp)

题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来描述一根树枝的位置.下面是一颗有4个树枝的树 2 5 \ / 3 4 \ / 1 现在这颗树枝条太多了,需要剪枝.但是一些树枝上长有苹果. 给定需要保留的树枝数量,求出最多能留住多少苹果. 输入输出格式 输入格式: 第1行2个数,N和Q(1<=Q<= N,1<N<=100). N表示树

poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299 最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧 1.初始化as[i]=i:对as数组按照num[]的大小间接排序 2.bs[as[i]]=i:现在bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include