#include <bits/stdc++.h>
#define ld long double
#define endl "\n"
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
//#define left oooooopss
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll b,ll e,ll m){
b%=m;
ll ans = 1;
for (; e; b = b * b % m, e /= 2)
if (e & 1) ans = ans * b % m;
return ans;
}
// debug:
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr<<"(" << #__VA_ARGS__<<"):" , dbg_out(__VA_ARGS__) , cerr << endl
#else
#define dbg(...) 42
#endif
//
const int N = 200100;
int n,m;
vi g[N];
vi dista(int a){
vector<int> d(n+2);
rep(i,1,n+1)d[i] = 1e9;
d[a] = 0;
priority_queue<pii,vector<pii>,greater<pii>> pq;
pq.push(pii(d[a],a));
while(!pq.empty()){
auto t = pq.top();pq.pop();
if(t.ff != d[t.ss])continue;
int v = t.ss;
for(auto to : g[v])if(d[to] > d[v] + 1){
d[to] = d[v] + 1;
pq.push(pii(d[to],to));
}
}
return d;
}
int s,d,a,b;
vi d1,d2;
int vis[N];
int getcommon(int ini){
rep(i,0,N)vis[i]=0;
queue<int> q;
q.push(ini);
vis[ini] = 1;
int res=0;
while(!q.empty()){
int v = q.front();
q.pop();
res=max(res,d1[ini] - d1[v]);
for(auto to : g[v])
if(!vis[to] && d1[to] == d1[v] - 1 && d2[to] == d2[v] - 1){
vis[to]=1;q.push(to);
}
}
return res;
}
int32_t main(){
fastio;
cin >> n >> m;
cin >> s >> d >> a >> b;
assert(a!=b);
while(m--){
int u,v;
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
d1 = dista(a),d2=dista(b);
int A = d1[d] - d1[s];
int B = d2[d] - d1[s];
if(A!=B){
cout << max(min(A,B),-1) << endl;
}else{
cout << max(A - ((A + getcommon(s) < getcommon(d)) ? 1 : 0),-1) << endl;
}
// math -> gcd it all
// Did u check N=1? Did you switch N,M?
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5728 KB |
Output is correct |
2 |
Partially correct |
3 ms |
4940 KB |
Partially correct |
3 |
Correct |
3 ms |
5708 KB |
Output is correct |
4 |
Correct |
3 ms |
5708 KB |
Output is correct |
5 |
Correct |
3 ms |
5708 KB |
Output is correct |
6 |
Correct |
3 ms |
5708 KB |
Output is correct |
7 |
Correct |
3 ms |
5708 KB |
Output is correct |
8 |
Correct |
3 ms |
5708 KB |
Output is correct |
9 |
Correct |
3 ms |
5708 KB |
Output is correct |
10 |
Correct |
4 ms |
5708 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5708 KB |
Output is correct |
13 |
Correct |
3 ms |
5708 KB |
Output is correct |
14 |
Correct |
3 ms |
5708 KB |
Output is correct |
15 |
Correct |
3 ms |
5708 KB |
Output is correct |
16 |
Correct |
3 ms |
5708 KB |
Output is correct |
17 |
Correct |
3 ms |
5708 KB |
Output is correct |
18 |
Correct |
5 ms |
5708 KB |
Output is correct |
19 |
Correct |
3 ms |
5708 KB |
Output is correct |
20 |
Correct |
4 ms |
5708 KB |
Output is correct |
21 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
22 |
Correct |
3 ms |
5836 KB |
Output is correct |
23 |
Correct |
4 ms |
5836 KB |
Output is correct |
24 |
Correct |
4 ms |
5836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
7400 KB |
Output is correct |
2 |
Correct |
28 ms |
8128 KB |
Output is correct |
3 |
Correct |
32 ms |
7520 KB |
Output is correct |
4 |
Correct |
27 ms |
8220 KB |
Output is correct |
5 |
Correct |
24 ms |
6476 KB |
Output is correct |
6 |
Correct |
19 ms |
6732 KB |
Output is correct |
7 |
Correct |
26 ms |
7720 KB |
Output is correct |
8 |
Correct |
27 ms |
7756 KB |
Output is correct |
9 |
Correct |
40 ms |
8028 KB |
Output is correct |
10 |
Correct |
124 ms |
12460 KB |
Output is correct |
11 |
Correct |
52 ms |
9348 KB |
Output is correct |
12 |
Correct |
68 ms |
10184 KB |
Output is correct |
13 |
Correct |
55 ms |
9636 KB |
Output is correct |
14 |
Correct |
47 ms |
8960 KB |
Output is correct |
15 |
Correct |
57 ms |
10308 KB |
Output is correct |
16 |
Correct |
66 ms |
9804 KB |
Output is correct |
17 |
Correct |
56 ms |
10020 KB |
Output is correct |
18 |
Correct |
59 ms |
10052 KB |
Output is correct |
19 |
Correct |
88 ms |
11140 KB |
Output is correct |
20 |
Correct |
154 ms |
14044 KB |
Output is correct |
21 |
Correct |
88 ms |
12224 KB |
Output is correct |
22 |
Correct |
84 ms |
11444 KB |
Output is correct |
23 |
Correct |
78 ms |
11284 KB |
Output is correct |
24 |
Correct |
74 ms |
11844 KB |
Output is correct |
25 |
Correct |
82 ms |
11540 KB |
Output is correct |
26 |
Correct |
77 ms |
10448 KB |
Output is correct |
27 |
Correct |
94 ms |
12228 KB |
Output is correct |
28 |
Correct |
100 ms |
12024 KB |
Output is correct |
29 |
Correct |
120 ms |
12696 KB |
Output is correct |
30 |
Correct |
162 ms |
14684 KB |
Output is correct |
31 |
Correct |
98 ms |
12932 KB |
Output is correct |
32 |
Correct |
101 ms |
12024 KB |
Output is correct |
33 |
Partially correct |
91 ms |
11416 KB |
Partially correct |
34 |
Correct |
105 ms |
12468 KB |
Output is correct |
35 |
Correct |
82 ms |
12264 KB |
Output is correct |
36 |
Correct |
86 ms |
12488 KB |
Output is correct |
37 |
Correct |
117 ms |
12612 KB |
Output is correct |
38 |
Correct |
117 ms |
13120 KB |
Output is correct |
39 |
Correct |
119 ms |
13260 KB |
Output is correct |
40 |
Correct |
159 ms |
14844 KB |
Output is correct |
41 |
Correct |
229 ms |
16996 KB |
Output is correct |