hdu 4272 2012长春赛区网络赛 dfs暴力 ***
总是T,以为要剪枝,后来发现加个map就行了
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #define MOD 1000000007 10 const int INF=0x3f3f3f3f; 11 const double eps=1e-5; 12 typedef long long ll; 13 #define cl(a) memset(a,0,sizeof(a)) 14 #define ts printf("*****\n"); 15 const int MAXN=1005; 16 int n,m,tt; 17 bool vis[MAXN]; 18 int a[MAXN]; 19 bool dfs(int pos) 20 { 21 while(vis[pos]==1&&pos>0) pos--; 22 if(pos==0) return 1; 23 if(pos==1) 24 { 25 return 0; 26 } 27 int temp=pos-1; 28 for(int i=0;i<=5;i++) 29 { 30 if(temp<=0) return 0; 31 if(vis[temp]) 32 { 33 i--; 34 temp--; 35 continue; 36 } 37 if(a[pos]==a[temp]) //找到 38 { 39 vis[temp]=1; 40 if(dfs(pos-1)) return 1; 41 vis[temp]=0; 42 } 43 temp--; 44 } 45 return 0; 46 } 47 int main() 48 { 49 int i,j,k,ca=1; 50 #ifndef ONLINE_JUDGE 51 freopen("1.in","r",stdin); 52 #endif 53 while(scanf("%d",&n)!=EOF) 54 { 55 map<int,int> mp; 56 for(i=1;i<=n;i++) 57 { 58 scanf("%d",a+i); 59 mp[a[i]]++; 60 } 61 if(n%2) 62 { 63 printf("0\n"); 64 continue; 65 } 66 bool f=1; 67 map<int,int>::iterator it; 68 for(it=mp.begin();it!=mp.end();it++) 69 { 70 if((it->second)%2==1) 71 { 72 f=0; 73 break; 74 } 75 } 76 if(!f) 77 { 78 printf("0\n"); 79 continue; 80 } 81 cl(vis); 82 printf("%d\n",dfs(n)); 83 } 84 }