zoukankan      html  css  js  c++  java
  • [SHOI2016]黑暗前的幻想乡

    嘟嘟嘟


    这题昨天看觉得没有思路,今天看了一眼觉得就是个水题。


    首先如果不考虑每一个人只能选一条路的话,那就是求一张无向图(有重边,有自环)的生成树个数。这个直接用矩阵树定理+高斯消元求解行列式即可解决。
    现在有了限制,怎么办?
    容斥!
    其实和[ZJOI2016]小星星这道题有点像。
    想一下,如果一个公司修了两条路,那么一定会有一个公司哪条路都没修。所以我们容斥一下:每一个公司修一条路的方案数=至少有0个公司没有修路的方案数-至少有1个公司没修路+至少2个公司没修路-至少3个公司没修路……所以(O(2 ^n))枚举没修路的公司,然后每一次用修路的公司构造基尔霍夫矩阵,然后高斯消元求解。
    复杂度(O(2 ^ n * n ^ 3))。实际上跑的很快。


    写完后debug了一会儿,发现是因为邻接矩阵我都赋值成了1,实际上应该遇到一条边就+1。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<assert.h>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 20;
    const ll mod = 1e9 + 7;
    In ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    In void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    In void MYFILE()
    {
    #ifndef mrclr
      freopen(".in", "r", stdin);
      freopen(".out", "w", stdout);
    #endif
    }
    
    int n;
    int E[maxn][maxn * maxn][2], cnt[maxn];
    
    In ll inc(ll a, ll b) {return a + b < mod ? a + b : a + b - mod;}
    In ll quickpow(ll a, ll b)
    {
      ll ret = 1;
      for(; b; b >>= 1, a = a * a % mod)
        if(b & 1) ret = ret * a % mod;
      return ret;
    }
    
    int A[maxn][maxn], D[maxn][maxn];
    In void build(int pos)
    {
      for(int i = 1; i <= cnt[pos]; ++i)
        {
          int x = E[pos][i][0], y = E[pos][i][1];
          ++A[x][y]; ++A[y][x];
          ++D[x][x], ++D[y][y];
        }
    }
    
    ll f[maxn][maxn];
    In ll Gauss()
    {
      int N = n - 1;
      for(int i = 1; i <= N; ++i)
        for(int j = 1; j <= N; ++j) f[i][j] = inc(D[i][j], mod - A[i][j]);
      for(int i = 1; i <= N; ++i)
        {
          int pos = 0;
          for(int j = i; j <= N && !pos; ++j) if(f[i][j]) pos = j;
          if(!pos) return 0;
          if(pos ^ i) swap(f[i], f[pos]);
          ll inv = quickpow(f[i][i], mod - 2);
          for(int j = i + 1; j <= N; ++j)
    		{
    		  ll tp = f[j][i] * inv % mod;
    		  for(int k = i; k <= N; ++k) f[j][k] = inc(f[j][k], mod - tp * f[i][k] % mod);
    		}
        }
      ll ans = 1;
      for(int i = 1; i <= N; ++i) ans = ans * f[i][i] % mod;
      return ans;
    }
    
    int main()
    {
      MYFILE();
      n = read();
      for(int i = 1; i < n; ++i)
        {
          cnt[i] = read();
          for(int j = 1; j <= cnt[i]; ++j) E[i][j][0] = read(), E[i][j][1] = read();
        }
      ll ans = 0;
      for(int S = 0; S < (1 << (n - 1)); ++S)
        {
          int cnt = n - 1;
          for(int i = 1; i < n; ++i)
    		if((S >> (i - 1)) & 1) build(i), --cnt;
          ans = inc(ans, (cnt & 1) ? mod - Gauss() : Gauss());
          Mem(A, 0), Mem(D, 0);
        }
      write(ans), enter;
      return 0;
    }
    
  • 相关阅读:
    判断java中两个对象是否相等
    面试题记录
    springboot集成redis操作
    Java 之Integer相等比较
    CSS+DIV网页样式与布局:第二章:CSS的基本语法
    JSP标签:jsp内置标签、jstl标签、自定义标签
    jsp jstl标签库 el表达式
    mysql数据库修改字段类型
    读CSS DIV网页样式与布局心得体会
    Absolute(绝对定位)与relative(相对定位)的图文讲解
  • 原文地址:https://www.cnblogs.com/mrclr/p/10960566.html
Copyright © 2011-2022 走看看