使模最大就是找某条路径上的严格次大值,由于一个点可以通过多次,那门可以tarjan缩点,处理出每个联通快的最大值与次小值,剩下就是在DAG图上bfs计算答案。(注意次小值需在最大值路径上)
#include<bits/stdc++.h>
using namespace std;
const int M=4000002,N=400002;
queue<int>q;
int tim,scc,dfn[N],low[N],head[N],st[N],top,fl[N],bl[N],a[N],tot,
E1[N],E2[N],mx1[N],mx2[N],mx3[N],in[N],s,vis[N],du[N],n,m,Q,i,x,y,j;
struct node{
int to,ne,from,pre;
}E[M],e[M];
inline char gc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int read(){
int x=0,fl=1;char ch=gc();
for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
return x*fl;
}
inline void wri(int a){if(a<0)a=-a,putchar('-');if(a>=10)wri(a/10);putchar(a%10|48);}
void add(int x,int y){
E[++tot]=(node){y,head[x],0,0};
head[x]=tot;
}
void add2(int x,int y){
e[++tot]=(node){y,E1[x],x,E2[y]};
E1[x]=E2[y]=tot;
}
void update(int x,int y){
if (y>mx1[x]) mx2[x]=mx1[x],mx1[x]=y;
else if (y>mx2[x] && y!=mx1[x]) mx2[x]=y;
}
void tarjan(int u){
low[u]=dfn[u]=++tim;
st[top++]=u,in[u]=1;
for (int i=head[u],v;i;i=E[i].ne)
if (!dfn[v=E[i].to]) tarjan(v),low[u]=min(low[u],low[v]);
else if (in[v]) low[u]=min(low[u],dfn[v]);
if (low[u]==dfn[u]){
scc++;int x;
do{
x=st[--top];
bl[x]=scc;
in[x]=0;
update(scc,a[x]);
}while (x!=u);
}
}
void bfs(){
q.push(bl[s]);vis[bl[s]]=1;
while (!q.empty()){
int u=q.front();q.pop();fl[u]=1;
for (int i=E1[u],v;i;i=e[i].ne){
du[v=e[i].to]++;
if (!vis[v]) vis[v]=1,q.push(v);
}
}
q.push(bl[s]);
while (!q.empty()){
int u=q.front();q.pop();
for (int i=E2[u],v;i;i=e[i].pre)
if (fl[v=e[i].from]){
if (mx1[v]>mx1[u]) mx3[u]=mx1[u],mx1[u]=mx1[v];
else if (mx1[v]<mx1[u] && mx1[v]>mx3[u]) mx3[u]=mx1[v];
if (mx3[v]>mx1[u]) mx3[u]=mx1[u],mx1[u]=mx3[v];
else if (mx3[v]<mx1[u] && mx3[v]>mx3[u]) mx3[u]=mx3[v];
}
for (int i=E1[u],v;i;i=e[i].ne)
if (fl[v=e[i].to]){
mx2[v]=max(mx2[u],mx2[v]);
if (mx1[u]!=mx1[v]) mx2[v]=max(mx2[v],min(mx1[u],mx1[v]));
else mx2[v]=max(mx2[v],max(mx3[u],mx3[v]));
if (!(--du[v])) q.push(v);
}
}
}
int main(){
n=read();m=read();Q=read();s=read();
for (i=1;i<=n;i++) a[i]=read();
for (i=1;i<=m;i++) x=read(),y=read(),add(x,y);
for (i=1;i<=n;i++)
if (!dfn[i]) tarjan(i);
tot=0;
for (i=1;i<=n;i++)
for (j=head[i];j;j=E[j].ne)
if (bl[i]!=bl[x=E[j].to]) add2(bl[i],bl[x]);
for (i=1;i<=scc;i++) mx3[i]=mx2[i];
bfs();
while (Q--){
x=read();
if (!fl[bl[x]]) wri(-1);
else wri(mx2[bl[x]]);
putchar(' ');
}
}