fork download
  1. #include<bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. const int N=1e5+5;
  5. vector<int> a[N];
  6. int n,k,par,ans=0;
  7. void dfs1(int node,int p){
  8. if(node==k) par=p;
  9. for(auto i:a[node]){
  10. if(i!=p) dfs1(i,node);
  11. }
  12. }
  13. void go(int node,int p){
  14. ans++;
  15. for(auto i:a[node]){
  16. if(i!=p) go(i,node);
  17. }
  18. }
  19. signed main(){
  20. ios::sync_with_stdio(false); cin.tie(nullptr);
  21. cin>>n>>k;
  22. for(int i=0;i<n-1;i++){
  23. int u,v; cin>>u>>v;
  24. a[u].push_back(v);
  25. a[v].push_back(u);
  26. }
  27. dfs1(1,0);
  28. go(k,par);
  29. cout<<ans<<endl;
  30.  
  31. }
Success #stdin #stdout 0.01s 5804KB
stdin
8 4
1 2 
1 3 
3 4 
4 5
4 6 
5 7 
5 8
stdout
5