BZOJ 3887[Usaco2015 Jan]Grass Cownoisseur

题面:

3887: [Usaco2015 Jan]Grass Cownoisseur

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 237  Solved: 130
[Submit][Status][Discuss]

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

先tarjan缩点,然后我们考虑反置那一条边。

反置强联通分量里的边是没有贡献的。所以我们只考虑DAG里的边,

可以求出从1到i和从i到1的最大权值,之后枚举每条边,将其反置,求出最大权值。

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<queue>
  5 using namespace std;
  6 #define maxn 100010
  7 struct node
  8 {
  9     int u,v,nex;
 10 }edge[maxn],e[maxn];
 11 int head[maxn],cnt;
 12 int dfn[maxn],low[maxn],belong[maxn],num[maxn],T,ans;
 13 int s[maxn],top;
 14 int n,m;
 15 bool book[maxn];
 16 int d1[maxn],d2[maxn];
 17 void add(int u,int v)
 18 {
 19     edge[++cnt]=(node){u,v,head[u]};
 20     head[u]=cnt;
 21 }
 22 void tarjan(int u)
 23 {
 24     int v;
 25     dfn[u]=low[u]=++T;
 26     s[++top]=u;
 27     book[u]=true;
 28     for(int i=head[u];i;i=edge[i].nex)
 29     {
 30         v=edge[i].v;
 31         if(!dfn[v])
 32         {
 33             tarjan(v);
 34             low[u]=min(low[u],low[v]);
 35         }
 36         else
 37             if(book[v])
 38                 low[u]=min(low[u],dfn[v]);
 39     }
 40     if(dfn[u]==low[u])
 41     {
 42         ++ans;
 43         v=-1;
 44         while(v!=u)
 45         {
 46             v=s[top--];
 47             belong[v]=ans;
 48             ++num[ans];
 49             book[v]=false;
 50         }
 51     }   
 52 }
 53 void init()
 54 {
 55     for(int i=1;i<=n;i++)
 56         if(!dfn[i])
 57             tarjan(i);
 58 }
 59 void spfa(int *d)
 60 {
 61     memset(d,0x7fff,sizeof(d));
 62     memset(book,false,sizeof(book));
 63     queue<int>q;
 64     q.push(belong[1]);
 65     book[belong[1]]=true;
 66     d[belong[1]]=num[belong[1]];
 67     int u,v;
 68     while(!q.empty())
 69     {
 70         u=q.front();
 71         q.pop();
 72         book[u]=false;
 73         for(int i=head[u];i;i=edge[i].nex)
 74         {
 75             v=edge[i].v;
 76             if(d[v]<d[u]+num[v])
 77             {
 78                 d[v]=d[u]+num[v];
 79                 if(!book[v])
 80                 {
 81                     q.push(v);
 82                     book[v]=true;
 83                 }
 84             }
 85         }
 86     }
 87 }
 88 void build()
 89 {
 90     cnt=0;
 91     memset(head,0,sizeof(head));
 92     for(int i=1;i<=m;i++)
 93         if(belong[e[i].u]!=belong[e[i].v])
 94             add(belong[e[i].u],belong[e[i].v]);
 95 }
 96 void rebuild()
 97 {
 98     cnt=0;
 99     memset(head,0,sizeof(head));
100     for(int i=1;i<=m;i++)
101         if(belong[e[i].u]!=belong[e[i].v])
102             add(belong[e[i].v],belong[e[i].u]);
103 }
104 int main()
105 {
106     scanf("%d%d",&n,&m);
107     for(int i=1;i<=m;i++)
108     {
109         scanf("%d%d",&e[i].u,&e[i].v);
110         add(e[i].u,e[i].v);
111     }
112     init();
113     ans=0;
114     build();
115     spfa(d1);
116     rebuild();
117     spfa(d2);
118     for(int i=1;i<=cnt;i++)
119         if(d1[edge[i].u]&&d2[edge[i].v])
120             ans=max(ans,d1[edge[i].u]+d2[edge[i].v]);
121     printf("%d",ans-num[belong[1]]);
122 }
BZOJ 3887

 

 
posted @ 2017-07-13 21:32  avancent  阅读(163)  评论(0编辑  收藏  举报