[BZOJ3887] [Usaco2015 Jan]Grass Cownoisseur

题目描述

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

题目分析

首先 肯定强连通分量的点都选最多 所以我们先缩点
然后枚举新图中的每条边 假设反转边x->to[i],答案是(1->to[i]+x->1);
所以将每个强连通分量的大小转换为边权 跑正反两张图的最长路
最后 因为包含1的强连通分量的大小算了两次 还要减去一个

#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
int n,m;
int size[100100];
struct tar
{
    int tot;
    int head[100100],net[200200],to[200200];
    void add(int x,int y) { net[++tot]=head[x],head[x]=tot,to[tot]=y; }
    int deep[100100],low[100100],sa[100100],tim,color,belong[100100];
    bool vis[100100];
    void tarjan(int x)
    {
        deep[x]=low[x]=++tim;
        sa[++sa[0]]=x,vis[x]=1;
        for(int i=head[x];i;i=net[i])
        {
            if(!deep[to[i]])
                tarjan(to[i]),low[x]=min(low[x],low[to[i]]);
            else if(vis[to[i]]) low[x]=min(low[x],deep[to[i]]);
        }
        if(deep[x]==low[x])
        {
            color++;
            int tmp;
            do
            {
                tmp=sa[sa[0]--],vis[tmp]=0;
                belong[tmp]=color,size[color]++;
            }while(tmp!=x);
        }
    }
}S;
struct your
{
    int tot;
    int head[100100],net[200200],to[200200];
    void add(int x,int y) { net[++tot]=head[x],head[x]=tot,to[tot]=y; }
    int dis[100100];
    bool vis[100100];
    int que[1001000];
    int l,r;
    your() { l=1; } 
    void spfa()
    {
        dis[S.belong[1]]=size[S.belong[1]];
        que[++r]=S.belong[1];
        while(l<=r)
        {
            int x=que[l++];
            vis[x]=0;
            for(int i=head[x];i;i=net[i])
                if(dis[to[i]]<dis[x]+size[to[i]])
                {
                    dis[to[i]]=dis[x]+size[to[i]];
                    if(!vis[to[i]]) que[++r]=(to[i]);
                }
        }
    }
}A,B;
int main()
{   
    scanf("%d%d",&n,&m);
    for(int x,y,i=1;i<=m;i++)
        scanf("%d%d",&x,&y),S.add(x,y);
    for(int i=1;i<=n;i++)
        if(!S.deep[i]) S.tarjan(i);
    for(int i=1;i<=n;i++)
        for(int j=S.head[i];j;j=S.net[j])
            if(S.belong[i]!=S.belong[S.to[j]])
                A.add(S.belong[i],S.belong[S.to[j]]),B.add(S.belong[S.to[j]],S.belong[i]);
    A.spfa(),B.spfa();
    int ans=size[S.belong[1]];
    for(int i=1;i<=S.color;i++)
        for(int j=A.head[i];j;j=A.net[j])   
            if(A.dis[A.to[j]]&&B.dis[i])
                ans=max(ans,A.dis[A.to[j]]+B.dis[i]-size[S.belong[1]]);     
    printf("%d",ans);
    return 0;
}

发表评论

邮箱地址不会被公开。 必填项已用*标注