HDU - 5765 Bonds thinking + sosdp (subset and dp)

HDU - 5765

A bond sure the communication is divided into two points n blocks, and each set corresponds to a bond, since the set current block belongs to the first communication, 

Then its complement another input communication on the block, ok [mask] represents the set of points mask can become a communication block.

We call a mask allocation is good if and only if ok [mask] == true && ok [~ mask] = true;

Then we consider an edge (u, v) belongs to the number of bond, which is always legitimate distribution program minus u, v several programs at the same point in the set.

We consider sosdp seeking a superset and ok.

 

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 3e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = (int)1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}

int n, m, all, G[20];
int U[20 * 20], V[20 * 20];
bool ok[1 << 20];
int c[1 << 20];

void init() {
    all = 0;
    for(int i = 0; i < (1 << n); i++) {
        ok[i] = false;
        c[i] = 0;
    }
    for(int i = 0; i < n; i++) {
        G[i] = 0;
    }
}

int main() {
    int cas = 0;
    int T; scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &m);
        init();
        for(int i = 1; i <= m; i++) {
            scanf("%d%d", &U[i], &V[i]);
            G[U[i]] |= 1 << V[i];
            G[V[i]] |= 1 << U[i];
        }
        ok[0] = true;
        for(int i = 0; i < n; i++) {
            ok[1 << i] = true;
        }
        for(int mask = 0; mask < (1 << n); mask++) {
            if(!ok[mask]) continue;
            for(int i = 0; i < n; i++) {
                if(mask >> i & 1) continue;
                if(!(G[i] & mask)) continue;
                ok[mask | (1 << i)] = true;
            }
        }
        for(int mask = 0; mask < (1 << n); mask++) {
            int fmask = (~mask) & ((1 << n) - 1);
            if(!ok[mask] || !ok[fmask]) continue;
            all++;
            c[mask]++;
        }
        for(int i = 0; i < n; i++) {
            for(int mask = 0; mask < (1 << n); mask++) {
                if(mask >> i & 1) {
                    c[mask ^ (1 << i)] += c[mask];
                }
            }
        }
        all >>= 1;
        printf("Case #%d: ", ++cas);
        for(int i = 1; i <= m; i++) {
            int ans = all - c[(1 << U[i]) | (1 << V[i])];
            printf("%d%c", ans, " \n"[i == m]);
        }
    }
    return 0;
}

/*
*/

 

Guess you like

Origin www.cnblogs.com/CJLHY/p/11505694.html