whosyourdaddy
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1800 Accepted Submission(s): 914
Problem Description
sevenzero liked Warcraft very much, but he haven't practiced it for several years after being addicted to algorithms. Now, though he is playing with computer, he nearly losed and only his hero Pit Lord left. sevenzero is angry, he decided to cheat to turn defeat into victory. So he entered "whosyourdaddy", that let Pit Lord kill any hostile unit he damages immediately. As all Warcrafters know, Pit Lord masters a skill called Cleaving Attack and he can damage neighbour units of the unit he attacks. Pit Lord can choice a position to attack to avoid killing partial neighbour units sevenzero don't want to kill. Because sevenzero wants to win as soon as possible, he needs to know the minimum attack times to eliminate all the enemys.
Input
There are several cases. For each case, first line contains two integer N (2 ≤ N ≤ 55) and M (0 ≤ M ≤ N*N),and N is the number of hostile units. Hostile units are numbered from 1 to N. For the subsequent M lines, each line contains two integers A and B, that means A and B are neighbor. Each unit has no more than 4 neighbor units. The input is terminated by EOF.
Output
One line shows the minimum attack times for each case.
Sample Input
5 4 1 2 1 3 2 4 4 5 6 4 1 2 1 3 1 4 4 5
Sample Output
2 3
Author
sevenzero
Source
Recommend
zhouzeyong
题意:n个点,m条无向边,删除一个点会把与其相邻的点一起删掉,问最少删几次可以删掉所有点
解题思路:舞蹈链重复覆盖
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 500005;
int n, m, x, y, tot;
int s[60], nt[7200], e[7200];
struct DLX
{
int L[maxn], R[maxn], U[maxn], D[maxn];
int row[maxn], col[maxn], sum[maxn], ans[maxn];
int n, m, cnt, num;
int vis[maxn];
void add(int k, int l, int r, int u, int d, int x, int y)
{
L[k] = l; R[k] = r; U[k] = u;
D[k] = d; row[k] = x; col[k] = y;
}
void reset(int n, int m)
{
num = 0x7FFFFFFF;
this->n = n; this->m = m;
for (int i = 0; i <= m; i++)
{
add(i, i - 1, i + 1, i, i, 0, i);
sum[i] = 0;
}
L[0] = m, R[m] = 0, cnt = m + 1;
}
void insert(int x, int y)
{
int temp = cnt - 1;
if (row[temp] != x)
{
add(cnt, cnt, cnt, U[y], y, x, y);
U[D[cnt]] = cnt; D[U[cnt]] = cnt;
}
else
{
add(cnt, temp, R[temp], U[y], y, x, y);
R[L[cnt]] = cnt; L[R[cnt]] = cnt;
U[D[cnt]] = cnt; D[U[cnt]] = cnt;
}
sum[y]++, cnt++;
}
void Remove(int k)
{
for (int i = D[k]; i != k; i = D[i])
{
L[R[i]] = L[i];
R[L[i]] = R[i];
}
}
void Resume(int k)
{
for (int i = U[k]; i != k; i = U[i]) L[R[i]] = R[L[i]] = i;
}
int A()
{
int dis = 0;
for (int i = R[0]; i != 0; i = R[i]) vis[i] = 0;
for (int i = R[0]; i != 0; i = R[i])
if (!vis[i])
{
dis++, vis[i] = 1;
for (int j = D[i]; j != i; j = D[j])
for (int k = R[j]; k != j; k = R[k])
vis[col[k]] = 1;
}
return dis;
}
void Dfs(int k)
{
if (!R[0]) { num = min(num, k); return ; }
else if (k + A() < num)
{
int now = R[0];
for (int i = R[0]; i != 0; i = R[i])
if (sum[now] > sum[i]) now = i;
for (int i = D[now]; i != now; i = D[i])
{
Remove(i);
for (int j = R[i]; j != i; j = R[j]) Remove(j);
Dfs(k + 1);
for (int j = L[i]; j != i; j = L[j]) Resume(j);
Resume(i);
}
}
}
}dlx;
int main()
{
while (~scanf("%d%d",&n,&m))
{
memset(s, -1, sizeof s);
tot = 0;
dlx.reset(n, n);
for (int i = 1; i <= m; i++)
{
scanf("%d%d", &x, &y);
nt[tot] = s[x], s[x] = tot, e[tot++] = y;
nt[tot] = s[y], s[y] = tot, e[tot++] = x;
}
for (int i = 1; i <= n; i++)
{
for (int j = s[i]; ~j; j = nt[j]) dlx.insert(i, e[j]);
dlx.insert(i, i);
}
dlx.Dfs(0);
printf("%d\n", dlx.num);
}
return 0;
}