Gym - 102569H Tree Painting
You are given a tree with nn vertices and (n−1)(n−1) edges. At the beginning its edges and vertices are not painted. You can perform the following operation: choose two vertices in the tree and paint the path between them (all vertices and edges along this path are painted).
What is the minimal number of such operations to paint the whole tree (all edges and all vertices)?
Input
The first line contains the integer nn (2≤n≤2000002≤n≤200000) — the number of vertices in the tree.
Each of the next (n−1)(n−1) lines contains two integers uiui, vivi (1≤ui,vi≤n,ui≠vi1≤ui,vi≤n,ui≠vi) — the vertices connected by the ii-th edge.
Output
Output one integer — the minimal number of operations to paint the tree.
Examples
Input
5 1 2 1 3 1 4 1 5
Output
2
Input
5 1 2 2 3 3 4 4 5
Output
1
Input
4 1 2 3 2 4 2
Output
2
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
和牛客第二次C差不多的东西,比牛客C还简单一点,给出的样例也很友好。
统计一下边为1的点,就是叶子节点数,然后叶子节点数/2就ok了。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <stack> #include <queue> #include <map> #include <vector> #include <set> #include <deque> #include <cfloat> #include <algorithm> typedef long long ll; using namespace std; int gcd(int x,int y) { return y?gcd(y,x%y):x; } int lcm(int x,int y) { return x/gcd(x,y)*y; } vector<int> mp[200100]; int main() { int n; scanf("%d",&n); for(int i=0;i<n-1;i++) { int x,y; scanf("%d%d",&x,&y); mp[x].push_back(y); mp[y].push_back(x); } int ans=0; for(int i=1;i<=n;i++) { if(mp[i].size()==1) ans++; } printf("%d",(ans+1)/2); }

浙公网安备 33010602011771号