zoukankan      html  css  js  c++  java
  • 洛谷—— P3119 [USACO15JAN]草鉴定Grass Cownoisseur || BZOJ——T 3887: [Usaco2015 Jan]Grass Cownoisseur

    http://www.lydsy.com/JudgeOnline/problem.php?id=3887||

    https://www.luogu.org/problem/show?pid=3119

    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

    HINT

     

    Source

    Gold&鸣谢18357

    先把原图缩点,跑出从1到n和从n到1的最多可以遍历的牧场数,

    枚举每个边做无向边的情况更新答案

     SPFA跑最多牧场数

      1 #include <cstdio>
      2 #include <queue>
      3 #define min(a,b) (a<b?a:b)
      4 #define max(a,b) (a>b?a:b)
      5 
      6 const int INF(0x3f3f3f3f);
      7 const int N(1e5+5);
      8 int n,head[N],sumedge;
      9 struct Edge
     10 {
     11     int v,next;
     12     Edge(int v=0,int next=0):v(v),next(next){}
     13 }edge[N];
     14 inline void ins(int u,int v)
     15 {
     16     edge[++sumedge]=Edge(v,head[u]);
     17     head[u]=sumedge;
     18 }
     19 
     20 int tim,dfn[N],low[N];
     21 int top,Stack[N],instack[N];
     22 int sumcol,col[N],point[N];
     23 void DFS(int u)
     24 {
     25     low[u]=dfn[u]=++tim;
     26     Stack[++top]=u; instack[u]=1;
     27     for(int v,i=head[u];i;i=edge[i].next)
     28     {
     29         v=edge[i].v;
     30         if(!dfn[v])  DFS(v),low[u]=min(low[u],low[v]);
     31         else if(instack[v]) low[u]=min(low[u],dfn[v]);
     32     }
     33     if(low[u]==dfn[u])
     34     {
     35         col[u]=++sumcol;
     36         point[sumcol]++;
     37         for(;Stack[top]!=u;top--)
     38         {
     39             point[sumcol]++;
     40             col[Stack[top]]=sumcol;
     41             instack[Stack[top]]=0;
     42         }
     43         instack[u]=0; top--;
     44     }
     45 }
     46 
     47 int hed[N],had[N],sum;
     48 struct E
     49 {
     50     int v,next,w;
     51     E(int v=0,int next=0,int w=0):v(v),next(next),w(w){}
     52 }e[N][2];
     53 inline void insert(int u,int v)
     54 {
     55     e[++sum][0]=E(v,hed[u],point[v]);
     56     hed[u]=sum;
     57     e[sum][1]=E(u,had[v],point[u]);
     58     had[v]=sum;
     59 }
     60 
     61 bool inq[N];
     62 int v1[N],v2[N];
     63 void SPFA(int op,int s,int *val,int *head)
     64 {
     65     for(int i=1;i<=sumcol;i++)
     66         inq[i]=0,val[i]=-INF;
     67     val[s]=point[s];
     68     std::queue<int>que;
     69     que.push(s);
     70     for(int u,v;!que.empty();)
     71     {
     72         u=que.front(); que.pop(); inq[u]=0;
     73         for(int i=head[u];i;i=e[i][op].next)
     74         {
     75             v=e[i][op].v;
     76             if(val[v]<val[u]+e[i][op].w)
     77             {
     78                 val[v]=val[u]+e[i][op].w;
     79                 if(!inq[v]) inq[v]++,que.push(v);
     80             }
     81         }
     82     }
     83 }
     84 
     85 inline void read(int &x)
     86 {
     87     x=0; register char ch=getchar();
     88     for(;ch>'9'||ch<'0';) ch=getchar();
     89     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
     90 }
     91 
     92 int AC()
     93 {
     94     int m; read(n),read(m);
     95     for(int u,v;m--;)
     96         read(u),read(v),ins(u,v);
     97     for(int i=1;i<=n;i++)
     98         if(!dfn[i]) DFS(i);
     99     for(int v,u=1;u<=n;u++)
    100         for(int i=head[u];i;i=edge[i].next)
    101         {
    102             v=edge[i].v;
    103             if(col[u]!=col[v]) insert(col[u],col[v]);
    104         }
    105     int ans=-INF;
    106     SPFA(0,col[1],v1,hed);
    107     SPFA(1,col[1],v2,had);
    108     for(int v,u=1;u<=sum;u++)
    109         for(int i=hed[u];i;i=e[i][0].next)
    110         {
    111             v=e[i][0].v;
    112             ans=max(ans,v1[v]+v2[u]);
    113         }
    114     for(int v,u=1;u<=sum;u++)
    115         for(int i=had[u];i;i=e[i][1].next)
    116         {
    117             v=e[i][1].v;
    118             ans=max(ans,v1[u]+v2[v]);
    119         }
    120     printf("%d
    ",ans-point[col[1]]);
    121     return 0;
    122 }
    123 
    124 int Hope=AC();
    125 int main(){;}
    SPFA AC

    Topsort跑最多牧场数

      1 #include <cstdio>
      2 #include <queue>
      3 #define min(a,b) (a<b?a:b)
      4 #define max(a,b) (a>b?a:b)
      5 
      6 const int INF(0x3f3f3f3f);
      7 const int N(1e5+5);
      8 int n,head[N],sumedge;
      9 struct Edge
     10 {
     11     int v,next;
     12     Edge(int v=0,int next=0):v(v),next(next){}
     13 }edge[N];
     14 inline void ins(int u,int v)
     15 {
     16     edge[++sumedge]=Edge(v,head[u]);
     17     head[u]=sumedge;
     18 }
     19 
     20 int tim,dfn[N],low[N];
     21 int top,Stack[N],instack[N];
     22 int sumcol,col[N],point[N],rd[N],cd[N];
     23 void DFS(int u)
     24 {
     25     low[u]=dfn[u]=++tim;
     26     Stack[++top]=u; instack[u]=1;
     27     for(int v,i=head[u];i;i=edge[i].next)
     28     {
     29         v=edge[i].v;
     30         if(!dfn[v])  DFS(v),low[u]=min(low[u],low[v]);
     31         else if(instack[v]) low[u]=min(low[u],dfn[v]);
     32     }
     33     if(low[u]==dfn[u])
     34     {
     35         col[u]=++sumcol;
     36         point[sumcol]++;
     37         for(;Stack[top]!=u;top--)
     38         {
     39             point[sumcol]++;
     40             col[Stack[top]]=sumcol;
     41             instack[Stack[top]]=0;
     42         }
     43         instack[u]=0; top--;
     44     }
     45 }
     46 
     47 int hed[N],had[N],sum;
     48 struct E
     49 {
     50     int v,next,w;
     51     E(int v=0,int next=0,int w=0):v(v),next(next),w(w){}
     52 }e[N][2];
     53 inline void insert(int u,int v)
     54 {
     55     e[++sum][0]=E(v,hed[u],point[v]);
     56     hed[u]=sum;
     57     e[sum][1]=E(u,had[v],point[u]);
     58     had[v]=sum;
     59 }
     60 
     61 int v1[N],v2[N];
     62 #define max(a,b) (a>b?a:b)
     63 void Topsort(int op,int s,int *val,int *head,int *du)
     64 {
     65     std::queue<int>que;
     66     for(int i=1;i<=sumcol;i++)
     67     {
     68         if(!du[i]) que.push(i);
     69         val[i]=-INF;
     70     }
     71     val[s]=point[s];
     72     for(int u,v;!que.empty();)
     73     {
     74         u=que.front(); que.pop();
     75         for(int i=head[u];i;i=e[i][op].next)
     76         {
     77             v=e[i][op].v;
     78             val[v]=max(val[v],val[u]+e[i][op].w);
     79             if(--du[v]==0) que.push(v);
     80         }
     81     }
     82 }
     83 
     84 inline void read(int &x)
     85 {
     86     x=0; register char ch=getchar();
     87     for(;ch>'9'||ch<'0';) ch=getchar();
     88     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
     89 }
     90 
     91 int AC()
     92 {
     93     int m; read(n),read(m);
     94     for(int u,v;m--;)
     95         read(u),read(v),ins(u,v);
     96     for(int i=1;i<=n;i++)
     97         if(!dfn[i]) DFS(i);
     98     for(int v,u=1;u<=n;u++)
     99         for(int i=head[u];i;i=edge[i].next)
    100         {
    101             v=edge[i].v;
    102             if(col[u]==col[v]) continue;
    103             rd[col[v]]++,cd[col[u]]++;
    104             insert(col[u],col[v]);
    105         }
    106     int ans=-INF;
    107     Topsort(0,col[1],v1,hed,rd);
    108     Topsort(1,col[1],v2,had,cd);
    109     for(int v,u=1;u<=sum;u++)
    110         for(int i=hed[u];i;i=e[i][0].next)
    111         {
    112             v=e[i][0].v;
    113             ans=max(ans,v1[v]+v2[u]);
    114         }
    115     for(int v,u=1;u<=sum;u++)
    116         for(int i=had[u];i;i=e[i][1].next)
    117         {
    118             v=e[i][1].v;
    119             ans=max(ans,v1[u]+v2[v]);
    120         }
    121     printf("%d
    ",ans-point[col[1]]);
    122     return 0;
    123 }
    124 
    125 int Hope=AC();
    126 int main(){;}
    Topsort AC
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    STM32存储器知识的相关应用(IAP、Bit Banding)
    转:嵌入式编程(以STM32为例)中的volatile,const意义及应用场景
    STM32 :IAP实验 & 写入内部Flash
    modint
    poly
    小蒟蒻太蒻了
    volume 服务
    Vold分析
    文件系统属性详解
    PCI 百度百科
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7471460.html
Copyright © 2011-2022 走看看