Tarjan 60pts求助,写了半个月了还没过
查看原帖
Tarjan 60pts求助,写了半个月了还没过
174118
Novaorbit楼主2021/11/8 19:13

RT

半个月来,0pts -> 50pts -> 40pts -> 60pts

反正就是A不了

求大佬帮忙看看,这会心态爆炸

代码如下

#include<bits/stdc++.h>
using namespace std;
int n,m,a[10001],head[10001],zhead[10001],cnt,col[10001],colt,dfn[10001],low[10001],poi,st[10001],nd[10001],rd[10001],mx;
bool vis[10001];
struct edge
{
	int v,next;
}e[100001],ze[100001];
inline int read()
{
	int x=0,k=1;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')
			k=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=(x<<3)+(x<<1)+(c^48);
		c=getchar();
	}
	return x*k;
}
inline void add(int u,int v)
{
	e[++cnt].v=v;
	e[cnt].next=head[u];
	head[u]=cnt;
}
inline void zadd(int u,int v)
{
	ze[++cnt].v=v;
	ze[cnt].next=zhead[u];
	zhead[u]=cnt;
}
inline int mn(int x,int y)
{
	return x<y ? x : y;
}
void tarjan(int u)
{
	dfn[u]=low[u]=++cnt;	st[++poi]=u;
	for(register int i=head[u];i;i=e[i].next)
	{
		if(!dfn[e[i].v])
		{
			tarjan(e[i].v);
			low[u]=mn(low[u],low[e[i].v]);
		}
		if(!col[e[i].v])	low[u]=mn(low[u],low[e[i].v]);	
	}
	if(dfn[u]==low[u])
	{
		col[u]=++colt;	
		while(st[poi]!=u)	col[st[poi]]=colt , nd[colt]+=a[st[poi]] , --poi;
		nd[colt]+=a[u];	--poi;
	}
}
void topu()
{
	int u;
	while(poi)
	{
		u=st[poi--];
		for(register int i=zhead[u];i;i=ze[i].next)
		{
			if(vis[ze[i].v])	continue;
			vis[ze[i].v]=true;
			nd[ze[i].v]+=nd[u];
			if(!(--rd[ze[i].v]))	st[++poi]=ze[i].v;
		}
	}
}
int main()
{
	n=read();	m=read();
	for(register int i=1;i<=n;++i)	a[i]=read();
	for(register int x,y,i=0;i<m;++i)	{	x=read();	y=read();	add(x,y);	}	cnt=0;
	for(register int i=1;i<=n;++i)	if(!dfn[i])	tarjan(i) , poi=0;	cnt=0;
	for(register int i=1;i<=n;++i)	
		for(register int j=head[i];j;j=e[j].next)
			if(col[i]!=col[e[j].v])	zadd(col[i],col[e[j].v]) , ++rd[col[e[j].v]];	poi=0;
	for(register int i=1;i<=colt;++i)
		if(!rd[i])	st[++poi]=i;
	topu();
	for(register int i=1;i<=colt;++i)	mx=mx>nd[i] ? mx : nd[i];
	cout<<mx;
	return 0;
}
2021/11/8 19:13
加载中...