您的位置:首页 > 理论基础 > 计算机网络

hdu 4272 2012长春赛区网络赛 dfs暴力 ***

2015-08-15 15:42 429 查看
总是T,以为要剪枝,后来发现加个map就行了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
#define MOD 1000000007
const int INF=0x3f3f3f3f;
const double eps=1e-5;
typedef long long ll;
#define cl(a) memset(a,0,sizeof(a))
#define ts printf("*****\n");
const int MAXN=1005;
int n,m,tt;
bool vis[MAXN];
int a[MAXN];
bool dfs(int pos)
{
while(vis[pos]==1&&pos>0)   pos--;
if(pos==0)  return 1;
if(pos==1)
{
return 0;
}
int temp=pos-1;
for(int i=0;i<=5;i++)
{
if(temp<=0) return 0;
if(vis[temp])
{
i--;
temp--;
continue;
}
if(a[pos]==a[temp]) //找到
{
vis[temp]=1;
if(dfs(pos-1))  return 1;
vis[temp]=0;
}
temp--;
}
return 0;
}
int main()
{
int i,j,k,ca=1;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
while(scanf("%d",&n)!=EOF)
{
map<int,int> mp;
for(i=1;i<=n;i++)
{
scanf("%d",a+i);
mp[a[i]]++;
}
if(n%2)
{
printf("0\n");
continue;
}
bool f=1;
map<int,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++)
{
if((it->second)%2==1)
{
f=0;
break;
}
}
if(!f)
{
printf("0\n");
continue;
}
cl(vis);
printf("%d\n",dfs(n));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: