【HDU】3663 Power Stations

  1 #include<cstdio>

  2 #include<cstring>

  3 #define MAXN 400000

  4 #define MAXM 70

  5 #define INF 0x7FFFFFFF

  6 bool G[MAXM][MAXM], vis[MAXM << 5];

  7 int L[MAXN], R[MAXN], U[MAXN], D[MAXN];

  8 int S[MAXN], C[MAXN], X[MAXN], H[MAXN];

  9 int n, m, d, size, row[MAXM][10][10];

 10 void Init(int x)

 11 {

 12     int i, j, k, t;

 13     for (i = t = 1; i <= n; i++)

 14     {

 15         H[t] = -1;

 16         row[i][0][0] = t++;

 17         for (j = 1; j <= d; j++)

 18         {

 19             for (k = j; k <= d; k++)

 20             {

 21                 H[t] = -1;

 22                 row[i][j][k] = t++;

 23             }

 24         }

 25     }

 26     memset(vis, false, sizeof(vis));

 27     memset(G, false, sizeof(G));

 28     for (i = 0; i <= x; i++)

 29     {

 30         R[i] = i + 1;

 31         L[i + 1] = i;

 32         U[i] = D[i] = i;

 33         S[i] = 0;

 34     }

 35     R[x] = 0;

 36     size = x + 1;

 37 }

 38 void Link(int r, int c)

 39 {

 40     U[size] = c;

 41     D[size] = D[c];

 42     U[D[c]] = size;

 43     D[c] = size;

 44     if (H[r] < 0)

 45         H[r] = L[size] = R[size] = size;

 46     else

 47     {

 48         L[size] = H[r];

 49         R[size] = R[H[r]];

 50         L[R[H[r]]] = size;

 51         R[H[r]] = size;

 52     }

 53     S[c]++;

 54     X[size] = r;

 55     C[size++] = c;

 56 }

 57 void Remove(int c)

 58 {

 59     int i, j;

 60     L[R[c]] = L[c];

 61     R[L[c]] = R[c];

 62     for (i = D[c]; i != c; i = D[i])

 63     {

 64         for (j = R[i]; j != i; j = R[j])

 65         {

 66             S[C[j]]--;

 67             U[D[j]] = U[j];

 68             D[U[j]] = D[j];

 69         }

 70     }

 71 }

 72 void Resume(int c)

 73 {

 74     int i, j;

 75     L[R[c]] = R[L[c]] = c;

 76     for (i = D[c]; i != c; i = D[i])

 77     {

 78         for (j = R[i]; j != i; j = R[j])

 79         {

 80             S[C[j]]++;

 81             U[D[j]] = D[U[j]] = j;

 82         }

 83     }

 84 }

 85 bool Dance()

 86 {

 87     if (R[0] == 0)

 88         return true;

 89     int i, j, temp, c;

 90     for (temp = INF,i = R[0]; i; i = R[i])

 91     {

 92         if (temp > S[i])

 93         {

 94             temp = S[i];

 95             c = i;

 96         }

 97     }

 98     Remove(c);

 99     for (i = D[c]; i != c; i = D[i])

100     {

101         vis[X[i]] = true;

102         for (j = R[i]; j != i; j = R[j])

103             Remove(C[j]);

104         if (Dance())

105             return true;

106         for (j = L[i]; j != i; j = L[j])

107             Resume(C[j]);

108         vis[X[i]] = false;

109     }

110     Resume(c);

111     return false;

112 }

113 int main()

114 {

115     int i, j, k, t, p, u, v;

116     while (~scanf("%d%d%d", &n, &m, &d))

117     {

118         Init(n * d + n);

119         while (m--)

120         {

121             scanf("%d%d", &u, &v);

122             G[u][v] = G[v][u] = true;

123         }

124         for (i = 1; i <= n; i++)

125         {

126             G[i][i] = true;

127             scanf("%d%d", &u, &v);

128             Link(row[i][0][0], n * d + i);

129             for (j = u; j <= v; j++)

130             {

131                 for (k = j; k <= v; k++)

132                 {

133                     for (t = 1; t <= n; t++)

134                     {

135                         if (G[i][t])

136                         {

137                             for (p = j; p <= k; p++)

138                                 Link(row[i][j][k], (t - 1) * d + p);

139                         }

140                     }

141                     Link(row[i][j][k], n * d + i);

142                 }

143             }

144         }

145         if (Dance())

146         {

147             for (i = 1; i <= n; i++)

148             {

149                 for (j = 1; j <= d; j++)

150                 {

151                     for (k = j; k <= d; k++)

152                     {

153                         if (vis[row[i][j][k]])

154                         {

155                             printf("%d %d\n", j, k);

156                             break;

157                         }

158                     }

159                     if (k <= d)

160                         break;

161                 }

162                 if (j > d)

163                     puts("0 0");

164             }

165         }

166         else

167             puts("No solution");

168         putchar('\n');

169     }

170     return 0;

171 }

你可能感兴趣的:(HDU)