luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur

题目描述

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.

输出格式:

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.

1A爽,缩点后变成一棵树,spfa处理出树上每个点到1所在点的最长路,然后枚举连向点1和点1连向的点之间的边,答案就是这两个点最长路的和

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100007;
inline int read() {
    int x=0,f=1;
    char c=getchar();
    while(c<‘0‘||c>‘9‘) {
        if(c==‘-‘)f=-1;
        c=getchar();
    }
    while(c<=‘9‘&&c>=‘0‘) {
        x=x*10+c-‘0‘;
        c=getchar();
    }
    return x*f;
}
int n,m;
struct node{
    int v,next;
}edge[maxn],E1[maxn],E2[maxn];
int head[maxn],H1[maxn],H2[maxn],num,N1,N2;
void add_edge(int x,int y) {
    edge[++num].v=y;edge[num].next=head[x];head[x]=num;
}
void A1(int x,int y) {
    E1[++N1].v=y;E1[N1].next=H1[x];H1[x]=N1;
}
void A2(int x,int y){
    E2[++N2].v=y;E2[N2].next=H2[x];H2[x]=N2;
}
int dfn[maxn],low[maxn],tn=0,stack[maxn],color[maxn],color_cnt;
int top=0,cnt[maxn];bool vis[maxn];
void tarjan(int x) {
    dfn[x]=low[x]=++tn;stack[++top]=x,vis[x]=1;
    for(int i=head[x];i;i=edge[i].next) {
        int v=edge[i].v;
        if(!dfn[v]) {
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(vis[v]) low[x]=min(low[x],dfn[v]);
    }
    if(dfn[x]==low[x]) {
        ++color_cnt;
        while(stack[top]!=x) {
            vis[stack[top]]=0;
            color[stack[top]]=color_cnt;
            top--;
            cnt[color_cnt]++;
        }
        cnt[color_cnt]++;
        color[x]=color_cnt;vis[x]=0;top--;
    }
}
int dis1[maxn],dis2[maxn];
int que[maxn*4];
bool can[maxn],can2[maxn];
void topo1(int S) {
    int h=1,t=1;
    que[1]=S;
    dis1[S]=cnt[S];
    memset(vis,0,sizeof vis);
    vis[S]=1;can[S]=1;
    while(h<=t) {
        int u=que[h++];
        for(int i=H1[u];i;i=E1[i].next) {
            int v=E1[i].v;can[v]=1;
            if(dis1[v]<dis1[u]+cnt[v]) {
                dis1[v]=max(dis1[v],dis1[u]+cnt[v]);
                if(!vis[v])que[++t]=v,vis[v]=1;
            }
        }
        vis[u]=0;
    }
}
void topo2(int S) {
    int h=1,t=1;
    que[1]=S;
    dis2[S]=cnt[S];
    memset(vis,0,sizeof vis);
    vis[S]=1;can2[S]=1;
    while(h<=t) {
        int u=que[h++];
        for(int i=H2[u];i;i=E2[i].next) {
            int v=E2[i].v;can2[v]=1;
            if(dis2[v]<dis2[u]+cnt[v]) {
                dis2[v]=max(dis2[v],dis2[u]+cnt[v]);
                if(!vis[v])que[++t]=v,vis[v]=1;
            }
        }
        vis[u]=0;
    }
}
int main() {
    n=read(),m=read();
    for(int a,b,i=1;i<=m;++i) {
        a=read(),b=read();
        add_edge(a,b);
    }
    for(int i=1;i<=n;++i)
        if(!dfn[i])tarjan(i);
    int S=color[1];
    for(int i=1;i<=n;++i) {
        for(int j=head[i];j;j=edge[j].next) {
            int v=edge[j].v;
            if(color[v]!=color[i]){
                A1(color[i],color[v]);
                A2(color[v],color[i]);
            }
        }
    }
    topo1(color[1]);
    topo2(color[1]);
    int ans=0;
    for(int i=1;i<=color_cnt;++i) {
        for(int j=H1[i];j;j=E1[j].next) {
            int v=E1[j].v;
            if(can2[i]&&can[v]) {
                ans=max(ans,dis2[i]+dis1[v]-cnt[color[1]]);
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}
时间: 2024-08-03 18:21:48

luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur的相关文章

洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur

P3119 [USACO15JAN]草鉴定Grass Cownoisseur tarjan缩点,正反spfa,枚举边,更新最大值 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define maxn 1000000 4 #define inf 0x3f3f3f3f 5 int n,m,x[maxn],y[maxn],z,num,head[maxn],head2[maxn],tim,ans,tot,dis1[maxn],dis2[maxn

洛谷——P3119 [USACO15JAN]草鉴定Grass Cownoisseur

P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 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 co

P3119 [USACO15JAN]草鉴定Grass Cownoisseur

P3119 [USACO15JAN]草鉴定Grass Cownoisseur 题目描述 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 co

Luogu 3119 [USACO15JAN]草鉴定Grass Cownoisseur

思路很乱,写个博客理一理. 缩点 + dp. 首先发现把一个环上的边反向是意义不大的,这样子不但不好算,而且相当于浪费了一次反向的机会.反正一个强连通分量里的点绕一遍都可以走到,所以我们缩点之后把一个强连通分量放在一起处理. 设$st$表示缩点之后$1$所在的点,设$f_{x}$表示从$st$走到$x$的最长链,$g_{x}$表示从$x$走到$st$的最长链,因为把一个$DAG$上的边反向一下并不会走重复的点,那么我们最后枚举一下边$(x, y)$,把它反向,这样子$f_{x} + g_{y}

[USACO15JAN]草鉴定Grass Cownoisseur(分层图+tarjan)

[USACO15JAN]草鉴定Grass Cownoisseur 题目描述 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

[USACO15JAN]草鉴定Grass Cownoisseur

[题目描述]: [USACO15JAN]草鉴定Grass Cownoisseur [思路]: 首先我们先思考贝茜不走那条反边,那么对于任意强连通分量\(E\)易知: \(\forall u,v \in E\),\(\exists u \to v \ and \ v \to u\) \(\because\)贝茜每次经过一个草场时只会吃一次草,\(\therefore\)可以进行缩点,缩点后得到一个\(DAG\),统计每一个强连通分量的\(size\)值,表示此强连通分量中有多少个点,然后在\(DA

BZOJ 3887[Usaco2015 Jan]Grass Cownoisseur

题面: 3887: [Usaco2015 Jan]Grass Cownoisseur Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 237  Solved: 130[Submit][Status][Discuss] Description In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow pat

bzoj3887: [Usaco2015 Jan]Grass Cownoisseur

题意: 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1) =>有向图我们先考虑缩点.然后观察缩点后的图可以发现新的路径中必定只有一条边是反向的才符合条件.那么我们可以联想到某道最短路的题将边反向存一遍后分别从s和t跑一跑.那么这里bfs跑一跑就行了.然后有一个坑点:这种重建图的注意es和edges不然es会在中途就被修改掉了... #include<cstdio> #

luogu P3116 [USACO15JAN]会议时间Meeting Time

题目描述 Bessie and her sister Elsie want to travel from the barn to their favorite field, such that they leave at exactly the same time from the barn, and also arrive at exactly the same time at their favorite field. The farm is a collection of N fields