The Tarjan is shrunk to a dag, then the answer becomes the longest chain, and the DP count is
It's just that the face is too scary to respond.
#include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <queue >using namespace Std;const int N=100005;int n,m,mod,h[n],cnt,dfn[n],low[n],tot,bl[n],col,s[n],top,si[n],d[n],f[n ],g[n],vis[n],ans1,ans2;bool v[n];vector<pair<int,int> >a;queue<int>q;struct qwe{int no,ne,to;} E[n*10];int read () {int r=0,f=1; Char P=getchar (); while (p> ' 9 ' | | p< ' 0 ') {if (p== '-') f=-1; P=getchar (); } while (p>= ' 0 ' &&p<= ' 9 ') {r=r*10+p-48; P=getchar (); } return r*f;} void Add (int u,int v) {cnt++; E[cnt].ne=h[u]; E[cnt].no=u; E[cnt].to=v; h[u]=cnt;} void Tarjan (int u) {dfn[u]=low[u]=++tot; V[s[++top]=u]=1; for (int i=h[u];i;i=e[i].ne) {if (!dfn[e[i].to]) {Tarjan (e[i].to); Low[u]=min (low[u],low[e[i].to]); } else if (V[e[i].to]) low[u]=min (low[u],dfn[e[i].to]); } if (Dfn[u]==low[u]) {col++; while (s[top]!=u) {bl[s[top]]=col; si[col]++; v[s[top--]]=0; } Bl[s[top]]=col; si[col]++; v[s[top--]]=0; }}int Main () {N=read (), M=read (), Mod=read (); for (int i=1;i<=m;i++) {int x=read (), Y=read (); Add (x, y); } for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (i); for (int i=1;i<=cnt;i++) if (bl[e[i].no]!=bl[e[i].to]) a.push_back (Make_pair (bl[e[i].no],bl[e[i].to])); memset (h,0,sizeof (h)); cnt=0; for (int i=0;i<a.size (); i++) Add (A[i].first,a[i].second), d[a[i].second]++; for (int i=1;i<=col;i++) {if (!d[i]) Q.push (i); F[i]=si[i],g[i]=1; } while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=h[u];i;i=e[i].ne) {d[e[i].to]--; if (!d[e[i].to]) Q.push (e[i].to); if (vis[e[i].to]==u) Continue if (F[e[i].to]<f[u]+si[e[i].to]) {f[e[i].to]=f[u]+si[e[i].to]; G[e[i].to]=g[u]; } else if (F[e[i].to]==f[u]+si[e[i].to]) g[e[i].to]= (G[e[i].to]+g[u])%mod; Vis[e[i].to]=u; }} for (int i=1;i<=col;i++) {if (f[i]>ans1) ans1=f[i],ans2=g[i]; else if (f[i]==ans1) ans2= (ans2+g[i])%mod; } printf ("%d\n%d\n", ans1,ans2); return 0;}
Bzoj 1093: [ZJOI2007] Maximum semi-connected sub-graph "tarjan+ topology sort +DP"