Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. /*
  2.  
  3. Sviatoslav Bidzilia 2019
  4. CF: anakib1
  5.  
  6. */
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10. #include <algorithm>
  11. #include <stdio.h>
  12. #include <cstring>
  13. #include <string>
  14. #include <cstdlib>
  15. #include <vector>
  16. #include <bitset>
  17. #include <map>
  18. #include <chrono>
  19. #include <unordered_set>
  20. #include <unordered_map>
  21. #include <numeric>
  22. #include <queue>
  23. #include <ctime>
  24. #include <stack>
  25. #include <set>
  26. #include <list>
  27. #include <deque>
  28. #include <iomanip>
  29. #include <sstream>
  30. #include <fstream>
  31. #include <stdarg.h>
  32. #include <utility>
  33.  
  34. using namespace std;
  35.  
  36. #define pb push_back
  37. #define mp make_pair
  38. #define ll long long
  39. #define ull unisgned long long
  40. #define ld long double
  41. #define all(a) a.begin(), a.end()
  42. #define SORT(a) sort(all(a))
  43. #define pii pair<int, int>
  44. #define tii triple<int, int, int>
  45. #define e 1e-7
  46. #define PI acos(-1)
  47. #define inf 1e17
  48. #define vi vector<int>
  49. #define F first
  50. #define S second
  51. #define rng(x) for(int _ = 0; _ < (x); _++)
  52. #define rngi(i, x) for(int i = 0; i < (x); i++)
  53. #define rngsi(s, i, x) for(int i = (s); i <(x); i++)
  54. #define int long long
  55.  
  56. template<typename A, typename B, typename C>
  57. struct triple
  58. {
  59.     A X;
  60.     B Y;
  61.     C Z;
  62.     triple(A a = 0, B b = 0, C c = 0) :X(a), Y(b), Z(c) {}
  63. };
  64. template<typename A, typename B, typename C>
  65. triple<A, B, C> make_triple(A a = 0, B b = 0, C c = 0)
  66. {
  67.     return triple<A, B, C>(a, b, c);
  68. }
  69. template<typename A, typename B, typename C>
  70. bool operator<(const triple<A, B, C>& a, const triple<A, B, C>& b)
  71. {
  72.     if (a.X != b.X)
  73.         return a.X < b.X;
  74.     if (a.Y != b.Y)
  75.         return a.Y < b.Y;
  76.     return a.Z < b.Z;
  77. }
  78. template<typename T, typename SS>
  79. ostream& operator<<(ostream& ofs, const pair<T, SS>& p)
  80. {
  81.     ofs << "( " << p.F << " , " << p.S << " )";
  82.     return ofs;
  83. }
  84. template<typename T>
  85. void print(T a)
  86. {
  87.     for (auto i : a)
  88.         cout << i << ' ';
  89.     cout << '\n';
  90. }
  91. template<typename T>
  92. T max(T a, T b, T c)
  93. {
  94.     return max(a, max(b, c));
  95. }
  96. template<typename T>
  97. T min(T a, T b, T c)
  98. {
  99.     return min(a, min(b, c));
  100. }
  101. struct custom_hash
  102. {
  103.     static uint64_t splitmix64(uint64_t x)
  104.     {
  105.         x += 0x9e3779b97f4a7c15;
  106.         x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
  107.         x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
  108.         return x ^ (x >> 31);
  109.     }
  110.  
  111.     size_t operator()(uint64_t x) const
  112.     {
  113.         static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
  114.         return splitmix64(x + FIXED_RANDOM);
  115.     }
  116. };
  117. vector<vi> g;
  118. int n;
  119. set<int> now;
  120. void dfs(vector<vi>& gg, int v)
  121. {
  122.     now.insert(v);
  123.     for (auto to : gg[v])
  124.         if (!now.count(to))
  125.             dfs(gg, to);
  126. }
  127. pair<vi, vi> bfs(int v)
  128. {
  129.     queue<int> q;
  130.     vi d(n, inf);
  131.     d[v] = 0;
  132.     q.push(v);
  133.     vector< vi > p(n);
  134.     now.clear();
  135.     while (q.size())
  136.     {
  137.         int v = q.front();
  138.         q.pop();
  139.         for(auto to : g[v])
  140.             if (d[to] == d[v] + 1)
  141.             {
  142.                 p[to].pb(v);
  143.             }
  144.             else if(d[to] > d[v] + 1)
  145.             {
  146.                 p[to].clear();
  147.                 p[to].pb(v);
  148.                 d[to] = d[v] + 1;
  149.                 q.push(to);
  150.             }
  151.     }
  152.     dfs(p, n - 1);
  153.     vi ans;
  154.     for (auto i : now)
  155.         ans.pb(i);
  156.     return mp(ans, d);
  157. }
  158. signed main()
  159. {
  160.     //freopen(".in", "r", stdin);
  161.     //freopen(".out", "w", stdout);
  162.     srand(time(NULL));
  163.     cin.tie(0);
  164.     cout.tie(0);
  165.     ios_base::sync_with_stdio(false);
  166.     cin >> n;
  167.     g.resize(n);
  168.     int m;
  169.     cin >> m;
  170.     int x, y, z;
  171.     cin >> x >> y >> z;
  172.     x--, y--, z--;
  173.     rng(m)
  174.     {
  175.         int a, b;
  176.         cin >> a >> b;
  177.         a--, b--;
  178.         g[a].pb(b);
  179.         g[b].pb(a);
  180.     }
  181.     set<int> a1;
  182.     auto t = bfs(x);
  183.     for (auto x : t.F)
  184.         a1.insert(x);
  185.     vi d1 = t.second;
  186.     set<int> a2;
  187.     t = bfs(y);
  188.     for (auto x : t.F)
  189.         a2.insert(x);
  190.     vi d2 = t.second;
  191.     set<int> a3;
  192.     t = bfs(z);
  193.     for (auto x : t.F)
  194.         a3.insert(x);
  195.     vi d3 = t.second;
  196.     vi ans;
  197.     for (int i = 0; i + 1 < n; i++)
  198.         if (a1.count(i) && a2.count(i) && a3.count(i) && d1[i] == d2[i] && d2[i] == d3[i])
  199.             ans.pb(i + 1);
  200.     cout << ans.size() << '\n';
  201.     print(ans);
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement