double_trouble

Beams_of_arcs_list(not oriented)

Sep 21st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class Graph
  9. {
  10. public:
  11.     const int block = -1;
  12.     int vcount, acount;
  13.     vector<int> IJ, H, L;
  14.  
  15.     Graph(int _vcount, vector<int>& _i, vector<int>& _j) :vcount(_vcount), acount(_i.size() * 2) {
  16.         IJ.resize(acount);
  17.         for (int k(0); k < acount / 2; ++k) {
  18.             IJ[k] = _i[k];
  19.             int ind = acount - k - 1;
  20.             IJ[ind] = _j[k];
  21.         }
  22.         makeList();
  23.     }
  24.  
  25.     void makeList() {
  26.         H.assign(vcount, block);
  27.         L.resize(acount);
  28.         for (int i(0); i < acount; ++i) {
  29.             int ind(IJ[i]);
  30.             L[i] = H[ind];
  31.             H[ind] = i;
  32.         }
  33.     }
  34.  
  35.     void addArc(int i, int j) {
  36.         IJ.insert(IJ.begin() + acount / 2, i);
  37.         IJ.insert(IJ.begin() + acount / 2 + 1, j);
  38.         acount += 2;
  39.         makeList();
  40.     }
  41.  
  42.     void print() {
  43.         cout << "graph G {" << endl;
  44.         for (int k(0); k < acount / 2; ++k) {
  45.             cout << IJ[k] + 1 << " -- " << IJ[acount - k - 1] + 1<< ";" << endl;
  46.         }
  47.         cout << "}";
  48.     }
  49.  
  50.     void showBeamsOfArcs() {
  51.         cout << "Beams of arcs: " << endl;
  52.         vector <int> ArcsList;
  53.         for (int i(0); i < vcount; ++i) {
  54.             ArcsList.clear();
  55.             for (int k(H[i]); k != -1; ) {
  56.                 ArcsList.push_back(k >= acount / 2 ? acount - k - 1 : k);
  57.                 k = L[k];
  58.             }
  59.  
  60.             int count(ArcsList.size());
  61.  
  62.             switch (count)
  63.             {
  64.             case 0:
  65.                 cout << "From vertex " << i + 1 << " come no edges";
  66.                 break;
  67.             case 1:
  68.                 cout << "From vertex " << i + 1 << " comes 1 edge: ";
  69.                 break;
  70.             default:
  71.                 cout << "From vertex " << i + 1 << " come " << count << " edges: ";
  72.             }
  73.            
  74.             if (count > 0) {
  75.                 for (int k(0); k < count - 1; ++k) {
  76.                     cout << ArcsList[k] + 1 << ", ";
  77.                 }
  78.                 cout << ArcsList[count - 1] + 1;
  79.             }
  80.  
  81.             cout << endl;
  82.         }
  83.     }
  84. };
  85.  
  86. int main()
  87. {
  88.     freopen("test.txt", "r", stdin);
  89.     freopen("test.gv", "w", stdout);
  90.  
  91.     int n, m;
  92.     cin >> n >> m;
  93.  
  94.     vector<int> _i, _j;
  95.     int ii, jj;
  96.     for (int i(0); i < m; ++i) {
  97.         cin >> ii >> jj;
  98.         _i.push_back(ii - 1);
  99.         _j.push_back(jj - 1);
  100.     }
  101.  
  102.     Graph G(n, _i, _j);
  103.     G.print();
  104.  
  105.     return 0;
  106. }
Add Comment
Please, Sign In to add comment