标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2164 Accepted Submission(s): 626
Special Judge
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 const int maxn=5010; 6 const int maxnode=1000010; 7 int s[maxn],t[maxn],belong[maxn],ans[maxn]; 8 struct DLX{ 9 int L[maxnode],R[maxnode],U[maxnode],D[maxnode]; 10 int cnt,Row[maxnode],Col[maxnode],C[maxn],H[maxn]; 11 void Init(int n,int m){ 12 for(int i=0;i<=m;i++){ 13 L[i]=i-1;R[i]=i+1; 14 U[i]=D[i]=i;C[i]=0; 15 } 16 cnt=m;L[0]=m;R[m]=0; 17 for(int i=1;i<=n;i++)H[i]=0; 18 } 19 20 void Link(int r,int c){ 21 Row[++cnt]=r;C[Col[cnt]=c]+=1; 22 23 U[cnt]=c;D[cnt]=D[c];U[D[c]]=cnt;D[c]=cnt; 24 25 if(!H[r])H[r]=L[cnt]=R[cnt]=cnt; 26 else R[cnt]=R[H[r]],L[cnt]=H[r],L[R[cnt]]=cnt,R[L[cnt]]=cnt; 27 } 28 29 void Delete(int c){ 30 L[R[c]]=L[c];R[L[c]]=R[c]; 31 for(int i=D[c];i!=c;i=D[i]) 32 for(int j=R[i];j!=i;j=R[j]) 33 --C[Col[j]],U[D[j]]=U[j],D[U[j]]=D[j]; 34 } 35 36 void Resume(int c){ 37 L[R[c]]=c;R[L[c]]=c; 38 for(int i=U[c];i!=c;i=U[i]) 39 for(int j=L[i];j!=i;j=L[j]) 40 ++C[Col[j]],U[D[j]]=j,D[U[j]]=j; 41 } 42 43 bool Solve(){ 44 if(!R[0])return true; 45 int p=R[0]; 46 for(int i=R[p];i;i=R[i]) 47 if(C[p]>C[i]) 48 p=i; 49 50 Delete(p); 51 for(int i=D[p];i!=p;i=D[i]){ 52 if(ans[belong[Row[i]]])continue; 53 for(int j=R[i];j!=i;j=R[j]) 54 Delete(Col[j]); 55 56 ans[belong[Row[i]]]=Row[i]; 57 if(Solve()) 58 return true; 59 ans[belong[Row[i]]]=0; 60 for(int j=L[i];j!=i;j=L[j]) 61 Resume(Col[j]); 62 } 63 Resume(p); 64 return false; 65 } 66 }dlx; 67 68 int L[maxn],R[maxn]; 69 bool G[maxn][maxn]; 70 71 int main(){ 72 int a,b,N,M,D,tot; 73 while(scanf("%d%d%d",&N,&M,&D)!=EOF){ 74 memset(G,0,sizeof(G)); 75 while(M--){ 76 scanf("%d%d",&a,&b); 77 G[a][b]=true; 78 G[b][a]=true; 79 } 80 81 tot=0; 82 for(int i=1;i<=N;i++){ 83 scanf("%d%d",&s[i],&t[i]); 84 tot+=(t[i]-s[i]+1)*(t[i]-s[i]+2)/2; 85 G[i][i]=true; 86 } 87 88 dlx.Init(tot,N*D); 89 memset(ans,0,sizeof(ans)); 90 for(int x=1,p=0;x<=N;x++) 91 for(int l=s[x];l<=t[x];l++) 92 for(int r=l;r<=t[x];r++){ 93 ++p;L[p]=l;R[p]=r;belong[p]=x; 94 for(int j=l;j<=r;j++) 95 for(int y=1;y<=N;y++) 96 if(G[x][y])dlx.Link(p,N*(j-1)+y); 97 } 98 if(dlx.Solve()){ 99 for(int i=1;i<=N;i++) 100 printf("%d %d\n",L[ans[i]],R[ans[i]]); 101 } 102 else 103 printf("No solution\n"); 104 printf("\n"); 105 } 106 return 0; 107 }
搜索(DLX):HDU 3663 Power Stations
标签:
原文地址:http://www.cnblogs.com/TenderRun/p/5578574.html