1. 程式人生 > >洛谷——P3119 [USACO15JAN]草鑒定Grass Cownoisseur

洛谷——P3119 [USACO15JAN]草鑒定Grass Cownoisseur

lib return 找到 ast 題目 輸入 ostream starting point

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.

輸入輸出樣例

輸入樣例#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.

先tarjan縮點,然後建邊。在這個時候我們需要建兩條邊,一條跑正向,一條反向,說白了就是建一條反向邊。
然後在進行雙向dfs,跑出從1點所能到達的點的最長鏈及能到達一點的最長鏈,更新到達當前點時所能更新出的最大值。

然後進行枚舉每一條邊,我們將其進行反向,看其反向後是否能連通整個圖,若能,更新最大值。

我們更新結果的時候,枚舉每一條可以反向的邊,只有在這條邊可以從1出來並且可以回到1時才可以使用。

#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 210000
using namespace std;
int n,m,x,y,s,tot,tat,top,tim;
bool vis[N],vis1[N],vis2[N],vist1[N],vist2[N];
int xx[N],yy[N],in1[N],in2[N],head1[N],head2[N],dfn[N];
int low[N],sum[N],ans1[N],ans2[N],head[N],stack[N],belong[N];
queue<int>q;
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0; ch=getchar();}
    return x*f;
}
struct Edge
{
    int to,dis,next,from;
}edge[N],edge1[N],edge2[N];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int add1(int x,int y)
{
    tat++;
    edge1[tat].to=y;
    edge1[tat].next=head1[x];
    edge2[tat].to=x;
    edge2[tat].next=head2[y];
    head1[x]=head2[y]=tat;
}
int tarjan(int now)
{
    dfn[now]=low[now]=++tim;
    vis[now]=true; stack[++top]=now;
    for(int i=head[now];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if(vis[t]) low[now]=min(low[now],dfn[t]);
        else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
    }
    if(low[now]==dfn[now])
    {
        s++,belong[now]=s,sum[s]++;
        for(;stack[top]!=now;top--)
         belong[stack[top]]=s,vis[stack[top]]=false,sum[s]++;
        vis[now]=false,top--;
    }
}
int shink_point()
{
    for(int i=1;i<=n;i++)
     for(int j=head[i];j;j=edge[j].next)
      if(belong[i]!=belong[edge[j].to])
        add1(belong[i],belong[edge[j].to]);
}
int dfs1(int x)
{
    vis1[x]=true;vist1[x]=true;
    for(int i=head1[x];i;i=edge1[i].next)
    {
        int t=edge1[i].to;
        if(ans1[t]<ans1[x]+sum[t])
        {
            ans1[t]=ans1[x]+sum[t];
            dfs1(t);
        }
    }
    vis1[x]=false;
}
int dfs2(int x)
{
    vis2[x]=true;vist2[x]=true;
    for(int i=head2[x];i;i=edge2[i].next)
    {
        int t=edge2[i].to;
        if(ans2[t]<ans2[x]+sum[t])
        {
            ans2[t]=ans2[x]+sum[t];
            dfs2(t);
        }
    }
    vis2[x]=false;
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=m;i++)
     xx[i]=read(),yy[i]=read(),add(xx[i],yy[i]);
    for(int i=1;i<=n;i++)
     if(!dfn[i]) tarjan(i);
    shink_point();
    ans1[belong[1]]=ans2[belong[1]]=sum[belong[1]];
    dfs1(belong[1]);dfs2(belong[1]);
    int answer=2*sum[belong[1]];
    for(int i=1;i<=m;i++)
    {
        x=belong[yy[i]],y=belong[xx[i]];
        if(vist1[x]&&vist2[y])
         answer=max(answer,ans1[x]+ans2[y]);
    }
    printf("%d",answer-sum[belong[1]]);
    return 0;
}

拓撲排序:

這個題原來是打算用來練拓撲排序的,結果做了一天的拓撲排序發現不過樣例、、、、

為什麽會用拓撲排序??

因為我們用拓撲排序的話可以輕易地找到最長鏈。

怎麽拓撲排序??

我們如果直接進行拓撲排序的話,我們會意識到一個問題:縮完點以後直接統計出來入度為零的點並非是我們所需要的點1,我們要跑最長鏈的話我們需要從1點開始跑,也就是說我們的起點必須是1,怎樣做到這一點??我們要做到起點是一的話我們必須讓1的入度為零,從一點開始更新與他相連的點。從新統計他們的入讀,也就是說我們將這個可能出現環的圖抽離成一顆樹,這棵樹的樹根為1點。然後再進行拓撲排序,找出最長鏈。

最後在進行枚舉邊,進行更新、

#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 210000
using namespace std;
int n,m,x,y,s,tot,tat,top,tim;
bool vis[N],vis1[N],vis2[N];
int xx[N],yy[N],in1[N],in2[N],head1[N],head2[N],dfn[N];
int low[N],sum[N],ans1[N],ans2[N],head[N],stack[N],belong[N];
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0; ch=getchar();}
    return x*f;
}
struct Edge
{
    int to,from,next;
}edge[N],edge1[N],edge2[N];
int add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
int add1(int x,int y)
{
    tat++;
    edge1[tat].to=y;
    edge1[tat].next=head1[x];
    edge2[tat].to=x;
    edge2[tat].next=head2[y];
    head1[x]=head2[y]=tat;
}
int tarjan(int now)
{
    dfn[now]=low[now]=++tim;
    vis[now]=true;stack[++top]=now;
    for(int i=head[now];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if(vis[t]) low[now]=min(dfn[t],low[now]);
        else if(!dfn[t]) tarjan(t),low[now]=min(low[t],low[now]);
    }
    if(low[now]==dfn[now])
    {
        s++,belong[now]=s,sum[s]++;
        for(;stack[top]!=now;top--)
          belong[stack[top]]=s,sum[s]++,vis[stack[top]]=false;
        vis[now]=false;top--;    
    }
}
int shink_point()
{
    for(int i=1;i<=m;i++)
     for(int j=head[i];j;j=edge[j].next)
      if(belong[i]!=belong[edge[j].to])
          add1(belong[i],belong[edge[j].to]);  
}
int dfs1(int s)
{
    for(int i=head1[s];i;i=edge1[i].next)
    {
        int t=edge1[i].to;
        if(!in1[t]) dfs1(t);
        in1[t]++;
    }
}
int dfs2(int s)
{
    for(int i=head2[s];i;i=edge2[i].next)
    {
        int t=edge2[i].to;
        if(!in2[t]) dfs2(t);
        in2[t]++;
    }
}
int tpsort(int *in,Edge *edge,int *head,bool *vis,int *ans)
{
    queue<int>q;
    q.push(belong[1]);
    while(!q.empty())
    {
        int x=q.front();q.pop();vis[x]=true;
        for(int i=head[x];i;i=edge[i].next)
        {
            int t=edge[i].to;
            in[t]--;
            if(!in[t]) q.push(t);
            ans[t]=max(ans[t],ans[x]+sum[t]);
        }
    }
}
int main()
{
    n=read(),m=read();
    int answer=0;
    for(int i=1;i<=m;i++)
     xx[i]=read(),yy[i]=read(),add(xx[i],yy[i]);
    for(int i=1;i<=n;i++)
     if(!dfn[i]) tarjan(i);
    shink_point();
    dfs1(belong[1]),dfs2(belong[1]);
    ans1[belong[1]]=ans2[belong[1]]=sum[belong[1]];
    tpsort(in1,edge1,head1,vis1,ans1);
    tpsort(in2,edge2,head2,vis2,ans2);
    answer=2*sum[belong[1]];
    for(int i=1;i<=m;i++)
    {
        x=belong[yy[i]],y=belong[xx[i]];
        if(vis1[x]&&vis2[y])
         answer=max(answer,ans1[x]+ans2[y]);
    }
    printf("%d",answer-sum[belong[1]]);
    return 0;
}

洛谷——P3119 [USACO15JAN]草鑒定Grass Cownoisseur