思路

  • 先判断从s开始是不是能走到t
  • 二分中位数的大小
    如果相邻的两个点都大于等于mid,中位数能达到mid
    如果大小大小交替走也成立

代码

// Problem: 美丽的路径
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/9985/A
// Memory Limit: 524288 MB
// Time Limit: 6000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
#define debug(a) cout<<#a<<":"<<a<<"\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=200010;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);

int n,m,s,t;
ll a[N];
bool st[N],vis[N];
vector<int> g[N];

void dfs(int u){
    st[u]=1;
    for(int v:g[u]){
        if(st[v]) continue;
        dfs(v);
    }
}

void dfs2(int u,ll mid){
    vis[u]=1;
    for(int v:g[u]){
        if(vis[v]) continue;
        if(a[u]>=mid&&a[v]<mid) dfs2(v,mid);
        else if(a[v]>=mid&&a[u]<mid) dfs2(v,mid);
    }
}

bool check(ll mid){
    rep(u,1,n){
        vis[u]=0;
        if(st[u]&&a[u]>=mid){
            for(int v:g[u]){
                if(a[v]>=mid) return 1;
            }
        }
    }
    dfs2(s,mid);
    return vis[t];
}

void solve(){
    cin>>n>>m>>s>>t;
    rep(i,1,n) cin>>a[i],g[i].clear(),st[i]=0;
    while(m--){
        int u,v;cin>>u>>v;
        g[u].pb(v),g[v].pb(u);
    }
    dfs(s);
    if(!st[t]){
        cout<<"NO\n";
        return;
    }
    ll l=0,r=1e9,ans;
    while(l<=r){
        ll mid=(l+r)>>1;
        if(check(mid)) ans=mid,l=mid+1;
        else r=mid-1;
    }
    cout<<"YES\n"<<ans<<"\n";
}


int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int t;cin>>t;while(t--)
    solve();
    return 0;
}