https://www.luogu.org/problemnew/show/P3119

C++版本一

题解:强连通分量+最长路

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=200000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum,top,col;
int a,b,c,sta[N],vis[N],low[N],dfn[N],si[N],co[N],dist[N];
vector<int>G[N];
vector<int>H[N];
void Tarjan(int u){
    dfn[u]=low[u]=++sum;
    vis[u]=1;
    sta[++top]=u;
    for(int i=0,j=G[u].size();i<j;i++){
        int v=G[u][i];
        if(!dfn[v]){
            Tarjan(v);
            low[u]=min(low[v],low[u]);
        }else if(vis[v]){
            low[u]=min(dfn[v],low[u]);
        }
    }
    if(dfn[u]==low[u]){
        co[u]=++col;
        ++si[col];
        while(sta[top]!=u){
            ++si[col];
            co[sta[top]]=col;
            vis[sta[top]]=0;
            --top;
        }
        vis[sta[top]]=0;
        --top;
    }

}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&u,&v);
        G[u].push_back(v);
    }
    for(int i=1;i<=n;i++)if(!dfn[i])Tarjan(i);
    for(int i=1;i<=col;i++)si[i+col]=si[i];
    for(int i=1;i<=n;i++)
        for(int j=0,k=G[i].size();j<k;j++)
            if(co[i]!=co[G[i][j]]){
                int u=co[i];
                int v=co[G[i][j]];
                H[u].push_back(v);
                H[v].push_back(u+col);
                H[u+col].push_back(v+col);
            }
    vis[co[1]]=true;
    queue<int>q;
    q.push(co[1]);
    while(!q.empty()){
        int x=q.front();
        q.pop();
        for(int i=0;i<H[x].size();i++)
            if(dist[H[x][i]]<dist[x]+si[x]){
                dist[H[x][i]]=dist[x]+si[x];
                if(!vis[H[x][i]])
                    vis[H[x][i]]=true,q.push(H[x][i]);
        }
        vis[x]=false;
    }
    col==1?cout<<n<<endl:cout<<dist[co[1]+col]<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

#include<vector>
#include<map>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int N=1e5+5,M=1e5+5;
int h[N],nxt[M],r[M],sum=1;
int h1[N],nxt1[M],r1[M],sum1=1;
int h2[N],nxt2[M],r2[M],sum2=1;
int t,c[N],dfn[N],low[N],s[N],top,color,num,start,gass[N];
int p,q,n,m;
int dis1[N],dis2[N];
bool use[N];
void add(int a,int b){sum++;nxt[sum]=h[a],r[sum]=b;h[a]=sum;}
void add1(int a,int b){sum1++;nxt1[sum1]=h1[a],r1[sum1]=b;h1[a]=sum1;}
void add2(int a,int b){sum2++;nxt2[sum2]=h2[a],r2[sum2]=b;h2[a]=sum2;}
void found(int k)
{
dfn[k]=low[k]=++num,s[++top]=k,use[k]=1;
for(int i=h[k];i;i=nxt[i])
{
    int v=r[i];
    if(!dfn[v])found(v),low[k]=min(low[k],low[v]);
    else if(use[v])low[k]=min(low[k],low[v]);
}   
if(low[k]==dfn[k])
{
    c[k]=++color,use[k]=0;
    gass[color]=1;
    while(s[top]!=k){gass[color]++,use[s[top]]=0;c[s[top--]]=c[k];}
        top--;
}
}
void spfa1(int k)
{
    dis1[k]=gass[k];
    queue<int> Q;
    Q.push(k);
    while(!Q.empty()){
        int now=Q.front();Q.pop();
        for(int i=h1[now];i;i=nxt1[i]){
            int v=r1[i];
            if(dis1[v]<dis1[now]+gass[v]){
                dis1[v]=dis1[now]+gass[v];
                if(!use[v])Q.push(v),use[v]=1;
            }
        }
                use[now]=0;
    }
}
void spfa2(int k)
{ 
    dis2[k]=gass[k];
    queue<int> Q;
    Q.push(k);
    while(!Q.empty()){
        int now=Q.front();Q.pop();
        for(int i=h2[now];i;i=nxt2[i]){
            int v=r2[i];
            if(dis2[v]<dis2[now]+gass[v]){
                dis2[v]=dis2[now]+gass[v];
                if(!use[v])Q.push(v),use[v]=1;
            }
        }
        use[now]=0;
    }
}
int main()
{
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&q,&p);
add(q,p);
}
for(int i=1;i<=n;i++)if(!dfn[i])found(i);
start=c[1];
for(int i=1;i<=n;i++)
for(int u=h[i];u;u=nxt[u])
if(c[i]!=c[r[u]])add1(c[i],c[r[u]]),add2(c[r[u]],c[i]);
spfa1(start),spfa2(start);
int ans=gass[start];
for(int i=1;i<=n;i++)
    if(!use[c[i]]&&dis1[c[i]]){
    int now=c[i];
    use[now]=1;
    for(int u=h2[now];u;u=nxt2[u]){
        int v=r2[u];
        if(!dis2[v])continue;
        ans=max(ans,dis1[now]+dis2[v]-gass[start]);
    }

    }
    cout<<ans;
return 0;
}