3887: [Usaco2015 Jan]Grass Cownoisseur
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 170 Solved: 89
[ 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
Output
Sample Input
1 2
3 1
2 5
2 4
3 7
3 5
3 6
6 5
7 2
4 7
Sample Output
HINT
Source
题解:tarjan+spfa
tarjan将图变成有向无环图,然后正着跑一遍spfa,求出1到当前连通块中间经过的最多的点数,在反向建图,求出每次连通块到1经过的最多的点数,然后枚举反置哪一条边,更新答案即可。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define N 200003
using namespace std;
int n,m;
int point[N],v[N],next[N],x[N],y[N],dis[N],dis1[N],can[N];
int belong[N],size[N],st[N],top,tot,cnt,sz,dfsn[N],ins[N],low[N];
void add(int x,int y)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;
//cout<<x<<" "<<y<<endl;
}
void tarjan(int x)
{
dfsn[x]=low[x]=++sz; st[++top]=x;
ins[x]=1;
for (int i=point[x];i;i=next[i])
if (!dfsn[v[i]]) {
tarjan(v[i]);
low[x]=min(low[x],low[v[i]]);
}else if (ins[v[i]]) low[x]=min(low[x],dfsn[v[i]]);
if (dfsn[x]==low[x]) {
++cnt; int j;
do{
j=st[top--];
belong[j]=cnt;
size[cnt]++; ins[j]=0;
}while (j!=x);
}
}
void spfa(int dis[N])
{
memset(dis,0,sizeof(dis));
memset(can,0,sizeof(can));
int t=belong[1]; dis[t]=size[t]; can[t]=1;
queue<int> p;
p.push(t);
while (!p.empty()) {
int now=p.front(); p.pop();
for (int i=point[now];i;i=next[i])
if (dis[v[i]]<dis[now]+size[v[i]])
{
dis[v[i]]=dis[now]+size[v[i]];
if (!can[v[i]]) {
can[v[i]]=1;
p.push(v[i]);
}
}
can[now]=0;
}
}
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++) {
scanf("%d%d",&x[i],&y[i]);
add(x[i],y[i]);
}
for (int i=1;i<=n;i++)
if (!dfsn[i]) tarjan(i);
//cout<<endl;
tot=0;
memset(point,0,sizeof(point));
memset(next,0,sizeof(next));
for (int i=1;i<=m;i++)
if (belong[x[i]]!=belong[y[i]]) add(belong[x[i]],belong[y[i]]);
spfa(dis);
//cout<<endl;
tot=0;
memset(point,0,sizeof(point));
memset(next,0,sizeof(next));
for (int i=1;i<=m;i++)
if (belong[x[i]]!=belong[y[i]]) add(belong[y[i]],belong[x[i]]);
spfa(dis1);
int ans=size[belong[1]];
//for (int i=1;i<=cnt;i++)
// cout<<size[i]<<" ";
//cout<<endl;
for (int i=1;i<=m;i++)
if (belong[x[i]]!=belong[y[i]]) {
int t=belong[x[i]]; int t1=belong[y[i]];
if (dis[t1]&&dis1[t]) ans=max(ans,dis[t1]+dis1[t]-size[belong[1]]);
}
printf("%d\n",ans);
}