fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4.  
  5. const int MAXN=2E5+1;
  6.  
  7. int cnt=0;
  8. vector<ll>g[MAXN];
  9. vector<ll>vis(MAXN,0);
  10.  
  11. void dfs(int node,int par){
  12. for(auto &c:g[node]){
  13. if(c != par){
  14. dfs(c,node);
  15. if(!vis[node] && !vis[c]){
  16. //this edge bw node and c should be a part of matching set
  17. cnt++;
  18. vis[node]=vis[c]=1;
  19. }
  20. }
  21. }
  22. }
  23.  
  24. void _144()
  25. {
  26. ll n;
  27. cin>>n;
  28.  
  29. for(int i=1;i<=n-1;i++){
  30. ll a,b;
  31. cin>>a>>b;
  32.  
  33. g[a].push_back(b);
  34. g[b].push_back(a);
  35. }
  36.  
  37. dfs(1,-1);
  38.  
  39. cout<<cnt;
  40. return;
  41. }
  42.  
  43. int main() {
  44. ios::sync_with_stdio(false);
  45. cin.tie(nullptr);
  46.  
  47. int t=1;
  48. // cin >> t;
  49.  
  50. while (t--) _144();
  51. return 0;
  52. }
Runtime error #stdin #stdout 0.02s 9500KB
stdin
Standard input is empty
stdout
Standard output is empty