zoukankan      html  css  js  c++  java
  • 【POJ】3740 Easy Finding

      1 #include<cstdio>
      2 #define INF 0x7FFFFFFF
      3 #define MAXN 90010
      4 int n, m, size;
      5 int L[MAXN], R[MAXN], U[MAXN], D[MAXN], H[MAXN];
      6 int C[MAXN], S[MAXN];
      7 void Init() {
      8     int i;
      9     for (i = 0; i <= m; i++) {
     10         S[i] = 0;
     11         L[i + 1] = i;
     12         R[i] = i + 1;
     13         U[i] = D[i] = i;
     14     }
     15     R[m] = 0;
     16     size = m + 1;
     17 }
     18 void Link(int r, int c) {
     19     U[size] = c;
     20     D[size] = D[c];
     21     U[D[c]] = size;
     22     D[c] = size;
     23     if (H[r] < 0)
     24         H[r] = L[size] = R[size] = size;
     25     else {
     26         L[size] = H[r];
     27         R[size] = R[H[r]];
     28         L[R[H[r]]] = size;
     29         R[H[r]] = size;
     30     }
     31     S[c]++;
     32     C[size++] = c;
     33 }
     34 void Remove(int c) {
     35     int i, j;
     36     R[L[c]] = R[c];
     37     L[R[c]] = L[c];
     38     for (i = D[c]; i != c; i = D[i]) {
     39         for (j = R[i]; j != i; j = R[j]) {
     40             U[D[j]] = U[j];
     41             D[U[j]] = D[j];
     42             S[C[j]]--;
     43         }
     44     }
     45 }
     46 void Resume(int c) {
     47     int i, j;
     48     R[L[c]] = c;
     49     L[R[c]] = c;
     50     for (i = D[c]; i != c; i = D[i]) {
     51         for (j = R[i]; j != i; j = R[j]) {
     52             U[D[j]] = j;
     53             D[U[j]] = j;
     54             S[C[j]]++;
     55         }
     56     }
     57 }
     58 bool Dance(int now) {
     59     if (R[0] == 0)
     60         return true;
     61     int i, j, temp, c;
     62     for (temp=INF,i = R[0]; i; i = R[i]) {
     63         if(S[i]<temp)
     64         {
     65             temp=S[i];
     66             c=i;
     67         }
     68     }
     69     Remove(c);
     70     for(i=D[c];i!=c;i=D[i])
     71     {
     72         for(j=R[i];j!=i;j=R[j])
     73             Remove(C[j]);
     74         if(Dance(now+1))
     75             return true;
     76         for(j=L[i];j!=i;j=L[j])
     77             Resume(C[j]);
     78     }
     79     Resume(c);
     80     return false;
     81 }
     82 int main() {
     83     int i, j, k;
     84     while (~scanf("%d%d", &n, &m)) {
     85         Init();
     86         for (i = 1; i <= n; i++) {
     87             H[i] = -1;
     88             for (j = 1; j <= m; j++) {
     89                 scanf("%d", &k);
     90                 if (k)
     91                     Link(i, j);
     92             }
     93         }
     94         if (Dance(0))
     95             puts("Yes, I found it");
     96         else
     97             puts("It is impossible");
     98     }
     99     return 0;
    100 }
  • 相关阅读:
    Programming Contest Ranking(题解)
    Alphabet Cookies
    hpu 1267 Cafeteria (01背包)
    Triangles 正多边形分割锐角三角形
    ACdream 1067:Triangles
    hdu 1253 胜利大逃亡 (代码详解)解题报告
    最短路
    POJ- 1511 Invitation Cards
    E
    HDU
  • 原文地址:https://www.cnblogs.com/DrunBee/p/2604267.html
Copyright © 2011-2022 走看看