bzoj 3887: [Usaco2015 Jan]Grass Cownoisseur(spfa+tarjan)

3887: [Usaco2015 Jan]Grass Cownoisseur

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 170   Solved: 89
[ Submit][ Status][ Discuss]

Description

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.
给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1)

Input

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

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.

Sample Input

7 10
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7

Sample Output

6

HINT

Source

[ Submit][ Status][ Discuss]

题解:tarjan+spfa

tarjan将图变成有向无环图,然后正着跑一遍spfa,求出1到当前连通块中间经过的最多的点数,在反向建图,求出每次连通块到1经过的最多的点数,然后枚举反置哪一条边,更新答案即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define N 200003
using namespace std;
int n,m;
int point[N],v[N],next[N],x[N],y[N],dis[N],dis1[N],can[N];
int belong[N],size[N],st[N],top,tot,cnt,sz,dfsn[N],ins[N],low[N];
void add(int x,int y)
{
	tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;
	//cout<<x<<" "<<y<<endl;
}
void tarjan(int x)
{
	dfsn[x]=low[x]=++sz; st[++top]=x;
	ins[x]=1;
	for (int i=point[x];i;i=next[i])
	 if (!dfsn[v[i]]) {
	 	tarjan(v[i]);
	 	low[x]=min(low[x],low[v[i]]);
	 }else if (ins[v[i]])  low[x]=min(low[x],dfsn[v[i]]);
	if (dfsn[x]==low[x]) {
		++cnt; int j;
		do{
			j=st[top--];
			belong[j]=cnt;
			size[cnt]++; ins[j]=0;
		}while (j!=x);
	}
}
void spfa(int dis[N])
{
	memset(dis,0,sizeof(dis));
	memset(can,0,sizeof(can));
    int t=belong[1]; dis[t]=size[t]; can[t]=1;
    queue<int> p;
    p.push(t);
    while (!p.empty()) {
    	int now=p.front(); p.pop();
    	for (int i=point[now];i;i=next[i])
    	 if (dis[v[i]]<dis[now]+size[v[i]])
    	 {
    	 	dis[v[i]]=dis[now]+size[v[i]];
    	 	if (!can[v[i]]) {
    	 		can[v[i]]=1;
    	 		p.push(v[i]);
			 }
		 }
		can[now]=0;
	}
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++) {
		scanf("%d%d",&x[i],&y[i]);
		add(x[i],y[i]);
	}
	for (int i=1;i<=n;i++)
	 if (!dfsn[i]) tarjan(i);
	//cout<<endl;
	tot=0;
	memset(point,0,sizeof(point));
	memset(next,0,sizeof(next));
	for (int i=1;i<=m;i++)
	 if (belong[x[i]]!=belong[y[i]])   add(belong[x[i]],belong[y[i]]);
	spfa(dis);
	//cout<<endl;
	tot=0;
	memset(point,0,sizeof(point));
	memset(next,0,sizeof(next));
	for (int i=1;i<=m;i++)
	 if (belong[x[i]]!=belong[y[i]])   add(belong[y[i]],belong[x[i]]);
	spfa(dis1);
	int ans=size[belong[1]];
	//for (int i=1;i<=cnt;i++)
	// cout<<size[i]<<" ";
	//cout<<endl;
	for (int i=1;i<=m;i++)
	 if (belong[x[i]]!=belong[y[i]]) {
	 	int t=belong[x[i]]; int t1=belong[y[i]];
	 	if (dis[t1]&&dis1[t]) ans=max(ans,dis[t1]+dis1[t]-size[belong[1]]);
	 }
	printf("%d\n",ans);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值