#include <bits/stdc++.h>
using namespace std;
const int maxn = 200001;
/*const int logn = 18;
int p[logn+1][maxn], h[maxn];
int H[maxn];
int d[maxn];
*/
vector<int> adjlist[maxn];
/*
bitset<maxn> vis;
void dfs(int x){
if (vis[x]) return;
vis[x] = 1;
for (int k = 0; k < logn; k++){
if (p[k][x] == -1) break;
p[k+1][x] = p[k][p[k][x]];
}
for (auto v : adjlist[x]){
if (vis[v]) continue;
//printf("%d -> %d\n",x,v);
p[0][v] = x;
d[v] = d[x]+1;
h[v] = h[x]+1;
dfs(v);
}
}
int lca(int a, int b){
if (h[a] > h[b]) swap(a,b);
for (int k = 0, d= h[b] - h[a]; k < logn; k++){
if (d & (1<<k)) b = p[k][b];
}
if (a==b) return a;
assert(h[a] == h[b]);
for (int k = logn-1; k >=0; --k){
if (p[k][a] != p[k][b])
a = p[k][a], b = p[k][b];
assert(p[0][a] == p[0][b]);
return p[0][a];
}
}
int anc(int x, int d){
for (int k = 0; k <= logn && x != -1; k++){
if (d & (1<<k)) x = p[k][x];
}
return x;
}
int dist(int a, int b){
return d[a] + d[b] - 2*d[lca(a,b)];
}*/
int ds[maxn][4];
void bfs(int u, int k){
queue<int> q;
q.push(u);
ds[u][k] = 0;
while (q.size()){
int x = q.front(); q.pop();
for (auto v :adjlist[x]){
if (ds[v][k] == -1){
ds[v][k] = ds[x][k]+1;
q.push(v);
}
}
}
}
int main(){
int n,e; scanf("%d%d",&n,&e);
int s0, sn, serve1, serve2;
scanf("%d%d%d%d",&s0,&sn,&serve1,&serve2);
s0--, sn--, serve1--, serve2--;
adjlist[serve1].push_back(serve2);
adjlist[serve2].push_back(serve1);
for (int i = 0; i < e; i++){
int a,b;
scanf("%d%d",&a,&b);
if ((a == serve1 && b == serve2) || (a==serve2 && b == serve1)) continue;
a--; b--;
adjlist[a].push_back(b);
adjlist[b].push_back(a);
}
memset(ds,-1,sizeof(ds));
bfs(s0,0);
bfs(sn,1);
//dfs(s0);
//printf("%d %d %d\n",sn,serve1,lca(sn,serve1));
//if (lca(sn,serve1) == s0){
// printf("%d",d[sn]);
//}
//else{
int dist0 = max(ds[serve1][0],ds[serve2][0]);
int distn = min(ds[serve1][1],ds[serve2][1]);
//printf("%d %d\n",dist0,distn);
if (dist0 > distn) printf("-1");
else printf("%d",distn-dist0);
//}
}
Compilation message
007.cpp: In function 'int main()':
007.cpp:67:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n,e; scanf("%d%d",&n,&e);
~~~~~^~~~~~~~~~~~~~
007.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d",&s0,&sn,&serve1,&serve2);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.cpp:75:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&a,&b);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8192 KB |
Output is correct |
2 |
Partially correct |
10 ms |
8192 KB |
Partially correct |
3 |
Correct |
9 ms |
8192 KB |
Output is correct |
4 |
Incorrect |
9 ms |
8192 KB |
Output isn't correct |
5 |
Incorrect |
9 ms |
8192 KB |
Output isn't correct |
6 |
Correct |
9 ms |
8192 KB |
Output is correct |
7 |
Correct |
10 ms |
8192 KB |
Output is correct |
8 |
Incorrect |
11 ms |
8192 KB |
Output isn't correct |
9 |
Correct |
12 ms |
8192 KB |
Output is correct |
10 |
Correct |
9 ms |
8192 KB |
Output is correct |
11 |
Correct |
10 ms |
8192 KB |
Output is correct |
12 |
Incorrect |
9 ms |
8192 KB |
Output isn't correct |
13 |
Correct |
9 ms |
8192 KB |
Output is correct |
14 |
Incorrect |
12 ms |
8192 KB |
Output isn't correct |
15 |
Correct |
10 ms |
8192 KB |
Output is correct |
16 |
Incorrect |
11 ms |
8192 KB |
Output isn't correct |
17 |
Incorrect |
9 ms |
8192 KB |
Output isn't correct |
18 |
Incorrect |
11 ms |
8192 KB |
Output isn't correct |
19 |
Correct |
11 ms |
8192 KB |
Output is correct |
20 |
Correct |
11 ms |
8192 KB |
Output is correct |
21 |
Correct |
10 ms |
8192 KB |
Output is correct |
22 |
Correct |
11 ms |
8192 KB |
Output is correct |
23 |
Correct |
11 ms |
8192 KB |
Output is correct |
24 |
Incorrect |
12 ms |
8192 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
9848 KB |
Output is correct |
2 |
Incorrect |
59 ms |
10744 KB |
Output isn't correct |
3 |
Correct |
44 ms |
9976 KB |
Output is correct |
4 |
Incorrect |
81 ms |
10744 KB |
Output isn't correct |
5 |
Correct |
32 ms |
9728 KB |
Output is correct |
6 |
Correct |
48 ms |
9976 KB |
Output is correct |
7 |
Correct |
39 ms |
10240 KB |
Output is correct |
8 |
Correct |
41 ms |
10192 KB |
Output is correct |
9 |
Incorrect |
55 ms |
11000 KB |
Output isn't correct |
10 |
Correct |
221 ms |
16240 KB |
Output is correct |
11 |
Incorrect |
111 ms |
12032 KB |
Output isn't correct |
12 |
Correct |
110 ms |
13104 KB |
Output is correct |
13 |
Incorrect |
94 ms |
12372 KB |
Output isn't correct |
14 |
Correct |
74 ms |
11692 KB |
Output is correct |
15 |
Correct |
134 ms |
13048 KB |
Output is correct |
16 |
Correct |
125 ms |
13380 KB |
Output is correct |
17 |
Correct |
156 ms |
12792 KB |
Output is correct |
18 |
Incorrect |
162 ms |
12816 KB |
Output isn't correct |
19 |
Correct |
205 ms |
14920 KB |
Output is correct |
20 |
Incorrect |
305 ms |
17424 KB |
Output isn't correct |
21 |
Incorrect |
194 ms |
14708 KB |
Output isn't correct |
22 |
Correct |
181 ms |
14012 KB |
Output is correct |
23 |
Partially correct |
194 ms |
14784 KB |
Partially correct |
24 |
Correct |
171 ms |
14584 KB |
Output is correct |
25 |
Incorrect |
174 ms |
14328 KB |
Output isn't correct |
26 |
Partially correct |
158 ms |
14048 KB |
Partially correct |
27 |
Correct |
200 ms |
14712 KB |
Output is correct |
28 |
Correct |
201 ms |
14968 KB |
Output is correct |
29 |
Correct |
265 ms |
16304 KB |
Output is correct |
30 |
Incorrect |
359 ms |
17888 KB |
Output isn't correct |
31 |
Incorrect |
225 ms |
15892 KB |
Output isn't correct |
32 |
Correct |
223 ms |
14712 KB |
Output is correct |
33 |
Partially correct |
193 ms |
14940 KB |
Partially correct |
34 |
Incorrect |
210 ms |
15400 KB |
Output isn't correct |
35 |
Incorrect |
222 ms |
15048 KB |
Output isn't correct |
36 |
Incorrect |
239 ms |
15224 KB |
Output isn't correct |
37 |
Correct |
246 ms |
16456 KB |
Output is correct |
38 |
Correct |
198 ms |
16132 KB |
Output is correct |
39 |
Correct |
255 ms |
16120 KB |
Output is correct |
40 |
Incorrect |
293 ms |
17912 KB |
Output isn't correct |
41 |
Correct |
406 ms |
20220 KB |
Output is correct |