1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 5006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; const int P = 998244353; int n , m , k; vi G[MAXN] , rG[MAXN];
int dfn[MAXN] , low[MAXN] , clo , ins[MAXN] , stk[MAXN] , tp , bel[MAXN] , scc; vi poi[MAXN]; void tarjan( int u ) { dfn[u] = low[u] = ++ clo; ins[u] = 1 , stk[++ tp] = u; for( int v : G[u] ) { if( !dfn[v] ) tarjan( v ) , low[u] = min( low[u] , low[v] ); else if( ins[v] ) low[u] = min( low[u] , dfn[v] ); } if( dfn[u] == low[u] ) { int x; ++ scc; do { x = stk[tp --]; poi[scc].pb( x ); bel[x] = scc , ins[x] = 0; } while( x != u ); reverse( all( poi[scc] ) ); } }
int dp[MAXN][MAXN] , pr[MAXN][MAXN] , pd[MAXN][MAXN] , tm[MAXN]; void Inc( int& a , int b ) { a = a + b < P ? a + b : a + b - P; } int le; int calc( int i , int j ) { if( i > j ) swap( i , j ); return ( ( 1ll * pr[i][j] - pr[i][i] + pr[le][j] - pr[le][i] - pr[j][j] + pr[j][i] ) % P + P ) % P; }
void solve() { cin >> n >> m >> k; rep( i , 1 , m ) { int u , v; scanf("%d%d",&u,&v); G[u].pb( v ) , rG[v].pb( u ); } if( !k ) return void( puts("0") ); rep( i , 1 , n ) G[n + 1].pb( i ) , rG[i].pb( n + 1 ); ++ n; rep( i , 1 , n ) if( !dfn[i] ) tarjan( i );
dp[n][n] = 1;
per( i , scc - 1 , 1 ) { for( int u : poi[i] ) rep( t , 1 , n ) if( bel[t] > i ) { for( int v : rG[u] ) if( min( bel[v] , bel[t] ) == i + 1 ) Inc( dp[u][t] , dp[v][t] ) , Inc( dp[t][u] , dp[v][t] ); } for( int u : poi[i] ) for( int v : poi[i] ) { for( int x : rG[u] ) if( bel[x] > i ) Inc( dp[u][v] , dp[x][v] ); } if( poi[i].size() == 1 ) { if( k == 1 ) dp[poi[i][0]][poi[i][0]] = 0; continue; } le = poi[i].size(); rep( x , 1 , n ) if( bel[x] > i ) { int sum = 0; for( int u : poi[i] ) tm[u] = dp[x][u] , Inc( sum , dp[x][u] ); sum = sum * 1ll * ( k - 1 ) % P; rep( j , 0 , le - 1 ) dp[x][poi[i][j]] = dp[poi[i][j]][x] = ( sum + tm[poi[i][( j + 1 ) % le]] ) % P; }
rep( j , 0 , le - 1 ) rep( t , 0 , le - 1 ) pr[j + 1][t + 1] = dp[poi[i][j]][poi[i][t]]; if( k == 1 ) { rep( j , 0 , le - 1 ) rep( t , 0 , le - 1 ) dp[poi[i][j]][poi[i][t]] = ( j == t ? 0 : pr[( t + 1 ) % le + 1][( j + 1 ) % le + 1] ); continue; } rep( j , 1 , le ) rep( t , 1 , le ) Inc( pr[j][t] , pr[j][t - 1] ); rep( j , 1 , le ) rep( t , 1 , le ) { int nxj = j % le + 1 , nxt = t % le + 1; pd[j][t] = ( pr[nxj][le] + pr[nxt][le] + 1ll * P - dp[poi[i][nxj - 1]][poi[i][nxt - 1]] ) % P; } rep( j , 1 , le ) rep( t , 1 , le ) Inc( pr[j][t] , pr[j - 1][t ] ); int sum = pr[le][le] * 1ll * ( ( k - 2 ) * 1ll * ( k + 1 ) / 2 % P ) % P;
rep( j , 1 , le ) rep( t , 1 , le ) if( j != t ) pd[j][t] = ( pd[j][t] + k * 1ll * calc( t , j ) + calc( t % le + 1 , j % le + 1 ) ) % P; rep( j , 1 , le ) rep( t , 1 , le ) dp[poi[i][j - 1]][poi[i][t - 1]] = ( pd[j][t] + sum ) % P; } int res = 0; rep( i , 1 , n ) rep( j , 1 , n ) if( min( bel[i] , bel[j] ) == 1 ) Inc( res , dp[i][j] ); cout << res << endl; }
signed main() { freopen("travel.in","r",stdin); freopen("travel.out","w",stdout);
solve(); }
|