Tarjan缩点+拓扑dp [USACO15JAN]Grass Cownoisseur G(洛谷 P3119)

[USACO15JAN]Grass Cownoisseur G

题目描述

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.

约翰有n块草场,编号1到n,这些草场由若干条单行道相连。奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草。

贝西总是从1号草场出发,最后回到1号草场。她想经过尽可能多的草场,贝西在通一个草场只吃一次草,所以一个草场可以经过多次。因为草场是单行道连接,这给贝西的品鉴工作带来了很大的不便,贝西想偷偷逆向行走一次,但最多只能有一次逆行。问,贝西最多能吃到多少个草场的牧草。

输入格式

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.

输入:

第一行:草场数n,道路数m。

以下m行,每行x和y表明有x到y的单向边,不会有重复的道路出现。

输出格式

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缩完点后变成DAG图,然后拓扑求 1 到 i (1<=i<=n) 的最大值,记为 dis1[i];然后反向建边,拓扑求 1 到 i (1<=i<=n) 的最大值,记为 dis2[i],这个就相当于求了 i 到 1 的最大值;

然后枚举每条边,例如 u–>v ,假如让 u–>v 反向,那么ans=max(dis2[u]+dis1[v]-w[fa[1]]);

这里求ans有个坑,就是一定要 dis1,2[i] 都要不为 0 ,也就是说都可到达 1 ,否则不能计算;

难点:难在想不到是枚举每条边让其方向;

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=200100;
const int M=1000100;
const LL mod=100000000;
int dfn[N],low[N],sta[N],tot,head[N],cnt,ans,top,L[N],R[N],fa[N],w[N];
int dis1[N],dis2[N],in[N];
int n,m;
bool vis[N];
struct Node{
	int to,nex;
}edge[M];
void add(int p,int q){
	edge[cnt].to=q;
	edge[cnt].nex=head[p];
	head[p]=cnt++;
}
void Tarjan(int p){
	dfn[p]=++tot,low[p]=tot;
	if(!vis[p]) vis[p]=true,sta[++top]=p;
	for(int i=head[p];~i;i=edge[i].nex){
		int q=edge[i].to;
		if(!dfn[q]){
			Tarjan(q);
			low[p]=min(low[p],low[q]);
		}
		else if(vis[q]) low[p]=min(low[p],dfn[q]);
	}
	if(dfn[p]==low[p]){
		vis[p]=false,fa[p]=p,w[p]=1;
		while(sta[top]!=p){
			fa[sta[top]]=p;
			vis[sta[top]]=false;
			w[p]++;
			top--;
		}
		top--;
	}
}
void bfs(int p,int q){
	queue<int>qu;
	for(int i=1;i<=n;i++){
		if(fa[i]==i&&in[i]==0) qu.push(i);
	}
	if(q==1) dis1[p]=w[p];
	else if(q==2) dis2[p]=w[p];
	while(!qu.empty()){
		int u=qu.front();
		qu.pop();
		for(int i=head[u];~i;i=edge[i].nex){
			int v=edge[i].to;
			if(q==1&&dis1[u]) dis1[v]=max(dis1[u]+w[v],dis1[v]);
			else if(q==2&&dis2[u]) dis2[v]=max(dis2[u]+w[v],dis2[v]);
			in[v]--;
			if(in[v]==0) qu.push(v);
		}
	}
}
int main(){
	memset(head,-1,sizeof(head));
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++){
		scanf("%d%d",&L[i],&R[i]);
		add(L[i],R[i]);
	} 
	for(int i=1;i<=n;i++){
		if(!dfn[i]) Tarjan(i);
	}
	
	cnt=0;
	memset(head,-1,sizeof(head));
	for(int i=1;i<=m;i++){//缩点 
		if(fa[L[i]]!=fa[R[i]]){
			in[fa[R[i]]]++;//入度 
			add(fa[L[i]],fa[R[i]]);
		}
	}
	bfs(fa[1],1);//1-i的最长路径 
	
	cnt=0;
	memset(in,0,sizeof(in));
	memset(head,-1,sizeof(head));//重新反向建边 
	for(int i=1;i<=m;i++){//缩点 
		if(fa[L[i]]!=fa[R[i]]){
			in[fa[L[i]]]++;//入度 
			add(fa[R[i]],fa[L[i]]);
		}
	}
	bfs(fa[1],2);//i-1的最长路径
	
	ans=w[fa[1]];
	for(int i=1;i<=m;i++){
		if(fa[L[i]]!=fa[R[i]]){
			int u=fa[L[i]],v=fa[R[i]];
			if(dis1[v]&&dis2[u]) ans=max(ans,dis2[u]+dis1[v]-w[fa[1]]);
		}
	}
	cout<<ans<<endl;
    return 0;
}

发布了264 篇原创文章 · 获赞 46 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44291254/article/details/105005330
今日推荐