Advertisement
didedoshka

Забавная игра

Aug 4th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <cmath>
  6. #include <set>
  7. #include <stack>
  8. #include <bitset>
  9. #include <map>
  10. #include <ctime>
  11. #include <numeric>
  12.  
  13.  
  14. #ifndef M_PI
  15. #define M_PI 3.141592653589
  16. #endif
  17. #define int long long
  18. #define double long double
  19.  
  20. #ifdef TIME
  21. #define start cin.tie(NULL); cout.tie(NULL); cout.setf(ios::fixed); cout.precision(10); ios_base::sync_with_stdio(false);int32_t START = clock()
  22. #define finish cout << "\ntime: " << (clock() - START) / (CLOCKS_PER_SEC * 1.0); return 0
  23. #endif
  24.  
  25. #ifndef TIME
  26. #define start cin.tie(NULL); cout.tie(NULL); cout.setf(ios::fixed); cout.precision(10); ios_base::sync_with_stdio(false)
  27. #define finish return 0
  28. #endif
  29.  
  30. using namespace std;
  31.  
  32.  
  33. //vector input
  34. template<typename T>
  35. istream &operator>>(istream &is, vector<T> &vec) {
  36.     for (auto &i : vec) {
  37.         cin >> i;
  38.     }
  39.     return is;
  40. }
  41.  
  42. //pair output
  43. template<typename E>
  44. ostream &operator<<(ostream &os, pair<E, E> &t) {
  45.     os << t.first << ' ' << t.second;
  46.     return os;
  47. }
  48.  
  49. //"map" pair output
  50. template<typename E>
  51. ostream &operator<<(ostream &os, pair<const E, E> &t) {
  52.     os << t.first << ' ' << t.second;
  53.     return os;
  54. }
  55.  
  56. //vector output
  57. template<typename T>
  58. ostream &operator<<(ostream &os, vector<T> &vec) {
  59.     for (T i : vec) {
  60.         os << i << ' ';
  61.     }
  62.     return os;
  63. }
  64.  
  65. //2 dimensional vector output
  66. template<typename T>
  67. ostream &operator<<(ostream &os, vector<vector<T> > &vec) {
  68.     for (vector<T> i : vec) {
  69.         os << i << '\n';
  70.     }
  71.     return os;
  72. }
  73.  
  74. vector<vector<int>> incorg;
  75. vector<vector<int>> rg;
  76. vector<vector<int>> g;
  77. vector<int> deq;
  78. vector<int> ans;
  79.  
  80. void dfs(int v, int p) {
  81.     for (auto u : incorg[v]) {
  82.         if (u != p) {
  83.             g[v].push_back(u);
  84.             dfs(u, v);
  85.         }
  86.     }
  87. }
  88.  
  89. int32_t main() {
  90.     start;
  91.  
  92.     int n, k;
  93.     cin >> n >> k;
  94.     incorg.assign(n, vector<int>());
  95.     rg.assign(n, vector<int>());
  96.     deq.assign(n, 0);
  97.     ans.assign(n, 0);
  98.     g.resize(n);
  99.  
  100.     for (int i = 0; i < n - 1; ++i) {
  101.         int from, to;
  102.         cin >> from >> to;
  103.         incorg[from - 1].push_back(to - 1);
  104.         incorg[to - 1].push_back(from - 1);
  105.     }
  106.  
  107.     dfs(k - 1, k - 1);
  108.  
  109.     for (int i = 0; i < n; ++i) {
  110.         for (auto v : g[i]) {
  111.             rg[v].push_back(i);
  112.             deq[i] += 1;
  113.         }
  114.     }
  115.  
  116.     queue<int> q;
  117.  
  118.     for (int i = 0; i < n; ++i) {
  119.         if (deq[i] == 0) {
  120.             ans[i] = 1;
  121.             q.push(i);
  122.         }
  123.     }
  124.  
  125.     while (!q.empty()) {
  126.         int v = q.front();
  127.         q.pop();
  128.         if (ans[v] == 1) {
  129.             for (auto u : rg[v]) {
  130.                 if (ans[u] == 0) {
  131.                     ans[u] = 2;
  132.                     q.push(u);
  133.                 }
  134.             }
  135.         } else if (ans[v] == 2) {
  136.             for (auto u : rg[v]) {
  137.                 deq[u] -= 1;
  138.                 if (deq[u] == 0 && ans[u] == 0) {
  139.                     ans[u] = 1;
  140.                     q.push(u);
  141.                 }
  142.             }
  143.         }
  144.     }
  145.  
  146.     if (ans[k - 1] != 2) {
  147.         cout << "First player loses" << '\n';
  148.     } else {
  149.         int mn = INT64_MAX;
  150.         for (auto u : g[k - 1]) {
  151.             if (ans[u] == 1) {
  152.                 mn = min(mn, u);
  153.             }
  154.         }
  155.         cout << "First player wins flying to airport" << " " << mn + 1 << '\n';
  156.     }
  157.  
  158.  
  159.     finish;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement