DuongNhi99

STNODE - VOI09

Jan 12th, 2022
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 1e4 + 5;
  5. const int maxM = 1e5 + 5;
  6.  
  7. typedef vector<vector<int>> dsk;
  8.  
  9. int n, m, s, t;
  10. vector<int> path;
  11.  
  12. bool visited[maxN];
  13.  
  14. // BFS tìm đường đi từ s đến t
  15. // Trả về danh sách các đỉnh trên đường đi
  16. vector<int> find_path(int s, int t, const dsk &ke) {
  17.     int n = ke.size();
  18.     vector<int> trace(n + 1, -1);
  19.     queue<int> q;
  20.     q.push(s);
  21.     trace[s] = s;
  22.  
  23.     while (!q.empty()) {
  24.         int u = q.front();
  25.         q.pop();
  26.         for (int v : ke[u]) {
  27.             if (trace[v] < 0) {
  28.                 q.push(v);
  29.                 trace[v] = u;
  30.             }
  31.         }
  32.     }
  33.  
  34.     vector<int> path;
  35.     int u = t;
  36.     while (u != s) {
  37.         path.push_back(u);
  38.         u = trace[u];
  39.     }
  40.     path.push_back(s);
  41.     for (int i = 0, j = path.size()-1; i < j; i++, j--)
  42.         swap(path[i], path[j]);
  43.     return path;
  44. }
  45.  
  46. int bfs(int s, const vector<int> &pos, const dsk &ke) {
  47.     queue<int> q;
  48.     q.push(s);
  49.     visited[s] = true;
  50.  
  51.     int r = -1;
  52.     while (!q.empty()) {
  53.         int u = q.front(); q.pop();
  54.  
  55.         for (int v: ke[u]) {
  56.                 if (!visited[v] && pos[v] < 0) {
  57.                 q.push(v);
  58.                 visited[v] = true;
  59.             } else r = max(r, pos[v]);
  60.         }
  61.     }
  62.     return r;
  63. }
  64.  
  65. int main() {
  66. #ifdef LOCAL
  67.     freopen("in2.txt", "r", stdin);
  68. #else
  69.     freopen("STNODE - VOI09.inp", "r", stdin);
  70.     freopen("STNODE - VOI09.out", "w", stdout);
  71. #endif
  72.     ios_base::sync_with_stdio(false);
  73.     cin.tie(nullptr);
  74.  
  75.     int n, m, s, t;
  76.     cin >> n >> m >> s >> t;
  77.     dsk ke(n + 1);
  78.     for (int i = 1; i <= m; i++) {
  79.         int u, v; cin >> u >> v;
  80.         ke[u].push_back(v);
  81.     }
  82.  
  83.     path = find_path(s, t, ke);
  84.  
  85.     // Lưu lại vị trí của đỉnh trên đường đi
  86.     vector<int> pos(n + 1, -1);
  87.     for (int i = 0; i < path.size(); i++)
  88.         pos[path[i]] = i;
  89.  
  90.     int r = -1, res = 0;
  91.     for (int u : path) {
  92.         // Nếu từ các đỉnh trước u có thể đi tới một
  93.         // đỉnh sau u thì u không xung yếu, ngược lại
  94.         // u là nút xung yếu.
  95.         if (u != s && u != t && r <= pos[u]) res++;
  96.  
  97.         r = max(r, bfs(u, pos, ke));
  98.     }
  99.     cout << res << '\n';
  100.  
  101.     return 0;
  102. }
  103.  
  104.  
Advertisement
Add Comment
Please, Sign In to add comment