#include <bits/stdc++.h>//gplt2020-haut-8522d9
using namespace std;
typedef long long LL;
const LL maxn=4e5+10;
const LL inf=1e17;
LL n,m;
LL a[maxn],b[maxn];
vector<LL> G[maxn];
struct node{
LL u,v;
}p[maxn];
LL vis[maxn];
LL ko;
LL dfn[maxn],low[maxn],cnt,bcc[maxn];
stack<LL> st;
LL ins[maxn],scc;
void tarjan(LL u)
{
dfn[u]=low[u]=++cnt;
st.push(u);
ins[u]=1;
LL len=G[u].size();
for(LL i=0;i<len;i++){
LL to=G[u][i];
if(!dfn[to]){
tarjan(to);
low[u]=min(low[u],low[to]);
}
else
if(ins[to]){
low[u]=min(low[u],dfn[to]);
}
}
if(dfn[u]==low[u]){
++scc;
while(1){
LL pre=st.top();
st.pop();
bcc[pre]=scc;
ins[pre]=0;
if(pre==u){
break;
}
}
}
}
bool judge(LL mid)
{
for(LL i=0;i<=2*n;i++){
G[i].clear();
}
for(LL i=1;i<=m;i++){
LL u=p[i].u,v=p[i].v;
if(abs(a[u]-a[v])>mid){
G[2*u-1].push_back(2*v);
G[2*v-1].push_back(2*u);
}
if(abs(a[u]-b[v])>mid){
G[2*u-1].push_back(2*v-1);
G[2*v].push_back(2*u);
}
if(abs(b[u]-a[v])>mid){
G[2*u].push_back(2*v);
G[2*v-1].push_back(2*u-1);
}
if(abs(b[u]-b[v])>mid){
G[2*u].push_back(2*v-1);
G[2*v].push_back(2*u-1);
}
}
for(LL i=0;i<=2*n;i++){
dfn[i]=low[i]=ins[i]=0;
bcc[i]=0;
}
while(!st.empty()){
st.pop();
}
cnt=ko=0;
scc=0;
for(LL i=1;i<=2*n;i++){
if(!dfn[i]){
tarjan(i);
}
}
for(int i=1;i<=n;i++){
if(bcc[2*i]==bcc[i*2-1]){
ko++;break;
}
}
if(ko){
return false;
}
return true;
}
int main()
{
scanf("%lld%lld",&n,&m);
for(LL i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
for(LL i=1;i<=n;i++){
scanf("%lld",&b[i]);
}
LL u,v;
for(LL i=1;i<=m;i++){
scanf("%lld%lld",&p[i].u,&p[i].v);
}
LL l=0,r=2e9;
LL ans=0;
while(l<=r){
LL mid=(l+r)>>1;
if(judge(mid)){
ans=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
printf("%lld\n",l);
return 0;
}
07-12
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交