2020牛客国庆集训派对day3 Points
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
輸入
復(fù)制
輸出
復(fù)制
說明
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的點(diǎn)有多少?
每輸入一個(gè)邊,就讓兩個(gè)點(diǎn)的度數(shù)+1,最后記錄有多少點(diǎn)的度數(shù)為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; }總結(jié)
以上是生活随笔為你收集整理的2020牛客国庆集训派对day3 Points的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P3911 最小公倍数之和
- 下一篇: 2020牛客国庆集训派对day3 Lef