Why
  • 板块灌水区
  • 楼主lcy0506
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/7/21 21:06
  • 上次更新2024/7/21 22:37:10
查看原帖
Why
1044302
lcy0506楼主2024/7/21 21:06
#include <bits/stdc++.h>
using namespace std;
int dfn[100001],low[100001],Time=0;
int stak[100001],top=0,tot=0,col[100001];
int sum[1000001],a[1000001],ru[1000001];
int que[1000001],f[1000001];
vector <int> e1[100001],e2[100001];
inline void tarjan(int x)
{
	int y;
	dfn[x]=low[x]=++Time;
	stak[++top]=x;
	for(int v : e1[x])
	{
		if(!dfn[v])//为什么这么写会CE
		{
			tarjan(v);
			low[x]=min(low[x],low[v]);
		}
		else if(!col[v])
		low[x]=min(low[x],dfn[v]);
	}
	if(dfn[x]==low[x])
	{
		col[x]=++tot;
		sum[tot]+=a[x];
		while(y=stak[top--],y!=x)
		{
			col[y]=tot;
			sum[tot]+=a[y];
		}
		--top;
	}
}
int main()
{
	int n,m,ans=0;
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(int i=1;i<=m;i++)
	{
		int x,y;
		cin>>x>>y;
		e1[x].push_back(y);
	}
	for(int i=1;i<=n;i++)
	{
		if(!dfn[i])
		tarjan(i);
	}
	for(int x=1;x<=n;x++)
	{
		for(int y:e1[x])
		{
			e2[col[x]].push_back(col[y]),++ru[col[y]];
		}
	}
	int qr=0;
	for(int x=1;x<=tot;x++)
	{
		if(!ru[x])
		que[++qr]=x,f[x]=sum[x];
	}
	for(int i=1,x;i<=qr;i++)
	{
		x=que[i];
		for(int y:e2[x])
		{
			f[y]=max(f[y],f[x]+sum[y]);
			if(!--ru[y])
			que[++qr]=y;
		}
	}
	for(int x=1;x<=tot;x++)
	{
		ans=max(ans,f[x]);
	}
	cout<<ans;
	return 0;
}
2024/7/21 21:06
加载中...