四个选项

链接:https://ac.nowcoder.com/acm/contest/5026/C
先dfs一遍,求出每个连通块有多少个点,把这个东西作为一个连通块的体积,一个连通块就是一个物品,这样就是在求恰好装满这4个体积分别为na,nb,nc,nd的背包有多少种方案。
dp[i][x1][x2][x3][x4]表示枚举到i的时候,四个背包被装填的体积分别为x1,x2,x3,x4的方案数。

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define debug(x) cout << #x << ": " << x << endl;
#define all(__vv__) (__vv__).begin(), (__vv__).end()
#define endl "\n"
#define SZ(x) ((int)(x).size())
#define pb push_back
#define pii pair<int, int>
#define mem(__x__,__val__) memset(__x__, __val__, sizeof(__x__))
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar())    s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; }
inline void print(ll x, int op = 10) { if (!x) { putchar('0'); if (op)    putchar(op); return; }    char F[40]; ll tmp = x > 0 ? x : -x;    if (x < 0)putchar('-');    int cnt = 0;    while (tmp > 0) { F[cnt++] = tmp % 10 + '0';        tmp /= 10; }    while (cnt > 0)putchar(F[--cnt]);    if (op)    putchar(op); }
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll qpow(ll a, ll b) { ll ans = 1;    while (b) { if (b & 1)    ans *= a;        b >>= 1;        a *= a; }    return ans; }    ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; }
const int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int N = 20;

int na, nb, nc, nd, m, x, y, v[N], dp[N][N][N][N][N];
bool vis[N]; 
vector<int> e[N];

int dfs(int u)
{
    int res = 0;
    vis[u] = 1; res++;
    for(auto &vv : e[u]){
        if(vis[vv]) continue;
        res += dfs(vv); 
    }
    return res;
}
int main()
{
    IO;
    mem(vis,0); mem(dp, 0);
    cin >> na >> nb >> nc >> nd >> m;
    while(m--)
    {
        cin >> x >> y;
        e[x].pb(y); e[y].pb(x);
    }
    int tot = 0;
    for(int i=1; i<=12; i++){
        if(vis[i]) continue;
        v[++tot] = dfs(i);
    }
    dp[0][0][0][0][0] = 1;
    for(int i=1; i<=tot; i++)
        for(int a=0; a<=na; a++)
            for(int b=0; b<=nb; b++)
                for(int c=0; c<=nc; c++)
                    for(int d=0; d<=nd; d++)
                    {
                        if(a+v[i] <= na) dp[i][a+v[i]][b][c][d]+=dp[i-1][a][b][c][d];
                        if(b+v[i] <= nb) dp[i][a][b+v[i]][c][d]+=dp[i-1][a][b][c][d];
                        if(c+v[i] <= nc) dp[i][a][b][c+v[i]][d]+=dp[i-1][a][b][c][d];
                        if(d+v[i] <= nd) dp[i][a][b][c][d+v[i]]+=dp[i-1][a][b][c][d];
                    }
    cout << dp[tot][na][nb][nc][nd] << endl;
}
全部评论

相关推荐

Noob1024:一笔传三代,人走笔还在
点赞 评论 收藏
分享
3 收藏 评论
分享
牛客网
牛客企业服务