构图比较麻烦,我也说不清楚。
反正我做的不是水题,就是转化一下直接套模板!
这个题就是构图完之后直接DLX模板~
代码如下:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define inf 0x3f3f3f3f
const int MaxM = 66*6;
const int MaxN = 66*16;
const int maxnode = MaxM*MaxN;
bool mat[66][66];
struct point
{
int s,e;
};
struct put
{
int s,e,i;
};
put ans[maxnode];
point p[66];
int N,M,DD;
struct DLX
{
int n,m,size;
int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
int H[MaxN], S[MaxM];
int ansd, ans[MaxN];
void init(int _n,int _m)
{
n = _n;
m = _m;
for(int i = 0;i <= m;i++)
{
S[i] = 0;
U[i] = D[i] = i;
L[i] = i-1;
R[i] = i+1;
}
R[m] = 0; L[0] = m;
size = m;
for(int i = 1;i <= n;i++)
H[i] = -1;
}
void Link(int r,int c)
{
++S[Col[++size]=c];
Row[size] = r;
D[size] = D[c];
U[D[c]] = size;
U[size] = c;
D[c] = size;
if(H[r] < 0)H[r] = L[size] = R[size] = size;
else
{
R[size] = R[H[r]];
L[R[H[r]]] = size;
L[size] = H[r];
R[H[r]] = size;
}
}
void remove(int c)
{
L[R[c]] = L[c]; R[L[c]] = R[c];
for(int i = D[c];i != c;i = D[i])
for(int j = R[i];j != i;j = R[j])
{
U[D[j]] = U[j];
D[U[j]] = D[j];
--S[Col[j]];
}
}
void resume(int c)
{
for(int i = U[c];i != c;i = U[i])
for(int j = L[i];j != i;j = L[j])
++S[Col[U[D[j]]=D[U[j]]=j]];
L[R[c]] = R[L[c]] = c;
}
//d为递归深度
bool Dance(int d)
{
int temp;
temp=inf;
if(R[0] == 0)
{
ansd = d;
return true;
}
int c = R[0];
for(int i = R[0];i != 0;i = R[i])
if(S[i] < S[c])
c = i;
remove(c);
for(int i = D[c];i != c;i = D[i])
{
ans[d] = Row[i];
for(int j = R[i]; j != i;j = R[j])remove(Col[j]);
if(Dance(d+1))return true;
for(int j = L[i]; j != i;j = L[j])resume(Col[j]);
}
resume(c);
return false;
}
};
DLX g;
void build()
{
int i,j,k,r,cnt=1;
for(i=1;i<=N;i++){
for(j=p[i].s;j<=p[i].e;j++){
for(k=p[i].s;k<=j;k++){
g.Link(cnt,i);
ans[cnt].s=k,ans[cnt].e=j,ans[cnt].i=i;
for(r=1;r<=N;r++){
if(!mat[i][r]) continue;
for(int d=k;d<=j;d++){
g.Link(cnt,N+(d-1)*N+r);
}
}
cnt++;
}
}
g.Link(cnt,i);
ans[cnt].s=0,ans[cnt].e=0,ans[cnt].i=i;
cnt++;
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int a,b;
while(scanf("%d%d%d",&N,&M,&DD)!=EOF)
{
g.init(N*16,(DD+1)*N);
memset(mat,0,sizeof(mat));
for(int i = 1;i <= M;i++)
{
scanf("%d%d",&a,&b);
mat[a][b]=mat[b][a]=1;
}
for(int i=1;i<=N;i++)
{
mat[i][i]=1;
scanf("%d%d",&p[i].s,&p[i].e);
}
build();
if(!g.Dance(0)) printf("No solution\n");
else
{
//printf("%d",g.ansd);
memset(p,0,sizeof(p));
for(int i = 0;i < g.ansd;i++){
p[ans[g.ans[i]].i].s=ans[g.ans[i]].s;
p[ans[g.ans[i]].i].e=ans[g.ans[i]].e;
}
for(int i=1;i<=N;i++)
printf("%d %d\n",p[i].s,p[i].e);
//printf("\n");
}
printf("\n");
}
return 0;
}