【HDU】5304 Eastest Magical Day Seep Group's Summer【环dp+生成树计数】

传送门:【HDU】5304 Eastest Magical Day Seep Group’s Summer

my  code:

#include <bits/stdc++.h>
using namespace std ;

typedef long long LL ;

#define clr( a , x ) memset ( a , x , sizeof a )

const int MAXN = 16 ;
const int mod = 998244353 ;
const int v2 = 499122177 ;

int n , m ;
bool G[MAXN][MAXN] ;
int mat[MAXN][MAXN] ;
int dp[1 << MAXN][MAXN] , cnt[1 << MAXN] ;
bool is[MAXN] ;
int S[MAXN] , top ;
int ans ;

void add ( int& x , int y ) {
    x += y ;
    if ( x >= mod ) x -= mod ;
}

void preprocess () {
    clr ( cnt , 0 ) ;
    for ( int i = 0 ; i < n - 2 ; ++ i ) {
        int n1 = n - i - 1 ;
        int S = 1 << n1 ;
        for ( int s = 0 ; s < S ; ++ s ) clr ( dp[s] , 0 ) ;
        for ( int j = 0 ; j < n1 ; ++ j ) if ( G[i][j + i + 1] ) dp[1 << j][j] = 1 ;
        for ( int s = 0 ; s < S ; ++ s ) {
            for ( int j = 0 ; j < n1 ; ++ j ) if ( dp[s][j] ) {
                if ( G[j + i + 1][i] && __builtin_popcount ( s ) >= 2 ) {
                    add ( cnt[( s << 1 | 1 ) << i] , dp[s][j] ) ;
                }
                for ( int k = 0 ; k < n1 ; ++ k ) {
                    int kk = 1 << k ;
                    if ( s & kk ) continue ;
                    if ( !G[j + i + 1][k + i + 1] ) continue ;
                    add ( dp[s | kk][k] , dp[s][j] ) ;
                }
            }
        }
    }
    int S = 1 << n ;
    for ( int i = 0 ; i < S ; ++ i ) cnt[i] = ( LL ) cnt[i] * v2 % mod ;
}

void subbuild ( int x , int y ) {
    -- mat[x][y] ;
    if ( mat[x][y] < 0 ) mat[x][y] += mod ;
    ++ mat[x][x] ;
}

int exgcd ( int a , int b , int& x , int& y ) {
    if ( b ) {
        exgcd ( b , a % b , y , x ) ;
        y -= a / b * x ;
    } else {
        x = 1 ;
        y = 0 ;
    }
}

int inv ( int a , int mod ) {
    int x , y , b = mod ;
    exgcd ( a , b , x , y ) ;
    if ( x < 0 ) x += mod ;
    return x ;
}

int det ( int n ) {
    int res = 1 ;
    for ( int i = 0 ; i < n ; ++ i ) {
        for ( int j = i ; j < n ; ++ j ) {
            if ( mat[j][i] ) {
                if ( i != j ) {
                    for ( int k = i ; k < n ; ++ k ) swap ( mat[i][k] , mat[j][k] ) ;
                    res = -res ;
                    if ( res < 0 ) res += mod ;
                }
                break ;
            }
        }
        if ( !mat[i][i] ) return 0 ;
        for ( int j = i + 1 ; j < n ; ++ j ) {
            int mut = ( LL ) mat[j][i] * inv ( mat[i][i] , mod ) % mod ;
            for ( int k = i ; k < n ; ++ k ) {
                mat[j][k] -= ( LL ) mat[i][k] * mut % mod ;
                if ( mat[j][k] < 0 ) mat[j][k] += mod ;
            }
        }
        res = ( LL ) res * mat[i][i] % mod ;
    }
    return res ;
}

void calc ( int s ) {
    top = 0 ;
    for ( int i = 0 ; i < n ; ++ i ) if ( !is[i] ) S[i] = top ++ ;
    for ( int i = 0 ; i < top ; ++ i ) {
        for ( int j = 0 ; j < top ; ++ j ) mat[i][j] = 0 ;
    }
    for ( int i = 0 ; i < n ; ++ i ) {
        for ( int j = 0 ; j < n ; ++ j ) if ( i != j && G[i][j] ) {
            if ( is[i] && is[j] ) continue ;
            if ( is[i] ) subbuild ( top , S[j] ) ;
            else if ( is[j] ) subbuild ( S[i] , top ) ;
            else subbuild ( S[i] , S[j] ) ;
        }
    }
    ans = ( ans + ( LL ) cnt[s] * det ( top ) ) % mod ;
}

void dfs ( int cur , int s ) {
    if ( cur == n ) {
        calc ( s ) ;
        return ;
    }
    is[cur] = 0 ;
    dfs ( cur + 1 , s ) ;
    is[cur] = 1 ;
    dfs ( cur + 1 , s | ( 1 << cur ) ) ;
}

void solve () {
    int u , v ;
    ans = 0 ;
    clr ( G , 0 ) ;
    for ( int i = 0 ; i < m ; ++ i ) {
        scanf ( "%d%d" , &u , &v ) ;
        -- u , -- v ;
        G[u][v] = G[v][u] = true ;
    }
    preprocess () ;
    dfs ( 0 , 0 ) ;
    printf ( "%d\n" , ans ) ;
}

int main () {
    while ( ~scanf ( "%d%d" , &n , &m ) ) solve () ;
    return 0 ;
}

你可能感兴趣的:(【HDU】5304 Eastest Magical Day Seep Group's Summer【环dp+生成树计数】)