whosyourdaddyTime Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1841 Accepted Submission(s): 946 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 2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT
Recommend zhouzeyong | We have carefully selected several similar problems for you: 3507 3506 3505 3504 3503
|
借鉴:https://www.cnblogs.com/smartweed/p/5877832.html
#include <iostream>
#include <cstdio>
using namespace std;
const int maxnode = 100010; //最多多少个1
const int MaxM = 1010;
const int MaxN = 1010;
struct DLX
{
int n,m,SIZE;
int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];//L,R,D,U四个数组记录某节点上下左右邻居
int H[MaxN], S[MaxM];//H记录行头,S记录某列有多少个节点
int ansd, ans[MaxN];
void init(int _n,int _m)
{
n = _n;
m = _m;
for(int i = 0;i <= m;i++)
{
S[i] = 0;
U[i] = D[i] = i;
L[i] = i-1;
R[i] = i+1;
}
R[m] = 0; L[0] = m;
SIZE = m;
for(int i = 1;i <= n;i++)
H[i] = -1;
}
void Link(int r,int c)
{
++S[Col[++SIZE]=c];
Row[SIZE] = r;
D[SIZE] = D[c];
U[D[c]] = SIZE;
U[SIZE] = c;
D[c] = SIZE;
if(H[r] < 0)H[r] = L[SIZE] = R[SIZE] = SIZE;
else
{
R[SIZE] = R[H[r]];
L[R[H[r]]] = SIZE;
L[SIZE] = H[r];
R[H[r]] = SIZE;
}
}
void exact_remove(int c)
{
L[R[c]] = L[c]; R[L[c]] = R[c];
for(int i = D[c];i != c;i = D[i])
for(int j = R[i];j != i;j = R[j])
{
U[D[j]] = U[j];
D[U[j]] = D[j];
--S[Col[j]];
}
}
void exact_resume(int c)
{
for(int i = U[c];i != c;i = U[i])
for(int j = L[i];j != i;j = L[j])
++S[Col[U[D[j]]=D[U[j]]=j]];
L[R[c]] = R[L[c]] = c;
}
void repeat_remove(int c)
{
for(int i = D[c]; i != c; i = D[i])
L[R[i]] = L[i], R[L[i]] = R[i];
}
void repeat_resume(int c)
{
for(int i = U[c]; i != c; i = U[i])
L[R[i]] = R[L[i]] = i;
}
int f()
{ //估价函数。 //估值放缩方式:
bool vv[MaxM]; //如果将能够覆盖当 前列的所有行全部选中,去掉这些行能够覆盖到的列,
//将这个操作作为一层深度。重复此操作直到所有列全部出解的深度是多少。
int ret=0,c,i,j;
for(c=R[0];c!=0;c=R[c])
vv[c]=1;
for(c=R[0];c!=0;c=R[c])
{
if(vv[c])
{
++ret;
vv[c]=0;
for(i=D[c];i!=c;i=D[i])
for(j=R[i];j!=i;j=R[j])
vv[Col[j]]=0;
}
}
return ret;
}
void repeat_dance(int d)
{
if(d+f()>=ansd)
return; //估价函数剪枝,IDA*搜索
if(R[0]==0) //IDA*搜索值一开始是取无穷大的
{
if(d<ansd)
ansd=d; //然后每次取小。
return;
}
int c = R[0], i, j;
for(i = R[0]; i; i=R[i])
if(S[i]<S[c])
c=i;
for(i=D[c];i!=c;i=D[i])
{
repeat_remove(i);
for(j=R[i];j!=i;j = R[j])
repeat_remove(j);
repeat_dance(d + 1);
for(j=L[i]; j != i; j = L[j])
repeat_resume(j);
repeat_resume(i);
}
}
//d为递归深度
bool exact_Dance(int d)
{
if(R[0]==0)
{
ansd=d;
return true;
}
int c = R[0];
for(int i = R[0];i != 0;i = R[i])
if(S[i] < S[c])
c = i;
exact_remove(c);
for(int i = D[c];i != c;i = D[i])
{
ans[d] = Row[i]; //记录每次拿掉的列吧
for(int j = R[i]; j != i;j = R[j])
exact_remove(Col[j]);
if(exact_Dance(d+1))return true;
for(int j = L[i]; j != i;j = L[j])
exact_resume(Col[j]);
}
exact_resume(c);
return false;
}
};
DLX dlx;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
dlx.init(n,n);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
dlx.Link(a,b);
dlx.Link(b,a);
}
for(int i=1;i<=n;i++)dlx.Link(i,i);
dlx.ansd=0x3f3f3f;
dlx.repeat_dance(0);
printf("%d\n",dlx.ansd);
}
return 0;
}