10pts求调
查看原帖
10pts求调
580544
ananyzx楼主2024/4/30 20:20
#include<bits/stdc++.h>
using namespace std;
const int N=500010;
int n,m,k,cnt,times,id[N],q[N],f[N],w_scc[N];
int dfn[N],din[N],w[N],s[N],pre[2][N],low[N],minn[N],maxx[N];
bool inst[N];
stack<int> st;
struct Node{
	int from,to,ne;
}a[N<<1];

void tarjan(int x){
	low[x]=dfn[x]=++times;
	st.push(x);
	inst[x]=true;
	for(int i=pre[0][x];i;i=a[i].ne){
		int to=a[i].to;
		if(!dfn[to]){
			tarjan(to);
			low[x]=min(low[to],low[x]);
		}else if(inst[to]){
			low[x]=min(low[x],dfn[to]);
		}
	}
	if(low[x]==dfn[x]){
		int t=st.top();
		cnt++;
		do{
			t=st.top();
			st.pop();
			id[t]=cnt;
			inst[t]=false;
			minn[cnt]=min(minn[cnt],w[t]);
            maxx[cnt]=max(maxx[cnt],w[t]);
		}while(t!=x);
	}

	
}

void add(int x,int y,int pre[]){
	k++;
	a[k]={x,y,pre[x]};
	pre[x]=k;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&w[i]);
	int x,y,z;
	for(int i=1;i<=m;i++){
		scanf("%d%d",&z,&x,&y);
		add(x,y,pre[0]);
		if(z==2) add(y,x,pre[0]);
	}
	memset(minn,127,sizeof(minn));
    memset(maxx,-127,sizeof(maxx));
	for(int i=1;i<=n;i++){
		if(!dfn[i]) tarjan(i);
	}
	for(int i=1;i<=m;i++){
		if(id[a[i].from]!=id[a[i].to]){
			add(id[a[i].from],id[a[i].to],pre[1]);
			din[id[a[i].to]]++;
		}
	}
	
	int h=1,t=0;
	q[++t]=id[1];
	f[id[1]]=max(f[id[1]],maxx[id[1]]-minn[id[1]]);
	while(h<=t){
		int hh=q[h];
		for(int i=pre[1][hh];i;i=a[i].ne){
			din[a[i].to]--;
			minn[a[i].to] = min(minn[a[i].to],minn[hh]);
            f[a[i].to]=max(max(f[hh],maxx[a[i].to]-minn[a[i].to]),f[a[i].to]);
			if(!din[a[i].to]){
				q[++t]=a[i].to;
			}
		}
		h++;
	}
	int ans=0;
	for(int i=1;i<=cnt;i++){
		ans=max(f[i],ans);
	}
	printf("%d",ans);
	return 0;
}
2024/4/30 20:20
加载中...