#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define lastbit(i) __builtin_ctz(i)
#define pil pair<int,long long>
const int maxn=2e5+9;
const long long inf=1e15;
long long b[maxn];
vector<int>a[maxn];
int n;
struct cus{
int u;
long long val;
bool operator < (const cus &p)const {
return val>p.val;
}
};
vector<long long> cal(int s){
vector<long long>f(n+1);
vector<bool>vis(n+1);
for1(i,1,n)f[i]=inf;
f[s]=0;
priority_queue<cus>t;
t.push({s,0});
while (!t.empty()){
auto u=t.top();
t.pop();
if (vis[u.u])continue;
vis[u.u]=1;
for (auto v:a[u.u]){
if (f[v]>max(b[v],f[u.u]+1)){
f[v]=max(b[v],f[u.u]+1);
t.push({v,f[v]});
}
}
}
return f;
}
long long ans[maxn];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int m;
cin>>n>>m;
for1(i,1,n)cin>>b[i];
for1(i,1,m){
int u,v;
cin>>u>>v;
a[u].pb(v);
a[v].pb(u);
}
vector<long long>tmp=cal(1);
for1(i,1,n){
ans[i]+=tmp[i];
}
tmp=cal(n);
long long vcl=inf;
for1(i,1,n){
if (abs(ans[i]-tmp[i])<=1)vcl=min(vcl,ans[i]+tmp[i]);
}
cout<<vcl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
57 ms |
17496 KB |
Output is correct |
3 |
Correct |
59 ms |
17488 KB |
Output is correct |
4 |
Correct |
60 ms |
17616 KB |
Output is correct |
5 |
Correct |
63 ms |
17488 KB |
Output is correct |
6 |
Correct |
61 ms |
17488 KB |
Output is correct |
7 |
Correct |
61 ms |
17488 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6616 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6676 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6744 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6744 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6616 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6488 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6676 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6744 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6744 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6748 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
3 ms |
6748 KB |
Output is correct |
29 |
Correct |
3 ms |
6748 KB |
Output is correct |
30 |
Correct |
3 ms |
7000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
57 ms |
17496 KB |
Output is correct |
3 |
Correct |
59 ms |
17488 KB |
Output is correct |
4 |
Correct |
60 ms |
17616 KB |
Output is correct |
5 |
Correct |
63 ms |
17488 KB |
Output is correct |
6 |
Correct |
61 ms |
17488 KB |
Output is correct |
7 |
Correct |
61 ms |
17488 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6616 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6488 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6676 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6744 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6744 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
2 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
2 ms |
6748 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
3 ms |
6748 KB |
Output is correct |
37 |
Correct |
3 ms |
6748 KB |
Output is correct |
38 |
Correct |
3 ms |
7000 KB |
Output is correct |
39 |
Correct |
110 ms |
17760 KB |
Output is correct |
40 |
Correct |
78 ms |
17488 KB |
Output is correct |
41 |
Correct |
84 ms |
17500 KB |
Output is correct |
42 |
Correct |
93 ms |
17744 KB |
Output is correct |
43 |
Correct |
78 ms |
17536 KB |
Output is correct |
44 |
Correct |
81 ms |
17572 KB |
Output is correct |
45 |
Correct |
85 ms |
17748 KB |
Output is correct |
46 |
Correct |
86 ms |
17744 KB |
Output is correct |
47 |
Correct |
83 ms |
17748 KB |
Output is correct |
48 |
Correct |
84 ms |
17744 KB |
Output is correct |
49 |
Correct |
126 ms |
17636 KB |
Output is correct |
50 |
Correct |
118 ms |
17552 KB |
Output is correct |
51 |
Correct |
103 ms |
17748 KB |
Output is correct |
52 |
Correct |
182 ms |
20228 KB |
Output is correct |
53 |
Correct |
191 ms |
20256 KB |
Output is correct |
54 |
Correct |
181 ms |
20280 KB |
Output is correct |
55 |
Correct |
175 ms |
20500 KB |
Output is correct |
56 |
Correct |
218 ms |
22992 KB |
Output is correct |
57 |
Correct |
226 ms |
23284 KB |
Output is correct |
58 |
Correct |
285 ms |
23076 KB |
Output is correct |
59 |
Correct |
293 ms |
23972 KB |
Output is correct |
60 |
Correct |
290 ms |
22988 KB |
Output is correct |
61 |
Correct |
298 ms |
22980 KB |
Output is correct |
62 |
Correct |
266 ms |
22992 KB |
Output is correct |
63 |
Correct |
274 ms |
22988 KB |
Output is correct |
64 |
Correct |
301 ms |
23168 KB |
Output is correct |
65 |
Correct |
268 ms |
22924 KB |
Output is correct |
66 |
Correct |
276 ms |
22976 KB |
Output is correct |
67 |
Correct |
315 ms |
23072 KB |
Output is correct |