bzoj 3887: [Usaco2015 Jan]Grass Cownoisseur

本文介绍了一种解决Bessie牛在Farmers John农场上寻找最多草地吃草的算法问题的方法。通过Tarjan算法进行缩点,并结合正反向图进行SPFA搜索,最终找出在允许一次违规反向行走的情况下,Bessie能访问的最大不重复草地数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


【分析】
tarjan缩点+正反建图跑spfa+枚举


【代码】

#include<queue>
#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fo(i,j,k) for(register int i=j;i<=k;i++)
using namespace std;
const int mxn=100005;
stack <int> s;
int n,m,mx,cnt,num,tim;
int dfn[mxn],low[mxn],from[mxn],des[mxn],que[10*mxn];
int head[mxn],siz[mxn],be[mxn],disz[mxn],disf[mxn];
bool in[mxn],vis[mxn];
struct edge {int to,next;} f[mxn<<1];
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void add(int u,int v)
{
    f[++cnt].to=v,f[cnt].next=head[u],head[u]=cnt;
}
inline void tarjan(int u)
{
    int v;
    dfn[u]=low[u]=++tim;
    s.push(u);
    in[u]=1;
    for(int i=head[u];i;i=f[i].next)
    {
        int v=f[i].to;
        if(!dfn[v])
          tarjan(v),low[u]=min(low[u],low[v]);
        else if(in[v])
          low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        num++;
        do
        {
            v=s.top();
            s.pop();
            in[v]=0;
            be[v]=num;
            siz[num]++;
        }while(u!=v);
    }
}
inline void rebuild(bool f)
{
    cnt=0;
    memset(head,0,sizeof head);
    fo(i,1,m)
    {
        int u=from[i],v=des[i];
        if(be[u]!=be[v])
        {
            if(f) add(be[u],be[v]);
            else add(be[v],be[u]);
        }
    }
}
inline void spfa_z()
{
    int h=0,t=0;
    disz[be[1]]=siz[be[1]];
    que[++t]=be[1];
    while(h<t)
    {
        int u=que[++h];
        vis[u]=0;
        for(int i=head[u];i;i=f[i].next)
        {
            int v=f[i].to;
            if(disz[v]<disz[u]+siz[v])
            {
                disz[v]=disz[u]+siz[v];
                if(!vis[v]) que[++t]=v,vis[v]=1;
            }
        }
    }
}
inline void spfa_f()
{
    int h=0,t=0;
    disf[be[1]]=siz[be[1]];
    que[++t]=be[1];
    while(h<t)
    {
        int u=que[++h];
        vis[u]=0;
        for(int i=head[u];i;i=f[i].next)
        {
            int v=f[i].to;
            if(disf[v]<disf[u]+siz[v])
            {
                disf[v]=disf[u]+siz[v];
                if(!vis[v]) que[++t]=v,vis[v]=1;
            }
        }
    }
}
int main()
{
    int u,v,ans=0;
    n=read(),m=read(); 
    fo(i,1,m)
    {
        from[i]=read(),des[i]=read();
        add(from[i],des[i]);
    }
    fo(i,1,n) if(!dfn[i]) tarjan(i);    
    rebuild(1);  //正向建图 
    spfa_z();
    rebuild(0);  //反向建图 
    spfa_f();
    fo(i,1,m)
      if(be[from[i]]!=be[des[i]])
      {
          if(disz[be[des[i]]] && disf[be[from[i]]])
            ans=max(ans,disz[be[des[i]]]+disf[be[from[i]]]-siz[be[1]]);
      }
    printf("%d\n",ans);
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值