1. 程式人生 > >P3119 [USACO15JAN]草鑒定Grass Cownoisseur

P3119 [USACO15JAN]草鑒定Grass Cownoisseur

-o time 操作 conn arr ++ 輸出 spfa won

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縮點見新圖,建分層圖,可保證只走一次反邊,自己畫圖;

然後便是在 DAGDAG 上進行操作的問題了。看了下其他大佬的題解,都是分類,枚舉。這裏介紹一個分層圖的方法。

考慮一張圖,將這個圖復制一份,點的編號從 11 ~ NN 到 (N+1)(N+1) ~ (N+N)(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\n",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\n",dis[place[1]+scccnt]);

}
View Code

P3119 [USACO15JAN]草鑒定Grass Cownoisseur