• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 87  文章 - 279  trackbacks - 0
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            潛心看書(shū)研究!

            常用鏈接

            留言簿(19)

            隨筆分類(lèi)(81)

            文章分類(lèi)(89)

            相冊(cè)

            ACM OJ

            My friends

            搜索

            •  

            積分與排名

            • 積分 - 216431
            • 排名 - 117

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            1018. A Binary Apple Tree
            Time Limit: 1.0 second
            Memory Limit: 16 MB

            Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enumerate by natural numbers the root of binary apple tree, points of branching and the ends of twigs. This way we may distinguish different branches by their ending points. We will assume that root of tree always is numbered by 1 and all numbers used for enumerating are numbered in range from 1 to N, where N is the total number of all enumerated points. For instance in the picture below N is equal to 5. Here is an example of an enumerated tree with four branches:

               2   5
            \ /
            3   4
            \ /
            1
            
            As you may know it's not convenient to pick an apples from a tree when there are too much of branches. That's why some of them should be removed from a tree. But you are interested in removing branches in the way of minimal loss of apples. So your are given amounts of apples on a branches and amount of branches that should be preserved. Your task is to determine how many apples can remain on a tree after removing of excessive branches.

             

            Input

            First line of input contains two numbers: N and Q (1 ≤ QN, 1 < N ≤ 100). N denotes the number of enumerated points in a tree. Q denotes amount of branches that should be preserved. Next N-1 lines contains descriptions of branches. Each description consists of a three integer numbers divided by spaces. The first two of them define branch by it's ending points. The third number defines the number of apples on this branch. You may assume that no branch contains more than 30000 apples.

            Output

            Output file should contain the only number — amount of apples that can be preserved. And don't forget to preserve tree's root ;-)

            Sample

            input
            output
            5 2
                        1 3 1
                        1 4 10
                        2 3 20
                        3 5 20
                        
            21
                        

            Problem Source: Ural State University Internal Contest '99 #2
            #include <iostream>
            using namespace std;

            const int MAXN = 110;
            const int MAXQ = 110;

            int n, q;
            int d[MAXN][MAXQ];
            int g[MAXN][MAXN], deg[MAXN], nw[MAXN], son[MAXN][2];
            int root;
            int f[MAXN], isleaf[MAXN];

            inline 
            int maxt(int a, int b) {
                
            return a > b ? a : b;
            }


            void DFS(int v) {
                
            int i, t = 0;
                
            for (i=1; i<=n; i++{
                    
            if (g[v][i] != -1 && !f[i]) {
                        son[v][t
            ++= i;
                        f[i] 
            = 1;
                        nw[i] 
            = g[v][i];
                        DFS(i);
                    }

                }

                
            if (!t) isleaf[v] = 1;
            }


            void buildT() {
                
            int i;
                
            for (i=1; i<=n; i++{
                    
            if (deg[i] == 2{
                        root 
            = i;
                        
            break;
                    }

                }

                f[root] 
            = 1; nw[root] = 0;
                DFS(root);
            }


            int DP(int ti, int tl) {
                
            if (tl <= 0return 0;
                
            if (d[ti][tl] != -1return d[ti][tl];
                
            if (isleaf[ti]) return nw[ti];
                
            int k, tmp = 0;
                
            for (k=0; k<tl; k++{
                    tmp 
            = maxt(tmp, DP(son[ti][0], k)+DP(son[ti][1], tl-1-k));
                }

                d[ti][tl] 
            = tmp + nw[ti];
                
            return d[ti][tl];
            }


            int main() {
                
            int i, j, k, x, y, w;
                scanf(
            "%d%d"&n, &q);
                memset(g, 
            -1sizeof(g));
                memset(d, 
            -1sizeof(d));
                
            for (i=0; i<n-1; i++{
                    scanf(
            "%d%d%d"&x, &y, &w); 
                    g[x][y] 
            = g[y][x] = w;
                    deg[x]
            ++, deg[y]++;
                }

                buildT();
                printf(
            "%d\n", DP(root, q+1));

                
            return 0;
            }

            posted on 2007-04-20 20:05 閱讀(1559) 評(píng)論(2)  編輯 收藏 引用 所屬分類(lèi): ACM題目

            FeedBack:
            # re: ural 1018(簡(jiǎn)單的樹(shù)狀dp) 2007-09-26 21:49 zYc
            Thank you!  回復(fù)  更多評(píng)論
              
            # re: ural 1018(簡(jiǎn)單的樹(shù)狀dp) 2008-02-25 21:24 jkdjfkdjfkdjflasdkjfkjeijfkdsjfkd
            弱弱的膜拜一下  回復(fù)  更多評(píng)論
              
            久久乐国产综合亚洲精品| 麻豆av久久av盛宴av| 日本精品一区二区久久久| 久久笫一福利免费导航 | 99久久国产免费福利| 久久国产精品国语对白| 久久精品国产乱子伦| 国产成人久久精品一区二区三区| 亚洲嫩草影院久久精品| 一个色综合久久| 国产精品久久久久天天影视| 人妻精品久久久久中文字幕| 久久久噜噜噜www成人网| 国产成人精品久久亚洲高清不卡 | 伊人久久国产免费观看视频| 99久久国产热无码精品免费| 欧美午夜A∨大片久久 | 四虎国产精品免费久久| 精品免费久久久久久久| 亚洲精品国产综合久久一线| 69久久精品无码一区二区| 亚洲美日韩Av中文字幕无码久久久妻妇| 日本强好片久久久久久AAA| 久久久久久av无码免费看大片 | 久久久久久精品久久久久| 91精品国产综合久久四虎久久无码一级| 亚洲日本va午夜中文字幕久久| 国产亚洲精品自在久久| 久久国产AVJUST麻豆| 国产精品免费久久久久影院| 奇米综合四色77777久久| 久久精品国产精品亚洲人人 | 狠狠色综合久久久久尤物| 午夜精品久久久久久久久| 久久久久综合国产欧美一区二区| 日韩久久久久久中文人妻| 亚洲午夜精品久久久久久app| 91亚洲国产成人久久精品| 久久精品国产亚洲av影院| 久久久久久久久久久| 人妻系列无码专区久久五月天|