F - Christmas Spruce

题目描叙:

Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn’t have children and has a parent.

Let’s call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it’s a spruce.

The definition of a rooted tree can be found here.

Input
The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer pi (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ pi ≤ i).

Vertex 1 is the root. It’s guaranteed that the root has at least 2 children.

Output
Print “Yes” if the tree is a spruce and “No” otherwise.

Examples
Input
4
1
1
1
Output
Yes
Input
7
1
1
1
2
2
2
Output
No
Input
8
1
1
1
1
3
3
3
Output
Yes
Note

分析:

本题,我们可以首先用一个vis数组来将每个不是叶子结点的进行标记。然后,在通过循环,如果当前,这个结点是叶子结点,则将它的父结点++,最后在一个循环,判断父结点下的叶子结点的个数是不是大于3。

#include"stdio.h"
#include"string.h"
int main()
{
    int n,a[1001],vis[1001],i,j,k;
    while(~scanf("%d",&n))
    {
       memset(vis,0,sizeof(vis));
       for(i=2;i<=n;i++)
       {
           scanf("%d",&a[i]);
           vis[a[i]]=1;//将此结点的父结点为1
       }
       for(i=n;i>=2;i--)
       {
           if(vis[i]==0)//因为当前结点为叶子结点
               vis[a[i]]++;//将当前结点的父结点++运算,保存此结点有几个叶子结点
       }
       for(i=1;i<=n;i++)
       {//这里是因为标记为1,所以<4
            if(vis[i]&&vis[i]<4)//当前结点为父结点,且叶子结点小于4个
                 break;
       }
       if(i>n)
           printf("Yes\n");
       else
           printf("No\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您解决这个问题。首先,让我们看一下数据集的基本信息: 数据集名称:covtype.csv 数据集来源:UCI Machine Learning Repository 数据集描述:该数据集包含根据各种地理和环境因素测量的30个特征,来预测科罗拉多州罗斯国家森林区域树木类型。数据集中共有7个树木类型,分别是:1 - Spruce/Fir、2 - Lodgepole Pine、3 - Ponderosa Pine、4 - Cottonwood/Willow、5 - Aspen、6 - Douglas-fir、7 - Krummholz。 数据集下载地址:https://archive.ics.uci.edu/ml/datasets/covertype 根据您的需求,我们需要使用Jupyter Notebook来对该数据集进行处理和分析。以下是代码实现: 首先,我们需要导入所需的Python库,代码如下: ```python import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score ``` 接着,我们需要读取数据集,代码如下: ```python data = pd.read_csv('covtype.csv') ``` 为了方便处理数据,我们需要将数据集中的目标变量(即树木类型)与其他特征分开,代码如下: ```python X = data.iloc[:, :-1] y = data.iloc[:, -1] ``` 由于数据集中的特征数量比较多,并且各个特征之间的取值范围也有所不同,因此我们需要对数据进行标准化处理,代码如下: ```python scaler = StandardScaler() X = scaler.fit_transform(X) ``` 接着,我们需要将数据集分为训练集和测试集,代码如下: ```python X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=58000, random_state=42) ``` 最后,我们使用决策树模型对数据进行训练和预测,并计算模型的准确率,代码如下: ```python model = DecisionTreeClassifier(random_state=42) model.fit(X_train, y_train) y_pred = model.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print('准确率:', accuracy) ``` 这样,我们就完成了树木类型识别任务的实现。希望这些代码能够对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值