洛谷P3119 [USACO15JAN]草鉴定Grass Cownoisseur
? 解题记录 ? ? 洛谷 ? ? 拓扑排序 ? ? 动态规划 ? ? 强连通分量 ?    2017-10-21 16:57:19    409    0    0

题目描述

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.

锻炼思路的好题。首先很明显有如下结论:如果处在一个强联通分量中,一个强连通分量中的所有点都可以达到(题目已经说明可以重复走点)。那么我们先tarjan缩点,记录每一个点大小为size。接下来的问题变成了如何找出一条边使得这条边反转后过点1的回路上的size总数最大。

考虑枚举每一条边,对于每一个点处理出全程沿着正向边走和全程沿着反向边走的最长路,再对每一条边进行计算取最值就行了。又因为tarjan缩点之后图一定是DAG,那么我们可以把tarjan缩点建两个图,一个连正向边,另一个连反向边,然后拓扑排序+DP O(n)处理出最长路信息,O(n)枚举。于是这道题就神奇般做出来了。

然而这里还有一个技巧,考虑到DAG将所有边反向后拓扑序与原来正好相反,就可以少做一次拓扑序。整个算法看似是O(n),实际上堪比n log n。下面是代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
const int maxn = 1e5 + 5;
struct edge {
    int u, v, next;
}e[maxn << 1];
int head[maxn], cnt;
int forhead[maxn], backhead[maxn], mxfor[maxn], mxback[maxn], topfor[maxn], cnt1;
bool vis[maxn];
void adde(const int &u, const int &v) {
    e[++cnt] = (edge) {u, v, head[u]};
    head[u] = cnt;
}
void adde(const int &u, const int &v, const bool &is_toward) {
    if(is_toward) e[++cnt] = (edge) {u, v, forhead[u]}, forhead[u] = cnt;
    else e[++cnt] = (edge) {u, v, backhead[u]}, backhead[u] = cnt;
}
int dfn[maxn], low[maxn], idcnt, part, num[maxn], size[maxn];
int us[maxn], vs[maxn], st;
int n, m;
stack<int > stk;
int tarjan(int u) {
    dfn[u] = low[u] = ++idcnt;
    stk.push(u);
    for(register int i = head[u]; i; i = e[i].next) {
        int v = e[i].v;
        if(num[v]) continue;
        if(dfn[v]) low[u] = min(low[u], low[v]);
        else low[u] = min(low[u], tarjan(v));
    }
    if(low[u] == dfn[u]) {
        int v;++part;
        do{
            v = stk.top(), stk.pop();
            num[v] = part, ++size[part];
        }while(v != u);
    }
    return low[u];
}
void dfsfor(int u) {
    vis[u] = 1;
    for(register int i = forhead[u]; i; i = e[i].next) {
        int v = e[i].v;
        if(vis[v]) continue;
        dfsfor(v);
    }
    topfor[++cnt1] = u;
}
void dtfor() {
    for(register int i = 1; i <= part; ++i) {
        if(!vis[i]) dfsfor(i);
        if(i == num[1]) st = i;
    }
}
int main() {
    scanf("%d%d", &n, &m);
    int u, v, w;
    for(register int i = 1; i <= m; ++i) {
        scanf("%d%d", &u, &v);
        adde(u, v), us[i] = u, vs[i] = v;
    }
    for(register int i = 1; i <= n; ++i) 
        if(!num[i]) tarjan(i);
    cnt = 0;
    memset(e, 0, sizeof(e));
    for(register int i = 1; i <= m; ++i) {
        if(num[us[i]] == num[vs[i]]) continue;
        adde(num[us[i]], num[vs[i]], 1);
        adde(num[vs[i]], num[us[i]], 0);
    }
    dtfor();
    mxfor[st] = size[st];
    for(register int i = st; i >= 1; --i) {
        int now = topfor[i];
        for(register int i = forhead[now]; i; i = e[i].next) {
            int v = e[i].v;
            if(!mxfor[now]) continue;
            mxfor[v] = max(mxfor[now] + size[v], mxfor[v]);
        }
    }
    mxback[st] = size[st];
    for(register int i = st; i <= n; ++i) {
        int now = topfor[i];
        for(register int i = backhead[now]; i; i = e[i].next) {
            int v = e[i].v;
            if(!mxback[now]) continue;
            mxback[v] = max(mxback[now] + size[v], mxback[v]);
        }
    }
    int mx = 0;
    for(register int i = 1; i <= cnt; ++i) {
        int u = e[i].u, v = e[i].v;
        if(!mxback[u] || !mxfor[v]) continue;
        if(mxback[u] + mxfor[v] - mxfor[num[1]] >= mx)
            mx = mxback[u] + mxfor[v] - mxfor[num[1]]; 
    }
    if(mx) printf("%d\n", mx);
    else printf("%d\n", max(mxfor[num[1]], mxback[num[1]]));
    return 0;
}

 

上一篇: 洛谷P1972 [SDOI2009]HH的项链

下一篇: HDU5434 Peace small elephant

409 人读过
立即登录, 发表评论.
没有帐号? 立即注册
0 条评论
文档导航