BZOJ 【BZOJ3887】【Usaco2015 Jan】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. 给一个有向图,然后选一条路径起点终点都为1的路径出来,有一次机会可以沿某条边逆方向走,问最多有多少个点可以被经过?(一个点在路径中无论出现多少正整数次对答案的贡献均为1)

输入

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.

输出

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.

样例输入

7 10
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7

样例输出

6

怎么每次考试最后一题都是英文题。啊啊啊,话说考试tarjan+dfs可以拿到80,但细节处理错了只拿到了40。。很心累啊,本蒟蒻还想通过随机性卡过,后来放弃了。正解是跑两边spfa。。然后枚举每一条边,Ans = 正图中dis[v]+反图中dis[u] 因为 不能对每一个点都spfa所以才考虑跑反图。代码极丑。

code:

#define MAXN 100005
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <ctime>
#include <queue>
using namespace std;
int n,m,e=1,first[MAXN];
int head[MAXN],o=1,Ans;
int vis[MAXN],dis[MAXN],Dis[MAXN],ee=1,woc[MAXN];
 
 
struct edge{
    int u,v,w,next;
}a[MAXN<<1],b[MAXN<<1],c[MAXN<<1];
 
void push_edge(int u,int v){
    b[o].u=u;
    b[o].v=v;
    b[o].next=head[u];
    head[u]=o++;
}
 
void push(int u,int v,int w){
    a[e].u=u;
    a[e].v=v;
    a[e].w=w;
    a[e].next=first[u];
    first[u]=e++;
}
 
void push_a(int u,int v,int w){
    c[ee].u=u;
    c[ee].v=v;
    c[ee].w=w;
    c[ee].next=woc[u];
    woc[u]=ee++;
}
 
int dfn[MAXN],low[MAXN],Stack[MAXN],top,scc,belong[MAXN],cnt,w[MAXN];
bool instack[MAXN];
 
void tarjan(int u){
    instack[u]=1;
    Stack[++top]=u;
    low[u]=dfn[u]=++cnt;
    for(int i=head[u];i;i=b[i].next){
        int v = b[i].v;
        if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]);
        else if(instack[v])low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u]){
        int v;
        scc++;
        do{
            v = Stack[top--];
            belong[v]=scc;
            w[scc]++;
            instack[v]=0;
        }while(u!=v);
    }
}
 
void spfa(){
    memset(dis,0,sizeof dis);
    queue<int>q;
    q.push(belong[1]);
    vis[belong[1]]=1;
    dis[belong[1]]=w[belong[1]];
    while(!q.empty()){
        int k = q.front();q.pop();
        vis[k]=0;
        for(int i = first[k];i;i=a[i].next){
            int v = a[i].v;
            if(dis[v]<dis[k]+a[i].w){
                dis[v]=dis[k]+a[i].w;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
 
void Spfa(){
    memset(Dis,0,sizeof Dis);
    queue<int>q;
    q.push(belong[1]);
    vis[belong[1]]=1;
    Dis[belong[1]]=w[belong[1]];
    while(!q.empty()){
        int k = q.front();q.pop();
        vis[k]=0;
        for(int i = woc[k];i;i=c[i].next){
            int v = c[i].v;
            if(Dis[v]<Dis[k]+c[i].w){
                Dis[v]=Dis[k]+c[i].w;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
 
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        push_edge(u,v);
    }
    for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
    for(int i=1;i<o;i++)
        if(belong[b[i].u]!=belong[b[i].v]){
            push(belong[b[i].u],belong[b[i].v],w[belong[b[i].v]]);
            push_a(belong[b[i].v],belong[b[i].u],w[belong[b[i].u]]);
        }
    spfa();Spfa();
    for(int i=1;i<e;i++){
        int u = a[i].u,v=a[i].v;
        if(dis[v]>0&&Dis[u]>0)
        Ans = max(Ans,dis[v]+Dis[u]-w[belong[1]]);
    }
    printf("%d\n",Ans);
}






posted @ 2017-07-13 21:49  cooook  阅读(134)  评论(0编辑  收藏  举报