Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID: wtfalex2
- LANG: C++14
- PROB: castle
- //9cz2b944
- */
- #define _CRT_SECURE_NO_WARNINGS
- #pragma comment(linker, "/STACK:259000000")
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <vector>
- #include <ctime>
- #include <map>
- #include <set>
- #include <string>
- #include <queue>
- #include <deque>
- #include <cassert>
- #include <cstdlib>
- #include <bitset>
- #include <algorithm>
- #include <string>
- #include <list>
- #include <fstream>
- #include <cstring>
- #include <climits>
- #include <stack>
- #include <unordered_map>
- #include <unordered_set>
- using namespace std;
- typedef unsigned long long ull;
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- typedef pair<string, int> psi;
- typedef vector<string> vs;
- typedef pair<ll, ll> pll;
- typedef vector<ll> vll;
- typedef vector<char> vc;
- typedef vector<pii> vpii;
- typedef vector<pair<ll, ll> > vpll;
- typedef long double ld;
- #define forn(i, n) for (int i = 0; i < (int)n; i++)
- #define for1(i, n) for (int i = 1; i <= (int)n; i++)
- #define forq(i, s, t) for (int i = s; i <= t; i++)
- #define ford(i, s, t) for (int i = s; i >= t; i--)
- #define mk make_pair
- #define inb push_back
- #define outb pop_back
- #define all(v) v.begin(), v.end()
- #define X first
- #define Y second
- #define TIME clock() * 1.0 / CLOCKS_PER_SEC
- #define sqr(x) (x) * (x)
- //#define double long double
- #define y1 amdknkgsdaasdwapgnpikn
- //
- #define mp make_pair
- #define pb push_back
- #define XX first
- #define YY second
- //
- const ld EPS = 1e-9;
- const ld pi = acos(-1.0);
- const int MAXN = (int)1e5 + 7;
- const ll INF = 2e9 + 7;
- const ll LINF = (ll)7e18;
- const ll MOD = (ll)1e9 + 7;
- const int CHASH = (ll)239017;
- const ld DINF = (ld)1000000000000000.0;
- void mkfile();
- int solve();
- int main()
- {
- #define TASK "rblock"
- #ifdef _DEBUG
- mkfile();
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- freopen("test.txt", "w", stderr);
- ld tstart = TIME;
- srand(2299);
- #else
- //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
- //freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout), freopen("test.txt", "w", stderr);
- srand(2299);
- #endif
- solve();
- #ifdef _DEBUG
- ld tend = TIME;
- cerr << tend - tstart << " s.\n";
- #endif
- return 0;
- }
- void mkfile()
- {
- freopen("input.txt", "a+", stdout);
- srand(time(0));
- return;
- }
- bool equal(double a, double b)
- {
- return abs(a - b) < EPS;
- }
- int n, m, s, f, r;
- int ds[MAXN], d[MAXN], dp[MAXN];
- vi gs[MAXN], g[MAXN];
- int was[MAXN];
- void bfss()
- {
- queue<int> q;
- q.push(s);
- ds[s] = 0;
- while (!q.empty())
- {
- int u = q.front();
- q.pop();
- for (auto to : gs[u])
- {
- if (ds[to] == INF)
- {
- ds[to] = ds[u] + 1;
- q.push(to);
- }
- }
- }
- }
- void dfsup(int u)
- {
- was[u] = 1;
- for (auto to : gs[u])
- {
- if (ds[u] - 1 == ds[to])
- {
- g[u].inb(to);
- g[to].inb(u);
- if (!was[to])
- dfsup(to);
- }
- }
- }
- void bfsr()
- {
- queue<int> q;
- q.push(r);
- was[r] = 1;
- d[r] = 0;
- while (!q.empty())
- {
- int u = q.front();
- q.pop();
- for (auto to : gs[u])
- {
- if (!was[to])
- {
- d[to] = d[u] + 1;
- was[to] = 1;
- q.push(to);
- }
- }
- }
- }
- void calcdp()
- {
- queue<int> q;
- q.push(s);
- was[s] = 1;
- while (!q.empty())
- {
- int u = q.front();
- q.pop();
- int mxval = -1;
- int f = 0;
- for (auto to : g[u])
- {
- if (ds[to] + 1 == ds[u])
- {
- mxval = max(mxval, dp[to]);
- f = 1;
- }
- if (ds[to] - 1 == ds[u])
- {
- if (!was[to])
- {
- was[to] = 1;
- q.push(to);
- }
- }
- }
- if (f)
- dp[u] = min(mxval, dp[u]);
- }
- }
- int solve()
- {
- scanf("%d %d", &n, &m);
- forn(i, m)
- {
- int x, y;
- scanf("%d %d", &x, &y);
- --x, --y;
- gs[x].inb(y), gs[y].inb(x);
- }
- scanf("%d %d %d", &s, &f, &r);
- --s, --f, --r;
- fill(ds, ds + n, INF);
- fill(d, d + n, INF);
- bfss();
- memset(was, 0, sizeof(was));
- dfsup(f);
- memset(was, 0, sizeof(was));
- bfsr();
- memset(was, 0, sizeof(was));
- forn(i, n)
- dp[i] = d[i];
- calcdp();
- printf("%d\n", dp[f]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement