2020牛客国庆集训派对day3 Points

Points

https://ac.nowcoder.com/acm/contest/7830/F

Points

题目描述

Jack and Rose are playing games after working out so many difficult problems. They together drew a “Haizi” tree to show their collaboration. “Haizi” tree is the same as the tree defined in graph theory.

Now Jack would like to count the number of vertices whose degree is one in this tree. You need to help him with this calculation.

\textit{Degree}Degree: The degree of a vertex of a graph is the number of edges that are connected with the vertex.

输入描述:

The first line contains an integer {n}n (2 \leq n \leq 10^52≤n≤10
5
) --- the number of vertices.
The next {n-1}n−1 lines describe the edges of the tree, where the {i}i-th line contains two integers x_i≤n) --- the two vertices that is connected by the {i}i-th edge.

输出描述:

Output one integers in one line --- the number of vertices whose degree is one.
示例1
输入
复制

5
1 2
2 3
1 4
4 5

输出
复制

2

说明
As shown in figure below, only the vertex {3}3 and vertex {5}5 are connected with only one edge. So there are {2}2 vertices whose degree is one.
在这里插入图片描述

题解:

题目很简单就是问度为1的点有多少?
每输入一个边,就让两个点的度数+1,最后记录有多少点的度数为1

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+8;
int odd[maxn];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        int x,y; 
        cin>>x>>y;
        odd[x]++;
        odd[y]++;
    }
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        if(odd[i]==1)sum++;
    }
    cout<<sum;
    return 0;
}
全部评论

相关推荐

这是什么操作什么意思,这公司我服了...
斯派克spark:意思是有比你更便宜的牛马了
点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 14:00
林子大了什么鸟都有啊,我觉得我说的已经很客气了,阴阳谁呢
牛客62656195...:应该不是阴阳吧?你第一次注册的时候boss就说你是牛人
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务