fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define maxx 100009
  4. #define pb push_back
  5. #define pii pair<int,int>
  6. queue<int>q;
  7. vector<pii>vpii[maxx];
  8. #define inf 0x7fffffff
  9. int X[]={+2,+2,-2,-2,+1,-1,-1,+1};
  10. int Y[]={-1,+1,-1,+1,+2,+2,-2,-2};
  11. map<char,int>cost;
  12. vector<int>v[maxx];
  13. int vis[maxx];
  14. enum{notvisited,black,white};
  15. int b=0,w=0;
  16. void bfs(int src)
  17. {
  18. vis[src]=black;
  19. b++;
  20. queue<int>q;
  21. q.push(src);
  22. while(!q.empty()){
  23. int s=q.front();
  24. q.pop();
  25. for(int i=0;i<v[s].size();i++){
  26.  
  27. if(vis[v[s][i]]==notvisited)
  28. {
  29. if(vis[s]==black)
  30. {
  31. vis[v[s][i]]=white;
  32. w++;
  33. }
  34. else if(vis[s]==white)
  35. {
  36. vis[v[s][i]]=black;
  37. b++;
  38. }
  39. q.push(v[s][i]);
  40. }
  41. }
  42. }
  43. }
  44. int main() {
  45. int n;
  46. scanf("%d",&n);
  47. int m=n-1,x;
  48. while(m--){
  49. int a,b;
  50. scanf("%d %d",&a,&b);
  51. v[a].pb(b);
  52. v[b].pb(a);
  53. x=a;
  54. }
  55. bfs(x);
  56. printf("%d\n",min(b,w));
  57. return 0;
  58. }
  59.  
Runtime error #stdin #stdout 0s 6008KB
stdin
Standard input is empty
stdout
Standard output is empty