Source code for submission s932

fn.cpp

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. #include <stack>
  6.  
  7. using namespace std;
  8.  
  9. bool visited[10000];
  10.  
  11. bool findBunny(vector<vector<int> > & gr, int start){
  12. stack<int> s, depth;
  13. int paws = 0, maxDepth = 0;
  14.  
  15. s.push(start);
  16. depth.push(0);
  17.  
  18. while(!s.empty()){
  19. int n = s.top(), d = depth.top();
  20. s.pop();
  21. visited[n] = true;
  22. bool leaf = true;
  23.  
  24. if(d > maxDepth) maxDepth = d;
  25.  
  26. //printf("node %d: ", n);
  27. for(unsigned i = 0; i < gr[n].size(); i++){
  28. if(!visited[gr[n][i]]){
  29. //printf("%d, ", gr[n][i]);
  30. s.push(gr[n][i]);
  31. depth.push(d + 1);
  32. leaf = false;
  33. }
  34. }
  35. //printf("\n");
  36.  
  37.  
  38. if(leaf){
  39. paws++;
  40. }
  41. }
  42.  
  43. if(maxDepth >= 2){
  44. paws++;
  45. }
  46.  
  47. //printf("paws: %d\n", paws);
  48.  
  49. return paws > 3;
  50. }
  51.  
  52. void solve(int n, int m){
  53. vector<vector<int> > gr(n + 1);
  54. int a, b;
  55.  
  56. memset(visited, 0, 10000 * sizeof(bool));
  57.  
  58. for(int i = 0; i < m; i++){
  59. scanf("%d %d ", &a, &b);
  60. gr[a].push_back(b);
  61. gr[b].push_back(a);
  62. }
  63.  
  64. for(int i = 1; i <= n; i++){
  65. if(!visited[i]){
  66. if(findBunny(gr, i)){
  67. printf("YES\n");
  68. return;
  69. }
  70. }
  71. }
  72.  
  73. printf("NO\n");
  74. }
  75.  
  76. int main(){
  77. int n, m;
  78.  
  79. while(scanf("%d %d ", &n, &m) == 2){
  80. solve(n, m);
  81. }
  82.  
  83. return 0;
  84. }
  85.