标签:
★★☆ 输入文件:transform.in
输出文件:transform.out
简单对比
时间限制:1 s 内存限制:128 MB
【问题描述】
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define MAXN 10010 4 int BF[MAXN],BF1[MAXN],d[MAXN],prey[MAXN],n,f[MAXN][2]; 5 //bool f[MAXN][MAXN]; 6 bitset<MAXN> vis; 7 int read() 8 { 9 int s=0,fh=1;char ch=getchar(); 10 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)fh=-1;ch=getchar();} 11 while(ch>=‘0‘&&ch<=‘9‘){s=s*10+(ch-‘0‘);ch=getchar();} 12 return s*fh; 13 } 14 int Xyl(int u) 15 { 16 int i,v; 17 for(i=0;i<=1;i++) 18 { 19 v=f[u][i]; 20 if(vis[v]==0) 21 { 22 vis[v]=1; 23 if(Xyl(BF[v])==1||BF[v]==0) 24 { 25 BF[v]=u; 26 BF1[u]=v; 27 return 1; 28 } 29 } 30 } 31 return 0; 32 } 33 int main() 34 { 35 freopen("transform.in","r",stdin); 36 freopen("transform.out","w",stdout); 37 int i,ans,dd,gs; 38 n=read(); 39 memset(f,false,sizeof(f)); 40 for(i=0;i<n;i++) 41 { 42 d[i]=read();dd=n-d[i]; 43 gs=-1; 44 if(i-d[i]>=0)f[i][++gs]=i-d[i]; 45 if(i+d[i]<=n-1)f[i][++gs]=i+d[i]; 46 if(i-dd>=0)f[i][++gs]=i-dd; 47 if(i+dd<=n-1)f[i][++gs]=i+dd; 48 if(f[i][0]>=f[i][1])swap(f[i][0],f[i][1]); 49 } 50 memset(BF,0,sizeof(BF));ans=0; 51 memset(prey,-1,sizeof(prey)); 52 for(i=n-1;i>=0;i--) 53 { 54 vis.reset(); 55 ans+=Xyl(i); 56 //prey[i]=BF1[i]; 57 } 58 if(ans!=n)printf("No Answer"); 59 else 60 { 61 //for(i=0;i<n;i++)BF1[BF[i]]=i; 62 for(i=0;i<n;i++)printf("%d ",BF1[i]); 63 } 64 fclose(stdin); 65 fclose(stdout); 66 return 0; 67 }
Bzoj 1562: [NOI2009]变换序列 匈牙利算法,二分图匹配
标签:
原文地址:http://www.cnblogs.com/Var123/p/5399165.html