Advertisement
Guest User

grust' pechal'

a guest
Nov 18th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <stack>
  9. #include <queue>
  10. #include <deque>
  11. using namespace std;
  12.  
  13. #define TASK "formation"
  14.  
  15. struct sovietsky_soldat {
  16.     int left, right;
  17.     sovietsky_soldat() {
  18.         left = right = 0;
  19.     }
  20. };
  21.  
  22. vector < sovietsky_soldat > p;
  23.  
  24. void left(int a, int b) {
  25.     if (p[b].left != 0) {
  26.         p[a].left = p[b].left;
  27.     }
  28.     p[b].left = a;
  29.     p[a].right = b;
  30. }
  31.  
  32. void right(int a, int b) {
  33.     if (p[b].right != 0) {
  34.         p[a].right = p[b].right;
  35.     }
  36.     p[b].right = a;
  37.     p[a].left = b;
  38. }
  39.  
  40. void leave(int a) {
  41.     if (p[a].left != 0) {
  42.         p[p[a].right].left = p[a].left;
  43.     }
  44.     if (p[a].right != 0) {
  45.         p[p[a].left].right = p[a].right;
  46.     }
  47.     p[a] = sovietsky_soldat();
  48. }
  49.  
  50. void name(int a) {
  51.     cout << p[a].left << " " << p[a].right << "\n";
  52. }
  53.  
  54. int main() {
  55.  
  56. #ifdef _DEBUG
  57.     freopen("debug.in", "r", stdin);
  58.     freopen("debug.out", "w", stdout);
  59. #else
  60.     freopen(TASK".in", "r", stdin);
  61.     freopen(TASK".out", "w", stdout);
  62. #endif // _DEBUG
  63.    
  64.     int n, m, a, b;
  65.     string s;
  66.     cin >> n >> m;
  67.     for (int i = 0; i <= n; i++) {
  68.         p.push_back(sovietsky_soldat());
  69.     }
  70.     for (int i = 0; i < m; i++) {
  71.         cin >> s >> a;
  72.         if (s == "left") {
  73.             cin >> b;
  74.             left(a, b);
  75.         }
  76.         if (s == "right") {
  77.             cin >> b;
  78.             right(a, b);
  79.         }
  80.         if (s == "leave") {
  81.             leave(a);
  82.         }
  83.         if (s == "name") {
  84.             name(a);
  85.         }
  86.     }
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement