hdu - 3498 - whosyourdaddy(重复覆盖DLX)
来源:程序员人生 发布时间:2014-11-07 08:31:03 阅读次数:1958次
题意:N(2 ≤ N ≤ 55)个点,M(0 ≤ M ≤ N*N)条无向边,删除1个点会把与其相邻的点1起删掉,问最少删几次可以删掉所有点。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3498
――>>N个点看成 N 个要被覆盖的列,每一个点作为1行,与其相邻的点的位置在这1行中标为 1,还有它自已的位置也标记为 1。。
这就是经典的重复覆盖问题了。。因而,DLX上场。。
#include <cstdio>
#include <cstring>
const int MAXR = 55 + 10;
const int MAXC = 55 + 10;
const int MAXNODE = MAXR * MAXC;
const int INF = 0x3f3f3f3f;
struct DLX
{
int sz;
int H[MAXR], S[MAXC];
int row[MAXNODE], col[MAXNODE];
int U[MAXNODE], D[MAXNODE], L[MAXNODE], R[MAXNODE];
int Min;
void Init(int n)
{
for (int i = 0; i <= n; ++i)
{
U[i] = D[i] = i;
L[i] = i - 1;
R[i] = i + 1;
}
L[0] = n;
R[n] = 0;
sz = n + 1;
memset(S, 0, sizeof(S));
memset(H, ⑴, sizeof(H));
}
void Link(const int& r, const int& c)
{
row[sz] = r;
col[sz] = c;
D[sz] = D[c];
U[D[c]] = sz;
D[c] = sz;
U[sz] = c;
if (H[r] == ⑴)
{
H[r] = L[sz] = R[sz] = sz;
}
else
{
R[sz] = R[H[r]];
L[R[H[r]]] = sz;
R[H[r]] = sz;
L[sz] = H[r];
}
S[c]++;
sz++;
}
void Remove(const int& c)
{
for (int i = D[c]; i != c; i = D[i])
{
L[R[i]] = L[i];
R[L[i]] = R[i];
}
}
void Restore(const int& c)
{
for (int i = U[c]; i != c; i = U[i])
{
L[R[i]] = i;
R[L[i]] = i;
}
}
int A()
{
int ret = 0;
bool vis[MAXC];
memset(vis, 0, sizeof(vis));
for (int i = R[0]; i != 0; i = R[i])
{
if (!vis[i])
{
vis[i] = true;
++ret;
for (int j = D[i]; j != i; j = D[j])
{
for (int k = R[j]; k != j; k = R[k])
{
vis[col[k]] = true;
}
}
}
}
return ret;
}
void Dfs(int cur)
{
if (cur + A() >= Min) return;
if (R[0] == 0)
{
if (cur < Min)
{
Min = cur;
}
return;
}
int c = R[0];
for (int i = R[0]; i != 0; i = R[i])
{
if (S[i] < S[c])
{
c = i;
}
}
for (int i = D[c]; i != c; i = D[i])
{
Remove(i);
for (int j = R[i]; j != i; j = R[j])
{
Remove(j);
}
Dfs(cur + 1);
for (int j = L[i]; j != i; j = L[j])
{
Restore(j);
}
Restore(i);
}
}
void Solve()
{
Min = INF;
Dfs(0);
printf("%d
", Min);
}
} dlx;
int N, M;
void Read()
{
int a, b;
dlx.Init(N);
while (M--)
{
scanf("%d%d", &a, &b);
dlx.Link(a, b);
dlx.Link(b, a);
}
for (int i = 1; i <= N; ++i)
{
dlx.Link(i, i);
}
}
int main()
{
while (scanf("%d%d", &N, &M) == 2)
{
Read();
dlx.Solve();
}
return 0;
}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠