TLE on#2、3、10,81pts求调教
查看原帖
TLE on#2、3、10,81pts求调教
1057535
_IX_楼主2024/12/7 11:24

RT

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e5+10;
const int INF=0x3f3f3f3f;
int n,m,k,s,x,y,z,cnt,ans,q,a[N],b[N],low[N],dfn[N],scc[N];
bool b1[N],b2[N],b3[N];
vector<int>v1[N],v2[N];
stack<int>s1;
int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){
            f=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9){
        write(x/10);
    }
    putchar(x%10+'0');
    return;
}
void tarjan(int u){
	dfn[u]=low[u]=++q;
	s1.push(u);
	for(int i=0;i<v1[u].size();i++){
		int v=v1[u][i];
		if(!dfn[v]){
		    tarjan(v);
		    low[u]=min(low[u],low[v]);
		}else{
		    if(!scc[v]){
				low[u]=min(low[u],dfn[v]);
			}
		}
	}
	if(dfn[u]==low[u]){
		cnt++;
		int v;
		while(1){
			v=s1.top();
			s1.pop();
			scc[v]=cnt;
			b[cnt]+=a[v];
			if(b1[v]){
				b2[cnt]=true;
			}
			if(v==s){
				z=cnt;
			}
			if(u==v){
				break;
			}
		}
	}
}
void dfs(int u,int tot){
    tot+=b[u];
    b3[u]=true;
    if(b2[u]){
        ans=max(ans,tot);
    }
    for(int i=0;i<v2[u].size();i++){
        int v=v2[u][i];
        if(!b3[v]){
			dfs(v,tot);
		}
    }
    b3[u]=false;
}
signed main(){
	n=read();
	m=read();
	for(int i=1;i<=m;i++){
	    x=read();
	    y=read();
	    v1[x].push_back(y);
	}
	for(int i=1;i<=n;i++){
	    a[i]=read();
	}
	s=read();
	k=read();
	for(int i=1;i<=k;i++){
	    x=read();
	    b1[x]=1;
	}
	for(int i=1;i<=n;i++){
	    if(!dfn[i]){
			tarjan(i);
		}
	}
	for(int i=1;i<=n;i++){
	    for(int j=0;j<v1[i].size();j++){
	        int u=i,v=v1[i][j];
	        if(scc[u]!=scc[v]){
	            v2[scc[u]].push_back(scc[v]);
	        }
	    }
	}
	dfs(z,0);
	write(ans);
	return 0;
}
2024/12/7 11:24
加载中...