zoukankan      html  css  js  c++  java
  • BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP

    BZOJ_3887_[Usaco2015 Jan]Grass Cownoisseur_强连通分量+拓扑排序+DP

    Description

    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)

    Input

    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

    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.

    Sample Input

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

    Sample Output

    6

    考虑一个强连通分量内的点可以随便走,于是缩个点。
    然后设f[i]表示1到i最多能走几个点,g[i]表示i到n最多能走几个点,由于没有环的存在,
    我们枚举边,用f[u]+g[v]更新答案,最后答案需要减掉1所在scc的大小。
     
    代码:
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #define N 100500
    int head[N],to[N],nxt[N],cnt,siz[N],bel[N],tot,dfn[N],low[N],scc,S[N],ins[N],n,m,xx[N],yy[N],Q[N],in[N],l,r;
    int f[N],g[N],ans;
    inline void add(int u,int v) {
        to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt;
    }
    void dfs(int x) {
        int i; dfn[x]=low[x]=++tot; ins[x]=1; S[++S[0]]=x;
        for(i=head[x];i;i=nxt[i]) {
            if(!dfn[to[i]]) {
                dfs(to[i]);
                low[x]=min(low[x],low[to[i]]);
            }else if(ins[to[i]]) {
                low[x]=min(low[x],dfn[to[i]]);
            }
        }
        if(dfn[x]==low[x]) {
            int t=S[S[0]--];
            scc++; siz[scc]++; bel[t]=scc; ins[t]=0;
            while(x!=t) {
                t=S[S[0]--]; siz[scc]++; bel[t]=scc; ins[t]=0;
            }
        }
    }
    int main() {
        // freopen("wander.in","r",stdin);
        // freopen("wander.out","w",stdout);
        scanf("%d%d",&n,&m);
        if(n==1) {
            puts("1"); return 0;
        }
        int i,x,y;
        for(i=1;i<=m;i++) {
            scanf("%d%d",&x,&y); add(x,y); xx[i]=x; yy[i]=y;
        }
        for(i=1;i<=n;i++) if(!dfn[i]) dfs(i);
        memset(head,0,sizeof(head)); cnt=0; tot=0;
        for(i=1;i<=m;i++) {
            x=bel[xx[i]]; y=bel[yy[i]];
            if(x!=y) add(x,y),in[y]++,xx[++tot]=x,yy[tot]=y;
        }
        for(i=1;i<=scc;i++) f[i]=g[i]=-1000000;
        int S=bel[1];f[S]=0; g[S]=0;
        for(i=1;i<=scc;i++) if(!in[i]) Q[r++]=i;
        while(l<r) {
            x=Q[l++]; f[x]+=siz[x];
            for(i=head[x];i;i=nxt[i]) {
                f[to[i]]=max(f[to[i]],f[x]);
                if((--in[to[i]])==0) Q[r++]=to[i];
            }
        }
        memset(head,0,sizeof(head)); cnt=0; memset(in,0,sizeof(in));
        for(i=1;i<=tot;i++) {
            add(yy[i],xx[i]); in[xx[i]]++;
        }
        l=r=0;
        for(i=1;i<=scc;i++) if(!in[i]) Q[r++]=i;
        while(l<r) {
            x=Q[l++]; g[x]+=siz[x];
            for(i=head[x];i;i=nxt[i]) {
                g[to[i]]=max(g[to[i]],g[x]);
                if((--in[to[i]])==0) Q[r++]=to[i];
            }
        }
        for(i=1;i<=tot;i++) {
            ans=max(ans,max(f[xx[i]]+g[yy[i]],f[yy[i]]+g[xx[i]]));
        }
        printf("%d
    ",ans-siz[S]);
    }
    /*
    7 10
    1 2
    3 1
    2 5
    2 4
    3 7
    3 5
    3 6
    6 5
    7 2
    4 7
    */
     
    /*
    5 8
    3 2
    4 3
    3 5
    4 1
    2 3
    1 3
    4 2
    1 4
    */
    
  • 相关阅读:
    汽车金融评分卡
    Lending Club 数据做数据分析&评分卡
    时间序列分析和预测 (转载)
    距离计算公式总结(转载)
    机器学习常用算法与辅助函数公式
    金融领域常用的数据分析方法
    常用模型评估方法总结
    A--集成算法的实现
    A--随机森林(RF)的 sciklit-learn 实现
    A--Scikit-Learn 实现决策树
  • 原文地址:https://www.cnblogs.com/suika/p/9279129.html
Copyright © 2011-2022 走看看