[HDOJ6149] Valley Numer II(枚举,状压DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149
比赛时这题想歪到最大流了,实际上高点至多只有15个。因此可以枚举低点,再枚举低点相连的高点做dp。
f(i,j)代表前i个低点,此时高点使用状态为j时的最多三元组数。事先预处理每一个高点在bit位中的位置,dp的时候直接枚举任意两个不等的高点,此时状态st,再枚举所有状态,把j^st的状态更新到j上就行,因为低点可能有间隔,所以用滚动数组即可。
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <cassert> 24 #include <cstdio> 25 #include <bitset> 26 #include <vector> 27 #include <deque> 28 #include <queue> 29 #include <stack> 30 #include <ctime> 31 #include <set> 32 #include <map> 33 #include <cmath> 34 using namespace std; 35 #define fr first 36 #define sc second 37 #define cl clear 38 #define BUG puts("here!!!") 39 #define W(a) while(a--) 40 #define pb(a) push_back(a) 41 #define Rint(a) scanf("%d", &a) 42 #define Rll(a) scanf("%lld", &a) 43 #define Rs(a) scanf("%s", a) 44 #define Cin(a) cin >> a 45 #define FRead() freopen("in", "r", stdin) 46 #define FWrite() freopen("out", "w", stdout) 47 #define Rep(i, len) for(int i = 0; i < (len); i++) 48 #define For(i, a, len) for(int i = (a); i < (len); i++) 49 #define Cls(a) memset((a), 0, sizeof(a)) 50 #define Clr(a, x) memset((a), (x), sizeof(a)) 51 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 52 #define lrt rt << 1 53 #define rrt rt << 1 | 1 54 #define pi 3.14159265359 55 #define RT return 56 #define lowbit(x) x & (-x) 57 #define onenum(x) __builtin_popcount(x) 58 typedef long long LL; 59 typedef long double LD; 60 typedef unsigned long long ULL; 61 typedef pair<int, int> pii; 62 typedef pair<string, int> psi; 63 typedef pair<LL, LL> pll; 64 typedef map<string, int> msi; 65 typedef vector<int> vi; 66 typedef vector<LL> vl; 67 typedef vector<vl> vvl; 68 typedef vector<bool> vb; 69 70 const int maxn = 33; 71 const int maxm = 1 << 16; 72 int n, m, k, f[2][maxm]; 73 int mask[maxn]; 74 vi G[maxn]; 75 76 signed main() { 77 // FRead(); 78 int T, u, v; 79 Rint(T); 80 W(T) { 81 Clr(mask, -1); Cls(f); 82 Rint(n); Rint(m); Rint(k); 83 Rep(i, n) G[i].clear(); 84 Rep(i, m) { 85 Rint(u); Rint(v); u--, v--; 86 G[u].pb(v); G[v].pb(u); 87 } 88 Rep(i, k) Rint(u), mask[--u] = i; 89 int kk = 1 << k; 90 Rep(i, n) { 91 if(mask[i] != -1) continue; 92 Rep(j, kk) f[1][j] = f[0][j]; 93 for(auto u : G[i]) { 94 for(auto v : G[i]) { 95 if(u == v) continue; 96 if(mask[u] == -1 || mask[v] == -1) continue; 97 int st = (1 << mask[u]) | (1 << mask[v]); 98 Rep(j, kk) { 99 if((j & st) == st) { 100 f[0][j] = max(f[0][j], f[1][j^st]+1); 101 } 102 } 103 } 104 } 105 } 106 printf("%d\n", f[0][kk-1]); 107 } 108 RT 0; 109 }