1018. Binary Apple Tree

1018. Binary Apple Tree

Time limit: 1.0 second
Memory limit: 64 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 integers 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 (2 ≤ N ≤ 100; 1 ≤ Q ≤ N − 1). N denotes the number of enumerated points in a tree. Q denotes amount of branches that should be preserved. NextN − 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 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 
这道题目是第一道树形dp,比起一般的dp,感觉有那么一点难度。首先如何存储二叉树,为了方便dp,用l和r数组记录左孩子和右孩子。并且此题比较麻烦,题目给的是边权,不易dp,于是将边权下移到下方节点,但此时根节点就没有了权值,故定义为0;
假设dp[i][j]表示以i为根有j个节点的子树的最大apple数量。
那么有转移方程dp[i][j]=max{dp[l[i]][k]+dp[r[i]][j-1-k]}+v[i] 0<=k<j. 注意dp[][0]=0并且由于叶子节点没有孩子,这里初始化l和r为0,所以dp[0][]=0. 树形dp感觉不好写f递推形式,故用了记忆化搜索。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define Maxn 110
using namespace std;

int adj[Maxn][Maxn],vis[Maxn],l[Maxn],r[Maxn];
int dp[Maxn][Maxn],v[Maxn];
void dfs(int u,int n){
    vis[u]=1;
    for(int i=1;i<=n;i++){
        if(vis[i]) continue;
        if(adj[u][i]!=-1){
            if(!l[u]) l[u]=i;
            else r[u]=i;
            v[i]=adj[u][i];
            dfs(i,n);
        }
    }
}
int dfs1(int x,int y){
    if(x==0||y==0) return 0;
    int &ans=dp[x][y];
    if(ans) return ans;
    for(int i=0;i<y;i++)
        ans=max(ans,dfs1(l[x],i)+dfs1(r[x],y-1-i)+v[x]);
    return ans;
}
int main()
{
    int n,m,a,b,w;
    scanf("%d%d",&n,&m);
    memset(adj,-1,sizeof adj);
    memset(vis,0,sizeof vis);
    memset(l,0,sizeof l);
    memset(r,0,sizeof r);
    memset(dp,0,sizeof dp);
    v[1]=0;
    for(int i=1;i<n;i++){
        scanf("%d%d%d",&a,&b,&w);
        adj[a][b]=adj[b][a]=w;
    }
    dfs(1,n);
    printf("%d\n",dfs1(1,++m));
    return 0;
}

你可以在识别出苹果为“apple_down”时,调用轮廓提取的相关代码,来进行轮廓提取操作。具体实现方式可以参考以下步骤: 1. 在yolov4代码中,找到识别苹果的部分,判断苹果是否为“apple_down”。 2. 如果识别出的苹果为“apple_down”,则调用轮廓提取的相关代码(例如OpenCV中的findContours函数),来提取苹果的轮廓。 3. 对于识别出的苹果不为“apple_down”的情况,不做任何处理,直接输出标签和置信度即可。 以下是一个简单的示例代码,仅供参考: ```python import cv2 # 加载YOLOv4模型和标签 net = cv2.dnn.readNetFromDarknet(config_path, weights_path) labels = open(labels_path).read().strip().split("\n") # 提取图像中的苹果轮廓 def extract_apple_contours(image): # 使用OpenCV的findContours函数提取轮廓 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) return contours # 识别图像中的苹果 def detect_apple(image): # 使用YOLOv4模型进行目标检测 blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] outputs = net.forward(output_layers) # 遍历输出层,找到苹果并提取其轮廓 apple_contours = [] for output in outputs: for detection in output: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > confidence_threshold and labels[class_id] in ["apple_right", "apple_down"]: # 当识别到"apple_down"标签时,提取苹果轮廓 if labels[class_id] == "apple_down": apple_contours = extract_apple_contours(image) # 输出标签和置信度 label = f"{labels[class_id]}: {confidence:.2f}" print(label) # 在图像中绘制苹果轮廓 if len(apple_contours) > 0: cv2.drawContours(image, apple_contours, -1, (0, 255, 0), 2) return image ``` 这里使用了OpenCV中的findContours函数来提取苹果轮廓。在detect_apple函数中,当识别到“apple_down”标签时,就会调用extract_apple_contours函数来提取苹果轮廓。在主函数中,我们可以通过调用detect_apple函数来对图像进行苹果识别和轮廓提取操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值