P3119 [USACO15JAN]草鉴定Grass Cownoisseur

                               [USACO15JAN]草鉴定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.

约翰有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.

 

输出格式:

 

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.

 

题意就是给你一张单向边的图,从1出发,问你最多能走过几个点再回到1,一个点可以重复走,但只算一次,还有一次走逆向边的机会

看完题目,我们发现图中有环,显而易见,先tarjan缩点,建一个新图,

如果不走逆向边,那么从1出发回到1,新图中没有环时,走过的点数就是1所在的环点的数目

有环时就是新环中点的数目

但是我们又一次走逆向边的机会,所以在没有环的情况下,我们可以主动制造一个环

 

P3119 [USACO15JAN]草鉴定Grass Cownoisseur_第1张图片

就如上面的情况,我们把 1->2 反向就成了一个环

就目前状态来看,我们可以认为这是由环断为两条链 

链中存在两种点 一个是1可以到达的点 

另一种是可以到达1的点

用拓扑排序处理 1能到达的点和 能到1的点

显然 我们可以枚举某一条边,把它反向,ans=从1到此边一点的数目+另一点到1的点的数目-1所在环点的数目(这个环加了两次)

  1 #include 
  2 #include 
  3 
  4 const int MAXN=100010;
  5 const int INF=0x3f3f3f3f;
  6 
  7 int n,m,top,id,inr,ans;
  8 
  9 int dfn[MAXN],low[MAXN],stack[MAXN],belong[MAXN],sum[MAXN];
 10 
 11 bool vis[MAXN];
 12 
 13 struct node {
 14     int to;
 15     int next;
 16     node(){}
 17     node(int to,int next):to(to),next(next){}
 18 };
 19 node e[MAXN];
 20 
 21 int head[MAXN],tot;
 22 
 23 inline void read(int&x) {
 24     int f=1;register char c=getchar();
 25     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
 26     for(;isdigit(c);x=x*10+c-48,c=getchar());
 27     x=x*f;
 28 }
 29 
 30 inline int max(int a,int b) {return ab:a;}
 31 
 32 inline void add(int x,int y) {
 33     e[++tot]=node(y,head[x]);
 34     head[x]=tot;
 35 }
 36 
 37 inline int min(int a,int b) {return aa:b;}
 38 
 39 void tarjan(int u) {
 40     dfn[u]=low[u]=++inr;
 41     vis[u]=true;
 42     stack[++top]=u;
 43     for(int i=head[u];i;i=e[i].next) {
 44         int v=e[i].to;
 45         if(!dfn[v]) {
 46             tarjan(v);
 47             low[u]=min(low[u],low[v]);
 48         }
 49         else if(vis[v]) low[u]=min(low[u],dfn[v]);
 50     }
 51     if(dfn[u]==low[u]) {
 52         ++id;
 53         int t;
 54         do {
 55             t=stack[top--];
 56             vis[t]=false;
 57             belong[t]=id;
 58             ++sum[id];
 59         }while(u!=t);
 60     }
 61     return;
 62 }
 63 
 64 struct Topu_sort{
 65     struct p{
 66         int to;
 67         int next;
 68     };
 69     p edge[MAXN];
 70     int Head[MAXN],Tot;
 71     int in[MAXN],q[MAXN],f[MAXN];
 72     int h,t;
 73     void add(int x,int y) {
 74         ++in[y];
 75         edge[++Tot].to=y;
 76         edge[Tot].next=Head[x];
 77         Head[x]=Tot;
 78     }
 79     void Sort() {
 80         for(int i=1;i<=id;++i) f[i]=-INF;
 81         f[belong[1]]=sum[belong[1]];
 82         for(int i=1;i<=id;++i) 
 83           if(!in[i]) q[++t]=i;
 84         while(h<t) {
 85             int u=q[++h];
 86             for(int i=Head[u];i;i=edge[i].next) {
 87                 int v=edge[i].to;
 88                 f[v]=max(f[v],f[u]+sum[v]);
 89                 if(!--in[v]) q[++t]=v;
 90             }
 91         }
 92     }
 93 }pos,neg;
 94 
 95 int hh() {
 96     int x,y;
 97     read(n);read(m);
 98     for(int i=1;i<=m;++i) {
 99         read(x);read(y);
100         add(x,y);
101     }
102     for(int i=1;i<=n;++i) 
103       if(!dfn[i]) tarjan(i);
104     for(int i=1;i<=n;++i) 
105       for(int j=head[i];j;j=e[j].next) {
106           int to=e[j].to;
107           if(belong[i]!=belong[to]) {
108               pos.add(belong[i],belong[to]);
109               neg.add(belong[to],belong[i]);
110           } 
111       }
112     pos.Sort();
113     neg.Sort();
114     ans=sum[belong[1]];
115     for(int i=1;i<=n;++i)
116       for(int j=head[i];j;j=e[j].next) {
117           int v=e[j].to;
118           ans=max(ans,pos.f[belong[v]]+neg.f[belong[i]]);
119       }
120     printf("%d\n",ans-sum[belong[1]]);
121     return 0;
122 } 
123 
124 int sb=hh();
125 int main(int argc,char**argv) {;}
代码

 

转载于:https://www.cnblogs.com/whistle13326/p/7473998.html

你可能感兴趣的:(P3119 [USACO15JAN]草鉴定Grass Cownoisseur)