Source code for submission s1088

Go to diff to previous submission

fn.cpp

  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. struct Point {
  6. unsigned counter;
  7. vector<short> points;
  8.  
  9. Point() {
  10. counter = 0;
  11. }
  12. };
  13.  
  14. Point ** graph;
  15. unsigned N;
  16.  
  17. inline bool isPaw(unsigned point) {
  18. return (graph[point]->counter == 1);
  19. }
  20.  
  21. bool isBunny(unsigned point) {
  22. int paws = 0;
  23. for(vector<short>::iterator it = graph[point]->points.begin(); it != graph[point]->points.end(); ++it) {
  24. if(isPaw(*it)) {
  25. paws++;
  26. } else
  27. return false;
  28. }
  29. return paws >= 4;
  30. }
  31.  
  32. int main() {
  33. unsigned points;
  34. unsigned lines;
  35. while(cin >> points >> lines) {
  36. graph = new Point * [points];
  37. for (unsigned i = 0; i < points; i++) {
  38. graph[i] = new Point();
  39. }
  40.  
  41. for (unsigned i = 0; i < lines; i++) {
  42. unsigned x, y;
  43. cin >> x >> y;
  44. graph[x-1]->points.push_back(y-1);
  45. graph[x-1]->counter++;
  46.  
  47. graph[y-1]->points.push_back(y-1);
  48. graph[y-1]->counter++;
  49. }
  50.  
  51. N = points;
  52.  
  53. bool found = false;
  54. for (unsigned i = 0; i < N; i++) {
  55. if(graph[i]->counter >= 4 && isBunny(i)) {
  56. cout << "YES" << endl;
  57. found = true;
  58. break;
  59. }
  60. }
  61.  
  62. if (!found)
  63. cout << "NO" << endl;
  64.  
  65. for (unsigned i = 0; i < points; i++) {
  66. delete graph[i];
  67. }
  68. delete [] graph;
  69. }
  70.  
  71. return 0;
  72. }
  73.  
  74.  
  75.  
  76.  

Diff to submission s1078

otherBunny.cpp

--- c5.s1078.cteam009.fn.cpp.0.otherBunny.cpp
+++ c5.s1088.cteam009.fn.cpp.0.fn.cpp
@@ -1,69 +1,72 @@
-#include <iostream>
-#include <vector>
+#include<iostream>
+#include<vector>
 using namespace std;
 
-struct Point{
-        int m_counter;
-        vector<short> m_points;
-        Point(){
-                m_counter = 0;
+struct Point {
+        unsigned counter;
+        vector<short> points;
+
+        Point() {
+                counter = 0;
         }
 };
 
-int main(void){
-        int points, lines;
-        while(cin >> points >> lines){
-                Point ** p_points = new Point* [points];
-                for(int i = 0; i < points ; i++){
-                        p_points[i] = NULL;
+Point ** graph;
+unsigned N;
+
+inline bool isPaw(unsigned point) {
+        return (graph[point]->counter == 1);
+}
+
+bool isBunny(unsigned point) {
+        int paws = 0;
+        for(vector<short>::iterator it = graph[point]->points.begin(); it != graph[point]->points.end(); ++it) {
+                if(isPaw(*it)) {
+                        paws++;
+                } else
+                        return false;
+        }
+        return paws >= 4;
+}
+
+int main() {
+        unsigned points;
+        unsigned lines;
+        while(cin >> points >> lines) {
+                graph = new Point * [points];
+                for (unsigned i = 0; i < points; i++) {
+                        graph[i] = new Point();
                 }
 
-                short first, second;
-                for(int i = 0; i < lines ; i++){
-                        cin >> first >> second;
-                        first--;
-                        second--;
-                        if(p_points[first]){
-                                p_points[first]->m_counter++;
-                                p_points[first]->m_points.push_back(second);
-                        }
-                        else{
-                                p_points[first] = new Point();
-                                p_points[first]->m_counter = 1;
-                                p_points[first]->m_points.push_back(second);
-                        }
+                for (unsigned i = 0; i < lines; i++) {
+                        unsigned x, y;
+                        cin >> x >> y;
+                                graph[x-1]->points.push_back(y-1);
+                                graph[x-1]->counter++;
+                        
+                                graph[y-1]->points.push_back(y-1);
+                                graph[y-1]->counter++;
+                }
+        
+                N = points;             
 
-                        if(p_points[second]){
-                                p_points[second]->m_counter++;
-                                p_points[second]->m_points.push_back(first);
-                        }
-                        else{
-                                p_points[second] = new Point();
-                                p_points[second]->m_counter = 1;
-                                p_points[second]->m_points.push_back(first);
+                bool found = false;
+                for (unsigned i = 0; i < N; i++) {
+                        if(graph[i]->counter >= 4 && isBunny(i)) {
+                                cout << "YES" << endl;
+                                found = true;
+                                break;
                         }
                 }
                 
-                bool isBunny = false;
+                if (!found)
+                        cout << "NO" << endl;
 
-                for(int i = 0; i < points ; i++){
-                        if(p_points[i] && p_points[i]->m_counter==4){
-                                bool found = true;
-                                for(int j = 0; j < p_points[i]->m_points.size(); j++){
-                                        if(p_points[ p_points[i]->m_points[j] ]->m_counter != 1){
-                                                found = false;
-                                                break;
-                                        }
-                                }
-                                if(found) {
-                                        cout << "YES" << endl;
-                                        goto konec;                             
-                                }
-                        }
+                for (unsigned i = 0; i < points; i++) {
+                        delete graph[i];
                 }
-                cout << "NO" << endl;
-                konec:
-                        ;
+                delete [] graph;
         }
+
         return 0;
 }