fork download
  1. #include<bits/stdc++.h>
  2.  
  3. #define MAXSIZE 10000
  4. #define pb push_back
  5. using namespace::std;
  6.  
  7. int dfs(vector< vector < int> > &g, int v, bool vs[])
  8. {
  9. vs[v] = true;
  10. int count = 1;
  11. for(auto it = g[v].begin() ; it != g[v].end() ; ++it)//auto it : g[v])
  12. if(!vs[*it])
  13. count += dfs(g,*it,vs);
  14. return count;
  15. }
  16.  
  17. void firesc(vector <vector <int> > &g, int n, vector <int> &result)
  18. {
  19. bool *vs = new bool[n];
  20. vs[0] = true;
  21. for(int i =1 ; i <= n ; ++i)
  22. vs[i] = false;
  23. for(int i = 0 ; i <= n ; ++i)
  24. if(!vs[i])
  25. result.pb(dfs(g,i,vs));
  26. }
  27. int main()
  28. { //0 1 2 3 4
  29. int T,N,M,a,b,ways = 1; // 2 1 2
  30. vector <int> result; // 3
  31.  
  32. //cout<<"\nenter T :\t";
  33. cin>>T;
  34. while(T--)
  35. {
  36. // cout<<"\nenter N and M :\t";
  37. cin>>N>>M;
  38. vector <vector <int> > g(N+1);
  39. while(M--)
  40. {
  41. // cout<<"\nenter couples of friends :\t";
  42. cin>>a>>b;
  43. g[a].pb(b);
  44. g[b].pb(a);
  45. }
  46. firesc(g,N,result);
  47. cout<<result.size()<<' '; //cout<<"\n total routes :\t "<<result.size();
  48. for(auto it = result.begin() ; it != result.end() ; ++it ) //int x : result
  49. ways *= (*it);
  50.  
  51. cout<<ways<<endl; //cout<<"\ntotal ways to select captains "<<ways;
  52.  
  53. result.clear();
  54. /************************* for(auto it : g)
  55.   g[(*it)].clear();*******/
  56. for(int i = 0 ; i<N ; ++i)
  57. g[i].clear();
  58. ways = 1;
  59. }
  60.  
  61. return 0;
  62. }
  63.  
Runtime error #stdin #stdout #stderr 0s 4324KB
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