Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define _GLIBCXX_DEBUG
- #define _CRT_SECURE_NO_WARNINGS
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- typedef long double ld;
- #define mk make_pair
- #define inb push_back
- #define X first
- #define Y second
- #define all(v) v.begin(), v.end()
- #define sqr(x) (x) * (x)
- #define TIME 1.0 * clock() / CLOCKS_PER_SEC
- #define y1 AYDARBOG
- //continue break pop_back return scanf
- int solve();
- int main()
- {
- //ios_base::sync_with_stdio(0);
- //cin.tie(0);
- #define TASK ""
- #ifndef _DEBUG
- //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
- #endif
- solve();
- #ifdef _DEBUG
- fprintf(stderr, "\nTIME: %.3f\n", TIME);
- #endif
- }
- const int BUFSZ = (int)1e5 + 7;
- char buf[BUFSZ];
- string get_str()
- {
- scanf(" %s", buf);
- return string(buf);
- }
- const int M = 1e5 + 5;
- int n, m, s, f, k, VAL;
- vector<int> g[M];
- int was[M], us[M];
- int bfs()
- {
- queue<int> q;
- us[s] = 1;
- q.push(s);
- while(q.size())
- {
- int v = q.front();
- q.pop();
- for(auto to : g[v])
- if (!us[to])
- {
- us[to] = us[v] + 1;
- q.push(to);
- }
- }
- return us[f];
- }
- bool dfs(int x)
- {
- queue<int> q;
- q.push(k);
- for(int i = 1; i <= n; ++i) was[i] = 0;
- was[k] = 1;
- while(q.size())
- {
- int v = q.front();
- q.pop();
- for(auto to : g[v])
- if (!was[to] && was[v] + 1 <= x + 1)
- {
- was[to] = was[v] + 1;
- q.push(to);
- }
- }
- for(int i = 1; i <= n; ++i) us[i] = 0;
- if (!was[s]) us[s] = 1, q.push(s);
- while(q.size())
- {
- int v = q.front();
- q.pop();
- for(auto to : g[v])
- if (!us[to] && !was[to])
- {
- us[to] = us[v] + 1;
- q.push(to);
- }
- }
- return (us[f] != VAL);
- }
- int solve()
- {
- scanf("%d%d", &n, &m);
- for(int i = 0, a, b; i < m; ++i)
- {
- scanf("%d%d", &a, &b);
- g[a].inb(b);
- g[b].inb(a);
- }
- scanf("%d%d%d", &s, &f, &k);
- VAL = bfs();
- int l = 0, r = n;
- while(l < r)
- {
- int mid = (l + r) >> 1;
- if (dfs(mid)) r = mid;
- else l = mid + 1;
- }
- printf("%d\n", l);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement