题意: 链接.
方法: 强连通分量tarjan缩点 + 拓扑dp .
样例图:
解析:
这题的题意读明白后紧接着对样例做分析
样例输出的3 3是代表最多有三个点,有三个有三个点的情况。
三种情况分别是
5 6 4
1 2 4
2 1 3
我们发现这三种情况都是有三个点的三条链,于是就往链的方向去想。
多次尝试之后发现,如果能构成题中所符合的情况,那么这几个点必定组成一条链。
所以,显而易见,这道题的两问分别转变为:求最长链的点数,有几个这样的最长链
注意:
缩点后再继续处理,并且要考虑每一个强连通分量的点的个数。
#include <queue>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
int from ;
int to ;
int next ;
};
node edge[1000001] ;
node edge_short[1000001] ;
int head[100001] ;
int head_short[100001] ;
int deep[100001] ;
int low[100001] ;
int z[100001] ;
int inz[100001] ;
int belong[100001] ;
int in[100001] ;
int v[100001];
int num[100001] ;
int dfn[100001] ;
int dp[100001] ;
int point[100001] ;
int cnt , ans , tot , top , cnt_short;
int n , m , MOD , assassin , poi;
void init()
{
memset(head , -1 , sizeof(head)) ;
memset(head_short , -1 , sizeof(head_short)) ;
cnt = 1 ;
cnt_short = 1 ;
}
void edgeadd(int from , int to)
{
edge[cnt].from = from ;
edge[cnt].to = to ;
edge[cnt].next = head[from] ;
head[from] = cnt ++ ;
}
void edgeadd_short(int from , int to)
{
edge_short[cnt_short].from = from ;
edge_short[cnt_short].to = to ;
edge_short[cnt_short].next = head_short[from] ;
head_short[from] = cnt_short ++ ;
}
void tarjan(int x)
{
deep[x] = low[x] = ++tot ;
z[++top] = x , inz[x] = 1 ;
for(int i = head[x] ; i != -1 ; i = edge[i].next)
{
int to = edge[i].to ;
if(!deep[to])
{
tarjan(to) ;
low[x] = min(low[x] , low[to]) ;
}else if(inz[to]) low[x] = min(low[x] , deep[to]) ;
}
if(deep[x] == low[x])
{
ans ++ ;
int t;
do
{
t = z[top--] , inz[t] = 0 ;
belong[t] = ans ;
num[ans] ++ ;
}while(t != x) ;
}
}
void topsort()
{
queue <int> q;
for(int i = 1 ; i <= ans ; i++)
{
if(!in[i])
{
q.push(i) ;
point[i] = 1 ;
dp[i] = num[i] ;
}
}
while(q.size())
{
int s = q.front() ;
q.pop() ;
for(int i = head_short[s] ; i != -1 ; i = edge_short[i].next)
{
int to = edge_short[i].to ;
in[to]-- ;
if(!in[to]) q.push(to) ;
if(v[to] == s) continue ;
if(dp[to] < dp[s] + num[to])
{
dp[to] = dp[s] + num[to] ;
point[to] = point[s] ;
}else if(dp[to] == dp[s] + num[to])
{
point[to] = (point[to] + point[s]) % MOD ;
}
v[to] = s ;
}
}
}
/*
void dfs1(int x)
{
v[x] = 1 ;
for(int i = head_short[x] ; i != -1 ; i = edge_short[i].next)
{
int to = edge_short[i].to ;
if(v[to] == 0) dfs1(to) ;
dfn[x] = max(dfn[x] , dfn[to]) ;
}
dfn[x] += num[x] ;
}
void dfs2(int x , int dep , int fa)
{
if(dep == poi) assassin = (assassin + 1)%MOD ;
for(int i = head[x] ; i != -1 ; i = edge[i].next)
{
int to = edge[i].to ;
if(to == fa) continue ;
dfs2(to , dep + 1 , x) ;
}
}*/
int main()
{
scanf("%d%d%d" , &n , &m , &MOD) ;
init() ;
for(int i = 1 ; i <= m ; i++)
{
int x , y ;
scanf("%d%d" , &x , &y) ;
edgeadd(x , y) ;
}
for(int i = 1 ; i <= n ; i++)
{
if(!deep[i]) tarjan(i) ;
}
for(int i = 1 ; i <= cnt ; i++)
{
int x = edge[i].from , y = edge[i].to ;
if(belong[x] != belong[y])
{
in[belong[y]] ++ ;
edgeadd_short(belong[x] , belong[y]) ;
}
}
topsort() ;
for(int i = 1 ; i <= ans ; i++)
{
poi = max(poi , dp[i]) ;
}
for(int i = 1 ; i <= ans ; i++)
{
if(dp[i] == poi)
{
assassin = (assassin + point[i]) % MOD ;
}
}
/*
for(int i = 1 ; i <= ans ; i++)
{
if(!v[i]) dfs1(i) ;
poi = max(poi , dfn[i]) ;
}
for(int i = 1 ; i <= n ; i++)
{
dfs2(i , 1 , 0) ;
}*/
printf("%d\n%d\n" , poi , assassin) ;
}