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.
means A and B are neighbor. Each unit has no more than 4 neighbor units. The input is terminated by EOF.
5 4 1 2 1 3 2 4 4 5 6 4 1 2 1 3 1 4 4 5
2 3
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define N 550
#define V 360000
int n,m;//n行 m列
int L[V],R[V];
int D[V],U[V];
int C[V];
int S[N],H[N];
int ak,size;//ak 最少多少行可以覆盖所有列(可重复)
void Link(int r,int c)
{
S[c]++;C[size]=c;
U[size]=U[c];D[U[c]]=size;
D[size]=c;U[c]=size;
if(H[r]==-1) H[r]=L[size]=R[size]=size;
else
{
L[size]=L[H[r]];R[L[H[r]]]=size;
R[size]=H[r];L[H[r]]=size;
}
size++;
}
void remove(int c)
{
int i;
for(i=D[c];i!=c;i=D[i])
L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c)
{
int i;
for(i=U[c];i!=c;i=U[i])
L[R[i]]=R[L[i]]=i;
}
int h()
{
int i,j,k,count=0;
bool visit[N];
memset(visit,0,sizeof(visit));
for(i=R[0];i;i=R[i])
{
if(visit[i]) continue;
count++;
visit[i]=1;
for(j=D[i];j!=i;j=D[j])
{
for(k=R[j];k!=j;k=R[k])
visit[C[k]]=1;
}
}
return count;
}
void Dance(int k)
{
int i,j,c,Min,ans;
ans=h();
if(k+ans>=ak) return;
if(!R[0])
{
if(k<ak) ak=k;
return;
}
for(Min=N,i=R[0];i;i=R[i])
if(S[i]<Min) Min=S[i],c=i;
for(i=D[c];i!=c;i=D[i])
{
remove(i);
for(j=R[i];j!=i;j=R[j])
remove(j);
Dance(k+1);
for(j=L[i];j!=i;j=L[j])
resume(j);
resume(i);
}
return;
}
int mat[300][300];
int main()
{
int tn,tm;
while(scanf("%d%d",&tn,&tm)==2)
{
//每个人当作一列,每个人当作一行 每次覆盖自己和他邻居
n=m=tn;
memset(mat,0,sizeof(mat));
while(tm--)
{
int u,v;scanf("%d%d",&u,&v);
mat[u][v]=mat[v][u]=1;
}
for(int i=0;i<=m;i++)
{
S[i]=0;
U[i]=D[i]=i;
L[i+1]=i;R[i]=i+1;
}R[m]=0;
memset(H,-1,sizeof(H));
size=m+1;
for(int i=1;i<=n;i++)
{
mat[i][i]=1;//important
for(int j=1;j<=m;j++)
{
if(mat[i][j]) Link(i,j);
}
}
ak=N;
Dance(0);
printf("%d\n",ak);
}
return 0;
}