qzmoot @ 2024-10-14 18:05:38
#include <bits/stdc++.h>
#define pb push_back
#define P pair<int,int>
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N=1e5+5,M=1e6+5;
int n,m,p;
vector<int>a[N],na[N];
int dfn[N],low[N],idx,vis[N],sd[N],siz[N];
int st[N],top;
int tot;
struct node{
int fr,to;
}e[M];
map<P,int>ma;
void tarjan(int u)
{
low[u]=dfn[u]=++idx;
st[++top]=u;
vis[u]=1;
for(auto v:a[u])
{
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int i;
while(i=st[top--])
{
sd[i]=u;
if(i==u)
break;
}
}
}
int in[N],ou[N],f[N],g[N],book[N];
int maxn,cnt;
void dfs(int u,int fa)
{
book[u]=1;
if(!ou[u])
{
f[u]=siz[u],g[u]=1;
maxn=max(maxn,f[u]);
return;
}
for(auto v:na[u])
{
if(v==fa)
continue;
if(!book[v])
dfs(v,u);
if(f[v]+siz[u]>f[u])
f[u]=f[v]+siz[u],g[u]=g[v]%p;
else if(f[v]+siz[u]==f[u])
g[u]=(g[u]+g[v])%p;
maxn=max(maxn,f[u]);
}
}
int main()
{
scanf("%d%d%d",&n,&m,&p);
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
a[u].pb(v);
e[i]=(node){u,v};
}
for(int i=1;i<=n;i++)
sd[i]=i;
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(int i=1;i<=n;i++)
siz[sd[i]]++;
// for(int i=1;i<=n;i++)
// {
// printf("%d %d %d\n",i,sd[i],siz[sd[i]]);
// }
// return 0;
for(int i=1;i<=m;i++)
{
int u=e[i].fr,v=e[i].to;
if(sd[u]!=sd[v] && !ma[mp(sd[u],sd[v])])
{
na[sd[u]].pb(sd[v]);
ou[sd[u]]++,in[sd[v]]++;
ma[mp(sd[u],sd[v])]=1;
}
}
for(int i=1;i<=n;i++)
if(sd[i]==i && !in[i] && !book[i])
dfs(i,0);
for(int i=1;i<=n;i++)
if(sd[i]==i && maxn==f[i])
cnt=(cnt+g[i])%p;
printf("%d\n%d",maxn,cnt);
return 0;
}
by jiangyunuo @ 2024-10-14 18:06:42
@qzmoot 6
by Netherite_Pickaxe @ 2024-10-14 18:26:01
《可爱萌新妹子》《才学oi》求调《图论》《红名才学oi》
by Autumn_Rain @ 2024-10-14 19:07:39
ta根本就不是妹纸(哭o(╥﹏╥)o