求助,求助,求助
查看原帖
求助,求助,求助
191993
six_小6猪楼主2021/2/3 21:52
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
using namespace std;
int n,m; 
int dfn[10009],low[10009]; 
bool pd[10009];
struct node{
	int to,nxt,from;
}e[100006];
node pt2[10009];
struct mn{
	int ha,val;
}pt[10009];
int cnt;
stack<int > q;
int head[10009];
void add(int x,int y)
{
	e[++cnt].to=y;
	e[cnt].nxt=head[x];
	head[x]=cnt;
}
int head2[100009];
int cnt2;
void add2(int a,int b)
{
	pt2[cnt2].to=b;
	pt2[++cnt2].nxt=head2[a];
	head2[a]=cnt;	
}
int tot;
int color[10005],du[10005];
int mo[10003];
int tim;
void tarjan(int x)
{
	pd[x]=1;
	q.push(x);
	low[x]=dfn[x]=++tim;
	for(int i=head[x];i;i=e[i].nxt)
	  {
	  	int to=e[i].to;
	  	if(!dfn[to])
	  	{
	  		tarjan(to);
	  		low[x]=min(low[to],low[x]);
		}
		else if(pd[to])
		  low[x]=min(low[x],dfn[to]);
	  }
	  int k;
	  if(dfn[x]==low[x])
	  {
	  	++tot;
	  	do{
	  	    k=q.top();
	  		q.pop();
	  		pd[k]=0;
	  		color[k]=tot;
	  		mo[tot]+=pt[k].val; 
	    } while(x!=k);
	  }
}

int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	  {
	  	cin>>pt[i].val;
	  	pt[i].ha=i; 
	  }
	for(int i=1;i<=n;i++)
	  {
	    int x,y;
	  	cin>>x>>y;
	  	add(x,y);
	  }
	  for(int i=1;i<=n;i++)
	    if(!dfn[i])
	      tarjan(i);
	for(int i=1;i<=n;i++)
	  for(int j=head[i];j;j=e[j].nxt)
	    	if(color[i]!=color[e[j].to])
	    	  {
	    	  	add2(color[i],color[e[j].to]);
	    	  	du[color[e[j].to]]++;
			  }
	queue<int > s; 
	for(int i=1;i<=tot;i++)
		if(du[i]==0)
		  s.push(i);
	while(!s.empty())
	  {
	  	
	  	int x=s.front();
	  	s.pop();
	  	for(int i=head2[x];i;i=pt2[i].nxt)
	  	  {
	  	  	int y=pt2[i].to;
	  	  	mo[y]+=mo[x];
	  	  	q.push(x);
		  }
	  } 
	int maxn=-0x3f3f3f;
	for(int i=1;i<=tot;i++)
	  maxn=max(maxn,mo[i]);
	printf("%d",maxn);
} 
2021/2/3 21:52
加载中...