In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow paths all over his farm. The farm consists of N fields, conveniently numbered 1..N, with each one-way cow path connecting a pair of fields. For example, if a path connects from field X to field Y, then cows are allowed to travel from X to Y but not from Y to X.

Bessie the cow, as we all know, enjoys eating grass from as many fields as possible. She always starts in field 1 at the beginning of the day and visits a sequence of fields, returning to field 1 at the end of the day. She tries to maximize the number of distinct fields along her route, since she gets to eat the grass in each one (if she visits a field multiple times, she only eats the grass there once).

As one might imagine, Bessie is not particularly happy about the one-way restriction on FJ’s paths, since this will likely reduce the number of distinct fields she can possibly visit along her daily route. She wonders how much grass she will be able to eat if she breaks the rules and follows up to one path in the wrong direction. Please compute the maximum number of distinct fields she can visit along a route starting and ending at field 1, where she can follow up to one path along the route in the wrong direction. Bessie can only travel backwards at most once in her journey. In particular, she cannot even take the same path backwards twice.

INPUT: (file grass.in)

The first line of input contains N and M, giving the number of fields and the number of one-way paths (1 <= N, M <= 100,000).

The following M lines each describe a one-way cow path. Each line contains two distinct field numbers X and Y, corresponding to a cow path from X to Y. The same cow path will never appear more than once.

OUTPUT: (file grass.out)

A single line indicating the maximum number of distinct fields Bessie

can visit along a route starting and ending at field 1, given that she can

follow at most one path along this route in the wrong direction.

四句话题解(非常简单):

Tarjan瞎缩一波点

然后单原SPFA求点1能到的点的答案贡献

然后跑单汇SPFA求能到点1的点的答案贡献

最后枚举反边判相连求答案

// luogu-judger-enable-o2
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<iterator>
#include<queue>
using namespace std;

struct ed
{
	int pre,to;
}edge[100010]={0};
int nodenum,edgenum,pointer[100010]={0},at=1,fx,tx;

int dfn[100010]={0},low[100010]={0},stackx[100010]={0},headx=0,tstamp=0,cache1;
bool instack[100010]={0};

int sccnum=0,belong[100010]={0},nodev[100010]={0};

ed edge1[100010]={0},edge2[100010]={0};
int at1=1,pt1[100010]={0},at2=1,pt2[100010]={0};
queue<int> que;
int mdp[100010]={0},mdn[100010]={0};

inline void readx(int& x)
{
	x=0; int k=1; register char ch=0;
	while (ch<'0' || ch>'9') { ch=getchar(); if (ch=='-') k=-1; }
	while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
	x*=k;
}

inline void Insert()
{
	edge[at].pre=pointer[fx];
	edge[at].to=tx;
	pointer[fx]=at;
	at++;
}

inline void Tarjan(int u)
{
	dfn[u]=low[u]=++tstamp; stackx[++headx]=u; instack[u]=true;
	int prex=pointer[u];
	while (prex)
	{
		if (!dfn[edge[prex].to])
		{
			Tarjan(edge[prex].to);
			low[u]=min(low[u],low[edge[prex].to]);
		}
		else if (instack[edge[prex].to]) low[u]=min(low[u],dfn[edge[prex].to]);
		prex=edge[prex].pre;
	}
	
	if (dfn[u]==low[u])
	{
		sccnum++;
		do
		{
			cache1=stackx[headx--];
			instack[cache1]=false; nodev[sccnum]++;
			belong[cache1]=sccnum;
		} while (cache1!=u);
	}
}

inline void Shrink()
{
	int prex=0;
	for (int i=1;i<=nodenum;i++)
	{
		prex=pointer[i];
		while (prex)
		{
			if (belong[edge[prex].to]!=belong[i])
			{
				edge1[at1].pre=pt1[belong[i]];
				edge1[at1].to=belong[edge[prex].to];
				pt1[belong[i]]=at1;
				at1++;
				
				edge2[at2].pre=pt2[belong[edge[prex].to]];
				edge2[at2].to=belong[i];
				pt2[belong[edge[prex].to]]=at2;
				at2++;
			}
			prex=edge[prex].pre;
		}
	}
}

inline void LPFA()
{
	int prex; memset(instack,0,sizeof(instack));
	
	que.push(belong[1]); mdp[belong[1]]=nodev[belong[1]];
	while (!que.empty())
	{
		cache1=que.front(); que.pop();
		prex=pt1[cache1]; instack[cache1]=false;
		while (prex)
		{
			if (mdp[edge1[prex].to]<mdp[cache1]+nodev[edge1[prex].to])
			{
				mdp[edge1[prex].to]=mdp[cache1]+nodev[edge1[prex].to];
				if (!instack[edge1[prex].to])
				{
					instack[edge1[prex].to]=true;
					que.push(edge1[prex].to);
				}
			}
			prex=edge1[prex].pre;
		}
	}
	
	memset(instack,0,sizeof(instack));
	que.push(belong[1]); mdn[belong[1]]=nodev[belong[1]];
	while (!que.empty())
	{
		cache1=que.front(); que.pop();
		prex=pt2[cache1]; instack[cache1]=false;
		while (prex)
		{
			if (mdn[edge2[prex].to]<mdn[cache1]+nodev[edge2[prex].to])
			{
				mdn[edge2[prex].to]=mdn[cache1]+nodev[edge2[prex].to];
				if (!instack[edge2[prex].to])
				{
					instack[edge2[prex].to]=true;
					que.push(edge2[prex].to);
				}
			}
			prex=edge2[prex].pre;
		}
	}
}

int main()
{
	readx(nodenum); readx(edgenum);
	for (int i=1;i<=edgenum;i++)
	{
		readx(fx); readx(tx);
		Insert();
	}
	
	for (int i=1;i<=nodenum;i++) if (!dfn[i]) Tarjan(i);
	
	Shrink();
	LPFA();
	
	int prexx=0,ans=nodev[belong[1]];
	for (int i=1;i<=nodenum;i++)
	{
		prexx=pointer[i];
		while (prexx)
		{
			if (belong[i]!=belong[edge[prexx].to] && mdn[belong[i]] && mdp[belong[edge[prexx].to]])
				ans=max(ans,mdp[belong[edge[prexx].to]]+mdn[belong[i]]-nodev[belong[1]]);	
			prexx=edge[prexx].pre;
		}
	}
	
	printf("%d\n",ans);
	return 0;
}

 


发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.