Advertisement
Singasking

Untitled

Oct 3rd, 2022
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 20;
  4. vector<int> adj[N];
  5.  
  6. int dfs(int index,vector<int>& visited) {
  7. // cout<<(index+1)<<endl;
  8. visited[index]=true;
  9. for(auto y:adj[index]) {
  10. if(visited[y]==false) {
  11. // cout<<"Setting "<<y<< " to be visited"<<endl;
  12. visited[y]=true;
  13. return 1 + dfs(y,visited);
  14. }
  15. }
  16. return 1;
  17. }
  18. int main() {
  19. // your code goes here
  20. int t;
  21. cin>>t;
  22. while(t--) {
  23. int n;
  24. cin>>n;
  25. vector<int> visited(n,0);
  26. int a[n];
  27. for(int i=0;i<n;i++) { cin>>a[i]; adj[i].clear(); }
  28. //Build the graph for it and then run dfs
  29.  
  30. for(int i=0;i<n;i++) {
  31. for(int j=i+1;j<n;j++) {
  32. if(a[j]-a[i]<=2) {
  33. //Add edge
  34. adj[i].push_back(j);
  35. adj[j].push_back(i);
  36. }
  37.  
  38. }
  39.  
  40. }
  41. int ma=-1;
  42. int mi = 100000;
  43.  
  44. for(int i=0;i<n;i++) {
  45. //Call dfs on each node
  46. if(visited[i]==false) {
  47. int count=dfs(i,visited);
  48. // cout<<"Starting at "<<i+1<<" we can infect at max "<<count<<" people "<<endl<<endl;
  49. ma = max(ma,count);
  50.  
  51. mi = min(mi,count);
  52. }
  53. }
  54. cout<<mi<<" "<<ma<<endl;
  55. }
  56.  
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement