zoukankan      html  css  js  c++  java
  • P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    P3119 [USACO15JAN]草鉴定Grass Cownoisseur

    链接: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缩点见新图,建分层图,可保证只走一次反边,自己画图;

    然后便是在 DAG 上进行操作的问题了。看了下其他大佬的题解,都是分类,枚举。这里介绍一个分层图的方法。

    考虑一张图,将这个图复制一份,点的编号从 1 ~ N 到 (N+1) ~ (N+N) 。然后在两层图中连边。对于原图上的每一条边,

    从原图的指向点到新图的起始点连一条边,边权与原边相同,代表逆向走一条边。逆向走了一条边,就不能再逆向走了,

    所以从上面的一层(新图)无法回到下面的一层。最后跑一遍 SPFA ,节点 11 所在的强连通分量编号,到节点 11 所在的强连通分量编号+ NN上的最长路,就是最后的答案。

    注意建了多层图点数量加倍,数组要开大

    #include<bits/stdc++.h>
    using namespace std;
    
    #define INF 10000008
    const int maxn = 100000+5;
    stack <int> s;
    vector <int> g[maxn<<1];
    const int inf = 1000000008;
    int cnt,low[maxn],h[maxn],dis[maxn<<1],siz[maxn],dfn[maxn],tot,scccnt,place[maxn];
    bool inq[maxn<<1],ins[maxn];
    struct edge{int v,nxt;}G[maxn];
    void add(int u, int v){
        G[++tot].v = v; G[tot].nxt = h[u]; h[u] = tot;
    }
    void tarjian(int x){
        dfn[x] = low[x] = ++tot;
        s.push(x);
        ins[x] = 1;
        for(int i = h[x]; i; i = G[i].nxt){
            int v = G[i].v;
            if(!dfn[v]){
                tarjian(v);
                low[x] = min(low[x], low[v]);
            }
            else if(ins[v])low[x] = min(low[x], dfn[v]);
        }
    
        if(low[x] == dfn[x]){
            scccnt++;
            int cnt = 0;
            while(1){
                int t = s.top();
                ins[t] = 0; place[t] = scccnt;
                s.pop();
                cnt++;
                if(t == x)break;
    
            }
            siz[scccnt] = cnt;
        }
    }
    void SPFA(int x){
        memset(inq, 0, sizeof(inq));
        for(int i = 1; i <= scccnt*2; i++) dis[i] = -inf;
        queue <int> Q;
        Q.push(x);
        dis[x] = 0;
        inq[x] = 1;
        while(!Q.empty()){
            int u = Q.front();
            Q.pop();
            inq[u] = 0;
            for(int i = 0; i < g[u].size(); i++){
                int v = g[u][i];
                if(dis[u] + siz[v] > dis[v]){
                    dis[v] = dis[u] + siz[v];
                //    printf("%d %d %d
    ",u,v,dis[v]);
                    if(!inq[v]){
                        Q.push(v);
                        inq[v] = 1;
                    }
                }
            }
        }
    
    }
    
    int main(){
        //freopen("1.in","r",stdin);
        //freopen("111.out","w",stdout);
        int n, m;
        cin>>n>>m;
        for(int i = 1; i <= m; i++){
            int u, v;
            scanf("%d%d",&u,&v);
            add(u, v);
        }
        for(int i = 1; i <= n; i++)
                if(!dfn[i])tarjian(i);
           for(int i = 1; i <= scccnt; i++)siz[i+scccnt] = siz[i];
        for(int i = 1; i <= n; i++)
            for(int j = h[i]; j; j = G[j].nxt){
                int v = G[j].v;
                if(place[i] != place[v])
                    {
                        g[place[i]].push_back(place[v]);
                        g[place[v]].push_back(place[i]+scccnt);
                        g[place[i]+scccnt].push_back(place[v]+scccnt);//分层图
                    }
    
            }
        SPFA(place[1]);
        printf("%d
    ",dis[place[1]+scccnt]);
    
    }
    View Code
  • 相关阅读:
    java 抽象类
    ClassNotFoundException: dao.impl.ActionImpl
    侦听状态一直为T的处理
    Duplicate entry '1' for key 'PRIMARY'(报错)
    ibatis学习笔记
    java中的堆、栈和常量池
    servlet学习
    三大排序
    第一次面试??交流
    毕业季,学长,学姐们的践行
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/9029305.html
Copyright © 2011-2022 走看看