洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur(tarjan+spfa) 题解

题目来源:

https://www.luogu.org/problemnew/show/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.

输出:

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

输入输出样例

输入样例#1: 复制

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

输出样例#1: 复制

6 

说明

SOLUTION NOTES:

Here is an ASCII drawing of the sample input:

v---3-->6
7   | \ |
^\  v  \|
| \ 1   |
|   |   v
|   v   5
4<--2---^

Bessie can visit pastures 1, 2, 4, 7, 2, 5, 3, 1 by traveling

backwards on the path between 5 and 3. When she arrives at 3 she

cannot reach 6 without following another backwards path.

解题思路:

           一开始肯定就是先tarjan重新建边,因为每一个强连通分量的点可以随意到达,所以到一个点就相当于到了所有分量,在建边的时候,a-b的边权就是fl【b】,fl【】就是强连通分量的大小,因为可以一次逆行,我们可以考虑分层图,先建1-n,在建n+1-2*n,对于每一条a-b,就连一条b-a+n,边权还是fl【a】,然后就是跑一边最长路就行。。

代码:

#include <iostream>
#include <stack>
#include <string>
#include <queue>
#include <cstring>
#define inf 0x3f3f3f3f
const int maxn=1e5+5;
using namespace std;
struct newt
{
    int to,next,val;
}e1[maxn],e2[3*maxn];
int dfn[maxn],low[maxn],head1[maxn],head2[3*maxn],fl[maxn],lt[maxn],cnt1,cnt2,tot,gs,vis[maxn],n,m;
bool jl[3*maxn];
int dis[3*maxn];
void addedge1(int u,int v,int w)
{
    e1[cnt1].to=v;
    e1[cnt1].val=w;
    e1[cnt1].next=head1[u];
    head1[u]=cnt1++;
}
void addedge2(int u,int v,int w)
{
    e2[cnt2].to=v;
    e2[cnt2].val=w;
    e2[cnt2].next=head2[u];
    head2[u]=cnt2++;
}
stack<int>s;
void tarjan(int u)
{
    low[u]=dfn[u]=++tot;
    vis[u]=1;s.push(u);
    for(int i=head1[u];i!=-1;i=e1[i].next)
    {
        int v=e1[i].to;
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u]){
        gs++;
        int sum=0;
        while(1){
            int now=s.top();s.pop();
            vis[now]=0;
            sum++;
            lt[now]=gs;
            if(now==u)break;
        }
        fl[gs]=sum;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    memset(head1,-1,sizeof(head1));
    memset(head2,-1,sizeof(head2));
    memset(dis,-1,sizeof(dis));
    cnt1=cnt2=0;gs=tot=0;
    for(int i=1;i<=m;i++)
    {
        int a,b;
        cin>>a>>b;
        addedge1(a,b,1);
    }
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])tarjan(i);
    }
    for(int i=1;i<=n;i++)
    for(int j=head1[i];j!=-1;j=e1[j].next)
    {
        int v=e1[j].to;
        if(lt[i]==lt[v])continue;
        addedge2(lt[i],lt[v],fl[lt[v]]);
        addedge2(lt[i]+gs,lt[v]+gs,fl[lt[v]]);
        addedge2(lt[v],lt[i]+gs,fl[lt[i]]);
    }
    int S=lt[1];
    queue<int>q;
    dis[S]=0;
    jl[S]=1;
    q.push(S);
    while(!q.empty())
    {
        int now=q.front();
        q.pop();jl[now]=0;
        for(int i=head2[now];i!=-1;i=e2[i].next)
        {
            int v=e2[i].to,w=e2[i].val;
           // cout<<v<<" "<<w<<endl;
            if(dis[v]<dis[now]+w)
            {
               // cout<<now<<" "<<v<<" "<<dis[v]<<" "<<dis[now]<<" "<<w<<endl;
                dis[v]=dis[now]+w;
                if(!jl[v]){
                    jl[v]=1;
                    q.push(v);
                }
            }
        }
    }
    cout<<dis[S+gs]<<endl;
    //cin>>n>>m;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40400202/article/details/81431750
今日推荐