码迷,mamicode.com
首页 > 其他好文 > 详细

[HDU 5304] 基环树计数

时间:2017-09-11 10:14:34      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:code   分析   define   wap   class   reg   etc   tin   ++   

题意

  给定一张 n 个点 m 条边的无向图, 问其有多少个生成基环树.

  n <= 16 , 无重边, 无自环.

 

分析

  状压DP 处理每个状态对应的环的个数.

  枚举所有状态, 缩点后重标号, 使用 Matrix 定理计算答案.

 

实现

  枚举环:

    f[s][i] 表示从 s 状态中的最小值出发, 终点在 i 的方案数.

    边界 f[2 ^ i][i] = 1 .

    统计答案的时候, 对于 f[s][i] , 若 s 中的个数大于 2 , 且 i 能到达 s 状态中的最小值, 则 cnt[s] += f[s][i] .

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <cctype>
  5 #include <algorithm>
  6 using namespace std;
  7 #define F(i, a, b) for (register int i = (a); i <= (b); i++)
  8 #define D(i, a, b) for (register int i = (a); i >= (b); i--)
  9 inline int rd(void) {
 10     int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == -) f = -1;
 11     int x = 0; for (; isdigit(c); c = getchar()) x = x*10+c-0; return x*f;
 12 }
 13 
 14 const int N = 20;
 15 const int S = 70000;
 16 const int MOD = 998244353;
 17 
 18 inline void Add(int &x, int y) { x = (x + y) % MOD; }
 19 inline int Pow(int x, int y) {
 20     int Mul = 1;
 21     for (; y > 0; y >>= 1, x = 1LL * x * x % MOD)
 22         if (y & 1) Mul = 1LL * Mul * x % MOD;
 23     return Mul;
 24 }
 25 inline int Inv(int x) { return Pow(x, MOD-2); }
 26 
 27 int n, m; bool G[N][N];
 28 int Full, cnt[S], f[S][N];
 29 int Lab[N], tot, Mat[N][N], ans;
 30 
 31 void Build(int s) {
 32     tot = 1; F(i, 1, n) Lab[i] = (s >> i-1 & 1 ? 1 : ++tot);
 33     memset(Mat, 0, sizeof Mat);
 34     F(i, 1, n) F(j, i+1, n)
 35         if (G[i][j] && Lab[i] != Lab[j]) {
 36             Mat[Lab[i]][Lab[i]]++, Mat[Lab[j]][Lab[j]]++;
 37             Mat[Lab[i]][Lab[j]]--, Mat[Lab[j]][Lab[i]]--;
 38         }
 39 }
 40 int Gauss(void) {
 41     tot--;
 42     int Mul = 1;
 43     F(i, 1, tot) {
 44         if (!Mat[i][i]) {
 45             F(j, i+1, tot) if (Mat[j][i] != 0) {
 46                 F(k, 1, tot) swap(Mat[i][k], Mat[j][k]);
 47                 Mul = -Mul;
 48                 break;
 49             }
 50         }
 51         if (!Mat[i][i]) return 0;
 52         Mul = 1LL * Mul * Mat[i][i] % MOD;
 53         int I = Inv(Mat[i][i]);
 54         F(j, i+1, tot) if (Mat[j][i] != 0) {
 55             int t = 1LL * I * Mat[j][i] % MOD;
 56             F(k, i, tot)
 57                 Add(Mat[j][k], -1LL * Mat[i][k] * t % MOD);
 58         }
 59     }
 60     return Mul;
 61 }
 62 
 63 int main(void) {
 64     #ifndef ONLINE_JUDGE
 65         freopen("hdu5304.in", "r", stdin);
 66     #endif
 67     
 68     int inv2 = Inv(2);
 69     while (~scanf("%d %d", &n, &m)) {
 70         memset(G, false, sizeof G);
 71         F(i, 1, m) {
 72             int x = rd(), y = rd();
 73             G[x][y] = G[y][x] = true;
 74         }
 75         
 76         Full = (1 << n) - 1, memset(cnt, 0, sizeof cnt), memset(f, 0, sizeof f);
 77         F(i, 1, n) f[1 << i-1][i] = 1;
 78         F(s, 1, Full) {
 79             int Min = 0; D(i, n, 1) if (s >> i-1 & 1) Min = i;
 80             F(i, Min, n) if ((s >> i-1 & 1) && f[s][i] != 0) {
 81                 F(j, Min+1, n) if (!(s >> j-1 & 1) && G[i][j])
 82                     Add(f[s | 1 << j-1][j], f[s][i]);
 83             }
 84         }
 85         F(s, 1, Full) {
 86             int tot = 0; for (int x = s; x > 0 && tot <= 2; x ^= (x & -x), tot++); if (tot <= 2) continue;
 87             int Min = 0; D(i, n, 1) if (s >> i-1 & 1) Min = i;
 88             F(i, Min+1, n) if ((s >> i-1 & 1) && G[i][Min])
 89                 Add(cnt[s], f[s][i]);
 90             cnt[s] = 1LL * cnt[s] * inv2 % MOD;
 91         }
 92         
 93         ans = 0;
 94         F(s, 1, Full) if (cnt[s] != 0) {
 95             Build(s);
 96             Add(ans, 1LL * cnt[s] * Gauss() % MOD);
 97         }
 98         printf("%d\n", (ans + MOD) % MOD);
 99     }
100     
101     return 0;
102 }

 

[HDU 5304] 基环树计数

标签:code   分析   define   wap   class   reg   etc   tin   ++   

原文地址:http://www.cnblogs.com/Sdchr/p/7503125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!