fork(1) download
  1. /*
  2.  
  3.   In the name of Allah, Most Gracious, Most Merciful
  4.  
  5.  
  6. */
  7. #include<bits/stdc++.h>
  8. #include<numeric>
  9. using namespace std;
  10.  
  11. //*** constant value ***
  12. const long double PI = 3.141592653589793238;
  13. const double EPS = 1e-6 ;
  14. //*** constant value ***
  15.  
  16. //****** define ********
  17. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  18. #define ll long long
  19. #define dd double
  20. #define vll vector<ll>
  21. #define vs vector<string>
  22. #define fn(i,n) for(ll i = 0; i<n; i++)
  23. #define f(i,a,b) for(i=a;i<b;i++)
  24. #define f1(i,a,b) for(i=b-1;i>=a;i--)
  25. #define pii pair<int,int>
  26. #define pll pair<ll,ll>
  27. #define pls pair<ll,string>
  28. #define vpl vector<pll>
  29. #define pb push_back
  30. #define mk make_pair
  31. #define x first
  32. #define y second
  33. #define tt cout<<"Came here"<<endl;
  34. //****** define ********
  35. void solve()
  36. {
  37. ll vertex,edge,a,b;
  38. cin>>vertex>>edge;
  39. vector<vector<ll> >v(vertex+1);
  40. vector<ll>visited(vertex+1,0);
  41. fn(i,edge)
  42. {
  43. cin>>a>>b;
  44. v[a].pb(b);
  45. v[b].pb(a);
  46. }
  47.  
  48. stack<ll>q;
  49. q.push(a);
  50.  
  51. while(!q.empty())
  52. {
  53. ll s = q.top();
  54. q.pop();
  55. fn(i,v[s].size())
  56. if(!visited[v[s][i]])
  57. q.push(v[s][i]), vertex--;
  58.  
  59. visited[s] = 1;
  60. }
  61.  
  62.  
  63. cout<<vertex<<endl;
  64.  
  65. }
  66.  
  67. int main()
  68. {
  69. IOS;
  70. ll tc;
  71. cin>>tc;
  72. while(tc--)
  73. {
  74. solve();
  75. }
  76.  
  77.  
  78. return 0;
  79. }
  80.  
  81.  
Runtime error #stdin #stdout #stderr 0s 4236KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc