洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur tarjan缩点+反图SPFA求最长路

26 篇文章 0 订阅
17 篇文章 0 订阅

https://www.luogu.org/problem/P3119
题目描述
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.

输出:

一个数,逆行一次最多可以走几个草场。

思路: t a r j a n tarjan tarjan缩点是很容易想到的,缩点后我们得到了一个有向无环图,同时再建立这个图的反图,以点 1 1 1所在的强连通分量为起点对这个两个图跑 S P F A SPFA SPFA求最长路,设 d i s 1 [ i ] dis1[i] dis1[i]表示原图中从点 1 1 1所在的强联通分量到第 i i i个强连通分量的最长路, d i s 2 [ i ] dis2[i] dis2[i]表示反图中从起点 1 1 1所在的强联通分量到第 i i i个强联通分量的最长路,假设原图中存在一条有向边 v − > u v->u v>u,我们逆着走这条边即反转该边,则可以更新答案: a n s = m a x ( a n s , d i s 1 [ u ] + d i s 2 [ v ] − s i z [ i d [ 1 ] ] ) ans=max(ans,dis1[u]+dis2[v]-siz[id[1]]) ans=max(ans,dis1[u]+dis2[v]siz[id[1]]),表示路径: 1 − > u − > v − > 1 1->u->v->1 1>u>v>1,因为 1 1 1的贡献被计算了两次,所以要减去 1 1 1次。因为原图和反图不可能有重复的边,所以正确性可以得到保证(有重边的话缩点就把两个强连通分量连到一起了)。

#include<bits/stdc++.h>
using namespace std;

const int maxn=1e5+5;
const int maxm=1e5+5;

struct node
{
	int id,dis;
	node(){}
	node(int i,int d)
	{
		id=i,dis=d;
	}
	bool operator <(const node &a)const
	{
		return dis<a.dis;
	}
};
struct edge
{
    int to,nxt;
}Edge[maxm];

int head_sd[maxn],revhead[maxn];
edge sd[maxm],revsd[maxm];
int dis1[maxn],dis2[maxn];
int head[maxn],dfn[maxn],low[maxn],Stack[maxn],ins[maxn],id[maxn],siz[maxn];
bool vis[maxn];
int n,m,tot,num,top,cnt,tol;

void addedge(int x,int y)
{
    Edge[++tot].to=y,Edge[tot].nxt=head[x],head[x]=tot;
}

void addedge_sd(int x,int y)
{
    sd[++tol].to=y,sd[tol].nxt=head_sd[x],head_sd[x]=tol;
}

void addedge_rev(int x,int y)
{
    revsd[++tot].to=y,revsd[tot].nxt=revhead[x],revhead[x]=tot;
}

void spfa1(int k)
{
    dis1[k]=siz[k];
    queue<int> q;
    q.push(k);
    vis[k]=1;
    while(!q.empty())
	{
        int now=q.front();
		q.pop(),vis[now]=0;
        for(int i=head_sd[now];i;i=sd[i].nxt)
		{
            int v=sd[i].to;
            if(dis1[v]<dis1[now]+siz[v])
			{
                dis1[v]=dis1[now]+siz[v];
                if(!vis[v])
					q.push(v),vis[v]=1;
            }
        }
    }
}

void spfa2(int k)
{
    dis2[k]=siz[k];
    queue<int> q;
    q.push(k);
    vis[k]=1;
    while(!q.empty())
	{
        int now=q.front();
		q.pop(),vis[now]=0;
        for(int i=revhead[now];i;i=revsd[i].nxt)
		{
            int v=revsd[i].to;
            if(dis2[v]<dis2[now]+siz[v])
			{
                dis2[v]=dis2[now]+siz[v];
                if(!vis[v])
					q.push(v),vis[v]=1;
            }
        }
    }
}

void tarjan(int x)
{
    dfn[x]=low[x]=++num;
    Stack[++top]=x,ins[x]=1;
    int y;
    for(int i=head[x];i;i=Edge[i].nxt)
    {
        y=Edge[i].to;
        if(!dfn[y])
        {
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        else if(ins[y])
            low[x]=min(low[x],dfn[y]);
    }
    if(dfn[x]==low[x])
    {
        ++cnt;
        do
        {
            y=Stack[top--],ins[y]=0;
            id[y]=cnt,++siz[cnt];
        }while(x!=y);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    int x,y;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        addedge(x,y);
    }
    for(int i=1;i<=n;i++)
        if(!dfn[i])
            tarjan(i);
	tot=tol=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j;j=Edge[j].nxt)
        {
            y=Edge[j].to;
            if(id[i]==id[y])
                continue;
            addedge_sd(id[i],id[y]);
            addedge_rev(id[y],id[i]);
        }
    }
    spfa1(id[1]),spfa2(id[1]);
    int ans=siz[id[1]];
    for(int i=1;i<=cnt;i++)
	{
		if(dis1[i])
		{
			for(int j=revhead[i];j;j=revsd[j].nxt)
			{
				int to=revsd[j].to;
				if(dis2[to])
					ans=max(ans,dis1[i]+dis2[to]-siz[id[1]]);
			}
		}
	}
	printf("%d\n",ans);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值