洛谷 P3119 [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.
输入输出样例
7 10
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7
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.
思路:tarjin缩点,然后跑正反两边spfa,找出1所能到达的点和能到达1的点,我们所要偷偷逆行的边一定连接这两个点。然后枚举那条边逆行,求求最大值。
#include<iostream> #include<map> #include<queue> #include<cstdio> #include<cstring> #define MAXN 100000 using namespace std; struct nond{ int x,y; }edge[MAXN]; queue<int>que; map<int,int>ma[MAXN]; int n,m,tot,tot1,tot2,tim; int top,ans,sumcol,sumedge; int to[MAXN],net[MAXN],head[MAXN]; int to1[MAXN],net1[MAXN],head1[MAXN]; int to2[MAXN],net2[MAXN],head2[MAXN]; int col[MAXN],sum[MAXN],dis[MAXN],dis1[MAXN]; int dfn[MAXN],low[MAXN],vis[MAXN],stack[MAXN],visstack[MAXN]; void add(int u,int v){ to[++tot]=v;net[tot]=head[u];head[u]=tot; } void add1(int u,int v){ to1[++tot1]=v;net1[tot1]=head1[u];head1[u]=tot1; to2[++tot2]=u;net2[tot2]=head2[v];head2[v]=tot2; } void tarjin(int now){ low[now]=dfn[now]=++tim; stack[++top]=now; vis[now]=1; visstack[now]=1; for(int i=head[now];i;i=net[i]) if(visstack[to[i]]) low[now]=min(low[now],dfn[to[i]]); else if(!vis[to[i]]){ tarjin(to[i]); low[now]=min(low[now],low[to[i]]); } if(low[now]==dfn[now]){ sumcol++; col[now]=sumcol; while(stack[top]!=now){ col[stack[top]]=sumcol; visstack[stack[top]]=0; top--; } visstack[now]=0; top--; } } void spfa(int s){ memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); while(!que.empty()) que.pop(); que.push(s); dis[s]=sum[s];vis[s]=1; while(!que.empty()){ int now=que.front(); que.pop(); vis[now]=0; for(int i=head1[now];i;i=net1[i]) if(dis[to1[i]]<dis[now]+sum[to1[i]]){ dis[to1[i]]=dis[now]+sum[to1[i]]; if(!vis[to1[i]]){ vis[to1[i]]=1; que.push(to1[i]); } } } } void spfa1(int s){ memset(vis,0,sizeof(vis)); memset(dis1,0,sizeof(dis)); while(!que.empty()) que.pop(); que.push(s); dis1[s]=sum[s];vis[s]=1; while(!que.empty()){ int now=que.front(); que.pop(); vis[now]=0; for(int i=head2[now];i;i=net2[i]) if(dis1[to2[i]]<dis1[now]+sum[to2[i]]){ dis1[to2[i]]=dis1[now]+sum[to2[i]]; if(!vis[to2[i]]){ vis[to2[i]]=1; que.push(to2[i]); } } } } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ int u,v; scanf("%d%d",&u,&v); add(u,v); } for(int i=1;i<=n;i++) if(!vis[i]) tarjin(i); for(int i=1;i<=n;i++) sum[col[i]]++; for(int i=1;i<=n;i++) for(int j=head[i];j;j=net[j]) if(col[i]!=col[to[j]]) if(ma[col[i]].find(col[to[j]])==ma[col[i]].end()){ ma[col[i]][col[to[j]]]=1; add1(col[i],col[to[j]]); edge[++sumedge].x=col[i]; edge[sumedge].y=col[to[j]]; } spfa(col[1]); spfa1(col[1]); for(int i=1;i<=sumedge;i++){ if(dis[edge[i].x]&&dis1[edge[i].y]) ans=max(ans,dis[edge[i].x]+dis1[edge[i].y]); if(dis[edge[i].y]&&dis1[edge[i].x]) ans=max(ans,dis[edge[i].y]+dis1[edge[i].x]); } cout<<ans-sum[col[1]]; }