舞蹈鏈模板題彙總

舞蹈鏈有兩種模型,精確覆蓋,多重覆蓋。

精確覆蓋

HUST 1017

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <queue>
//#include <tr1/unordered_set>
#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf 1e9
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) \
    for(i=a;a<b?i<=b:i>=b;a<b?i++:i--)

const int maxn = 1000*100*2+30;

struct sad{
    int L[maxn],R[maxn],U[maxn],D[maxn];
    int Row[maxn],Col[maxn];
    int h[maxn],s[maxn];
    int si;
    vector<int>ans;
    void init(int m)
    {
        for(int i=0;i<=m;i++)
        {
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
            s[i] = 0;
        }
        L[0] = m;
        R[m] = 0;
        clr(h,-1);
        si = m;
        ans.clear();
    }
    void link(int row,int col)
    {
        si++;
        Col[si] = col; Row[si] = row;
        s[col]++;
        U[si] = U[col]; D[si] = col;
        D[U[col]] = si;
        U[col] = si;
        if(h[row] != -1)
        {
            R[si] = h[row];
            L[si] = L[h[row]];
            R[L[si]] = si;
            L[R[si]] = si;
        }
        else
            h[row] = L[si] = R[si] = si;
    }
    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]){
                U[D[j]] = j;
                D[U[j]] = j;
                s[Col[j]]++;
            }
        }
        R[L[c]] = c;
        L[R[c]] = c;
    }
    bool dfs(){
        if( R[0] == 0 )
            return true;
        int c = 0;
        for(int i=R[0];i!=0;i=R[i])
        {
            if(!c||s[c]>s[i])
                c=i;
        }
        Remove(c);
        for(int i=D[c];i!=c;i=D[i])
        {
            ans.push_back(Row[i]);
            for(int j=R[i];j!=i;j=R[j])
                Remove(Col[j]);
            if(dfs()) return true;
            for(int j=L[i];j!=i;j=L[j])
                Resume(Col[j]);
            ans.pop_back();
        }
        Resume(c);
        return false;
    }
}dlx;

int main()
{
//  freopen("input.txt","r",stdin);
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        dlx.init(m);
        int a,b;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a);
            while(a--)
            {
                scanf("%d",&b);
                dlx.link(i,b);
            }
        }
        if( dlx.dfs() )
        {
            vector<int> ans = dlx.ans;
            sort(ans.begin(),ans.end());
            printf("%d",ans.size());
            for(int i=0;i<ans.size();i++)
                printf(" %d",ans[i]);
            printf("\n");
        }
        else printf("NO\n");
    }
    return 0;
}

ZOJ 3209


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <queue>
//#include <tr1/unordered_set>
#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf 1e9
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) \
    for(i=a;a<b?i<=b:i>=b;a<b?i++:i--)

const int maxn = 30*30*500*2+30;

struct sad
{
    int L[maxn] , R[maxn] , U[maxn] , D[maxn];
    int Row[maxn] , Col[maxn];
    int h[maxn] , s[maxn];
    int si;
    int ans;
    void init(int m)
    {
        for(int i=0;i<=m;i++)
        {
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
            s[i] = 0;
        }
        L[0] = m;
        R[m] = 0;
        clr(h,-1);
        si=m;
        ans=inf;
    }
    void link(int row,int col)
    {
        si++;
        if( si >= maxn ) while(1);
        Col[si] = col;
        Row[si] = row;
        s[col]++;
        U[si] = U[col];
        D[si] = col;
        D[U[col]] = si;
        U[col] = si;
        if(h[row]!=-1)
        {
            R[si] = h[row];
            L[si] = L[h[row]];
            R[L[si]] = si;
            L[R[si]] = si;
        }else
            h[row] = L[si] = R[si] = si;
    }
    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]){
                U[D[j]] = j;
                D[U[j]] = j;
                s[Col[j]]++;
            }
        }
        R[L[c]] = c;
        L[R[c]] = c;
    }
    void dfs(int cnt)
    {
        if(cnt >= ans) return ;
        if( R[0] == 0 )
        {
            ans = min(ans,cnt);
            return ;
        }
        int c=R[0];
        for(int i=R[0];i!=0;i=R[i])
            if(s[c]>s[i])
                c=i;
        Remove(c);
        for(int i=D[c];i!=c;i=D[i]){
            for(int j=R[i];j!=i;j=R[j])
                Remove(Col[j]);
            dfs(cnt+1);
            for(int j=L[i];j!=i;j=L[j])
                Resume(Col[j]);
        }
        Resume(c);
    }
}dlx;

///#define FOR(i,c,R) for(int i=c;i!=c;i=R[i])

int id[100][100];

int main()
{
//  freopen("input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m,p;
        scanf("%d%d%d",&n,&m,&p);
        int si=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                id[i][j]=++si;
        dlx.init(si);
//      debug(si);
        int a,b,c,d;
        for(int x=1;x<=p;x++)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            for(int i=a;i<c;i++)
                for(int j=b;j<d;j++)
                    dlx.link(x,id[i][j]);
        }
        dlx.dfs(0);
        if( dlx.ans == inf )
            printf("-1\n");
        else printf("%d\n",dlx.ans);
    }
    return 0;
}

POJ 3074
數獨問題有一點需要注意,限制條件有四個,不是三個。分別是行,列,塊,還有就是每個點只能放一個顏色。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
//#include <tr1/unordered_set>
//#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf (1e9+7)
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define LL long long
#define uLL unsigned LL
#define ffor(i,A,s) for(int i=A[s];i!=s;i=A[i])

using namespace std;

const int maxn = 2e5;
/*
struct sad{
    int L[maxn],R[maxn],U[maxn],D[maxn];
    int Row[maxn],Col[maxn];
    int h[maxn],s[maxn];
    int si;
    vector<int> ans;
    void init(int m){
        for(int i=0;i<=m;i++){
            U[i]=D[i]=i;
            L[i]=i-1;
            R[i]=i+1;
            s[i]=0;
        }
        L[0]=m;
        R[m]=0;
        clr(h,-1);
        ans.clear();
        si=m;
    }
    void link(int row,int col){
        si++;
        Col[si] = col; Row[si] = row;
        s[col]++;
        U[si] = U[col]; D[si] = col;
        D[U[col]] = si;
        U[col] = si;
        if( h[row] != -1 )
        {
            R[si] = h[row];
            L[si] = L[h[row]];
            R[L[si]] = si;
            L[R[si]] = si;
        }else
            h[row] = L[si] = R[si] = si;
    }
    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]){
                U[D[j]] = j;
                D[U[j]] = j;
                s[Col[j]]++;
            }
        }
        R[L[c]] = L[R[c]] = c;
    }
    bool dfs()
    {
        if(R[0]==0) return true;
        int c=R[0];
        for(int i=R[0];i;i=R[i])
            if(s[c]>s[i])
                c=i;
        Remove(c);
        for(int i=D[c];i-c;i=D[i])
        {
            ans.push_back(Row[i]);
            for(int j=R[i];j-i;j=R[j])
                Remove(Col[j]);
            if(dfs()) return true;
            for(int j=L[i];j-i;j=L[j])
                Resume(Col[j]);
            ans.pop_back();
        }
        Resume(c);
        return false;
    }
    void del(int c)
    {
        int i = h[c];
        Remove(Col[i]);
        for(int j=R[i];j-i;j=R[j])
            Remove(Col[j]);
    }
}dlx;
*/
struct sad{
    int L[maxn],R[maxn],U[maxn],D[maxn];
    int Row[maxn],Col[maxn];
    int h[maxn],s[maxn];
    int si;
    vector<int>ans;
    void init(int m)
    {
        for(int i=0;i<=m;i++)
        {
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
            s[i] = 0;
        }
        L[0] = m;
        R[m] = 0;
        clr(h,-1);
        si = m;
        ans.clear();
    }
    void link(int row,int col)
    {
        si++;
        Col[si] = col; Row[si] = row;
        s[col]++;
        U[si] = U[col]; D[si] = col;
        D[U[col]] = si;
        U[col] = si;
        if(h[row] != -1)
        {
            R[si] = h[row];
            L[si] = L[h[row]];
            R[L[si]] = si;
            L[R[si]] = si;
        }
        else
            h[row] = L[si] = R[si] = si;
    }
    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]){
                U[D[j]] = j;
                D[U[j]] = j;
                s[Col[j]]++;
            }
        }
        R[L[c]] = c;
        L[R[c]] = c;
    }
    bool dfs(){
        if( R[0] == 0 )
            return true;
        int c = 0;
        for(int i=R[0];i!=0;i=R[i])
        {
            if(!c||s[c]>s[i])
                c=i;
        }
        Remove(c);
        for(int i=D[c];i!=c;i=D[i])
        {
            ans.push_back(Row[i]);
            for(int j=R[i];j!=i;j=R[j])
                Remove(Col[j]);
            if(dfs()) return true;
            for(int j=L[i];j!=i;j=L[j])
                Resume(Col[j]);
            ans.pop_back();
        }
        Resume(c);
        return false;
    }
    void del(int c)
    {
        int i = h[c];
        Remove(Col[i]);
        for(int j=R[i];j-i;j=R[j])
            Remove(Col[j]);
    }
}dlx;

int Hash(int c,int x,int y){
    return c*81 + x*9 + y;
}
void get(int v,int &c,int &x,int &y)
{
    c = v / 81;
    x = v / 9 % 9;
    y = v % 9;
}
int col[20][20];
char s[100];

int main(){
//    freopen("input.txt","r",stdin);
    while(~scanf("%s",s)){
        if( !strcmp(s,"end") ) break;
        for(int i=0;s[i];i++)
        {
            if(s[i]=='.')
                col[i/9][i%9] = -1;
            else
                col[i/9][i%9] = s[i]-'0';
        }
        dlx.init(36*9);
        for(int x=0;x<9;x++)
            for(int y=0;y<9;y++)
                for(int c=0;c<9;c++)
                {
                    int h = Hash(c,x,y);
                    dlx.link( h + 1, Hash(0,x,c) + 1 );
                    dlx.link( h + 1, Hash(1,y,c) + 1 );
                    dlx.link( h + 1, Hash(2,x/3+y/3*3,c) + 1 );
                    dlx.link( h +1 , Hash(3,x,y) +1 );
                }
        for(int x=0;x<9;x++)
            for(int y=0;y<9;y++) if(col[x][y]!=-1)
            {
                int c = col[x][y]-1;
                dlx.del( Hash(c,x,y) + 1 );
            }
        dlx.dfs();
        for(int i=0;i<dlx.ans.size();i++)
        {
            int x,y,c;
//            debug(dlx.ans[i]);
            get( dlx.ans[i]-1 , c , x , y );
            col[x][y] = c+1;
        }
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++)
                printf("%d",col[i][j]);
        printf("\n");
    }
    return 0;
}

ZOJ 3122
這個題樣例有問題。。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
//#include <tr1/unordered_set>
//#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf (1e9+7)
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define LL long long
#define uLL unsigned LL
#define ffor(i,A,s) for(int i=A[s];i!=s;i=A[i])

using namespace std;

const int maxn = 5e5;

struct sad{
    int L[maxn],R[maxn],U[maxn],D[maxn];
    int Row[maxn],Col[maxn];
    int h[maxn],s[maxn];
    int si;
    vector<int>ans;
    void init(int m){
        for(int i=0;i<=m;i++)
        {
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
            s[i] = 0;
        }
        L[0] = m;R[m] = 0;
        clr(h,-1);
        si=m;
        ans.clear();
    }
    void link(int row,int col)
    {
        si++;
        Col[si] = col;
        Row[si] = row;
        s[col]++;
        U[si] = U[col];
        D[si] = col;
        D[U[col]] = si;
        U[col] = si;
        if(h[row]!=-1){
            R[si] = h[row];
            L[si] = L[h[row]];
            R[L[si]] = si;
            L[R[si]] = si;
        }else
            h[row] = L[si] = R[si] = si;
    }
    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=D[c];i-c;i=D[i])
            for(int j=R[i];j-i;j=R[j]){
                U[D[j]] = j;
                D[U[j]] = j;
                s[Col[j]]++;
            }
        R[L[c]] = L[R[c]] = c;
    }
    bool dfs(){
        if(R[0]==0) return true;
        int c=R[0];
        for(int i=R[0];i;i=R[i])
            if( s[c] > s[i] )
                c=i;
        Remove(c);
        for(int i=D[c];i-c;i=D[i]){
            ans.push_back(Row[i]);
            for(int j=R[i];j-i;j=R[j])
                Remove(Col[j]);
            if(dfs()) return true;
            for(int j=L[i];j-i;j=L[j])
                Resume(Col[j]);
            ans.pop_back();
        }
        Resume(c);
        return false;
    }
    void del(int c)
    {
        int i = h[c];
        Remove(Col[i]);
        for(int j=R[i];j!=i;j=R[j])
            Remove(Col[j]);
    }
}dlx;

int Hash(int c,int x,int y){
    return c*16*16+x*16+y;
}
void get(int v,int &c,int &x,int &y){
    c = v/(16*16);
    x = v/16%16;
    y = v%16;
}

int col[20][20];

int main(){
//    freopen("input.txt","r",stdin);
    int fi=1;
    while( 1 )
    {

        char c;
        for(int i=0;i<16;i++)
        {
            for(int j=0;j<16;j++)
            {
                if( scanf(" %c",&c) == -1 )
                    exit(0);
                if( c == '-' ) col[i][j] = -1;
                else col[i][j] = c-'A';
//col[i][j]=-1;
            }
        }        if( fi ) fi=0;
        else puts("");
        dlx.init( 16 * 4 * 16 );
        for(int x=0;x<16;x++)
            for(int y=0;y<16;y++)
                for(int c=0;c<16;c++)
                {
                    int h = Hash(c,x,y);
                    dlx.link( h+1 , Hash(0,x,y)+1 );
                    dlx.link( h+1 , Hash(1,x,c)+1 );
                    dlx.link( h+1 , Hash(2,y,c)+1 );
                    dlx.link( h+1 , Hash(3,x/4+y/4*4,c)+1 );
                }
        for(int x=0;x<16;x++)
            for(int y=0;y<16;y++) if(col[x][y]!=-1)
            {
                int h = Hash(col[x][y],x,y);
                dlx.del(h+1);
            }
        dlx.dfs();
        for(int i=0;i<dlx.ans.size();i++){
            int c,x,y;
            get( dlx.ans[i]-1 , c , x , y );
            col[x][y] = c;
        }
        for(int i=0;i<16;i++){
            for(int j=0;j<16;j++)
                printf("%c",col[i][j]+'A');
            puts("");
        }
    }
    return 0;
}


HDU 4069

/**
    試問嶺南應不好,卻道,此心安處是吾鄉。
**/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
//#include <tr1/unordered_set>
//#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf (1e9+7)
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define LL long long
#define uLL unsigned LL

using namespace std;

const int maxn = 4000;

vector<int>Q;

struct sad{
    int L[maxn],R[maxn],U[maxn],D[maxn];
    int Row[maxn],Col[maxn];
    int h[maxn],s[maxn];
    int si,cnt;
    vector<int>ans;
    void init(int m)
    {
        for(int i=0;i<=m;i++){
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
            s[i] = 0;
        }
        L[0] = m;
        R[m] = 0;
        clr(h,-1);
        si = m;
        ans.clear();
        cnt = 0;
    }
    void link(int row,int col){
        si++;
        Col[si] = col;
        Row[si] = row;
        s[col]++;
        U[si] = U[col];
        D[si] = col;
        D[U[col]] = si;
        U[col] = si;
        if(h[row] != -1){
            R[si] = h[row];
            L[si] = L[h[row]];
            R[L[si]] = si;
            L[R[si]] = si;
        }else
            h[row] = L[si] = R[si] = si;
    }
    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]){
                U[D[j]] = j;
                D[U[j]] = j;
                s[Col[j]]++;
            }
        }
        R[L[c]] = L[R[c]] = c;
    }
    bool dfs(){
        if(R[0]==0)
        {
            cnt++;
            Q.clear();
            for(int i=0;i<ans.size();i++)
                Q.push_back( ans[i] );
            if( cnt == 2 ) return true;
            return false;
        }
        int c = R[0];
        for(int i=R[0];i;i=R[i])
            if( s[c] > s[i] )
                c=i;
        Remove(c);
        for(int i=D[c];i-c;i=D[i]){
            ans.push_back( Row[i] );
            for(int j=R[i];j-i;j=R[j])
                Remove(Col[j]);
            if( dfs() ) return true;
            for(int j=L[i];j-i;j=L[j])
                Resume(Col[j]);
            ans.pop_back();
        }
        Resume(c);
        return false;
    }
    void del(int c){
        int i = h[c];
        Remove(Col[i]);
        for(int j=R[i];j-i;j=R[j])
            Remove(Col[j]);
    }
}dlx;

int dx[4] = { -1 , 0 , 1 , 0 };
int dy[4] = { 0 , 1 , 0, -1 };
int val[4] = { 16 , 32, 64, 128 };

int Hash(int c,int x,int y){
    return c*81 +x*9 +y;
}
void get(int v,int &c,int &x,int &y){
    c = v/81;
    x = v/9%9;
    y = v%9;
}

int col[12][12];
int st[12*12],p[12*12];

int Find(int x){
    if(p[x]==x) return x;
    return p[x]=Find(p[x]);
}
void _unite(int x,int y){
    p[Find(x)] = Find(y);
}

int main(){
//    freopen("input.txt","r",stdin);
    int T,CASE=0;
    scanf("%d",&T);
    while(T--){
        for(int i=0;i<81;i++){
            st[i] = -1;
            p[i] = i;
        }
        int cnt = 0,v;
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++){
                scanf("%d",&v);
                for(int k=3;k>=0;k--)
                {
                    if(v>=val[k])
                        v -= val[k];
                    else
                        _unite( Hash(0,i,j) , Hash(0,i+dx[k],j+dy[k]) );
                }
                col[i][j] = v-1;
            }
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++)
            {
                if( st[ Find(Hash(0,i,j)) ] == -1 )
                    st[ Find(Hash(0,i,j)) ] = cnt++;
            }
        dlx.init( 4 * 9 * 9);
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++)
                for(int c=0;c<9;c++){
                    dlx.link( Hash(c,i,j)+1 , Hash(0,i,c)+1 );
                    dlx.link( Hash(c,i,j)+1 , Hash(1,j,c)+1 );
                    dlx.link( Hash(c,i,j)+1 , Hash(2,i,j)+1 );
                    dlx.link( Hash(c,i,j)+1 , Hash(3, st[Find(Hash(0,i,j))] ,c)+1 );
                }
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++) if(col[i][j]!=-1)
                dlx.del( Hash(col[i][j],i,j)+1 );
        dlx.dfs();

        printf("Case %d:\n",++CASE);
        if( dlx.cnt == 0 )
            printf("No solution\n");
        else if( dlx.cnt == 2 )
            printf("Multiple Solutions\n");
        else{
            for(int i=0;i<Q.size();i++)
            {
                int x,y,c;
                get( Q[i]-1 ,c,x,y);
                col[x][y] = c;
            }
            for(int i=0;i<9;i++)
            {
                for(int j=0;j<9;j++)
                    printf("%d",col[i][j]+1);
                puts("");
            }
        }
    }
    return 0;
}


重複覆蓋

HDU 2295

/**
    試問嶺南應不好,卻道,此心安處是吾鄉。
**/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <queue>
//#include <tr1/unordered_set>
#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf 1e9
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) \
    for(i=a;a<b?i<=b:i>=b;a<b?i++:i--)

const int maxn = 50*50*2+30;

struct sad
{
    int L[maxn],R[maxn],U[maxn],D[maxn];
    int Row[maxn],Col[maxn];
    int h[maxn] , s[maxn];
    int si,ans;
    void init(int m)
    {
        for(int i=0;i<=m;i++)
        {
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
            s[i] = 0;
        }
        L[0] = m;
        R[m] = 0;
        clr(h,-1);
        si=m;
        ans=inf;
    }
    void link(int row,int col){
        si++;
        Col[si] = col;
        Row[si] = row;
        s[col]++;
        U[si] = U[col];
        D[si] = col;
        D[U[col]] = si;
        U[col] = si;
        if(h[row]!=-1)
        {
            R[si] = h[row];
            L[si] = L[h[row]];
            R[L[si]] = si;
            L[R[si]] = si;
        }else
            h[row] = L[si] = R[si] = si;
    }
    void Remove(int c)
    {
        for(int i=D[c];i!=c;i=D[i]){
            L[R[i]] = L[i];
            R[L[i]] = R[i];
        }
    }
    void Resume(int c)
    {
        for(int i=U[c];i!=c;i=U[i]){
            L[R[i]] = R[L[i]] = i;
        }
    }
    int vis[maxn];
    int H()
    {
        int ret=0;
        for(int i=R[0];i!=0;i=R[i])
            vis[i] = 0;
        for(int i=R[0];i!=0;i=R[i]) if(!vis[i]){
            ret++;
            vis[i] = 1;
            for(int j=D[i];j!=i;j=D[j])
                for(int k=R[j];k!=j;k=R[k])
                    vis[Col[k]] = 1;
        }
        return ret;
    }
    void dfs(int cnt)
    {
        if(cnt+H()>=ans) return ;
        if(R[0]==0){
            ans = min(ans,cnt);
            return ;
        }
        int c=R[0];
        for(int i=R[0];i!=0;i=R[i])
            if(s[c]>s[i]) c=i;
        for(int i=D[c];i-c;i=D[i]){
            Remove(i);
            for(int j=R[i];j-i;j=R[j])
                Remove(j);
            dfs(cnt+1);
            for(int j=L[i];j!=i;j=L[j])
                Resume(j);
            Resume(i);
        }
        return ;
    }
}dlx;

int n,m,k;
double x[maxn] , y[maxn];
double X[maxn] , Y[maxn];

bool cal(double d)
{
    dlx.init(n);
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
    {
        if( (X[j]-x[i])*(X[j]-x[i])+(Y[j]-y[i])*(Y[j]-y[i]) < d*d )
            dlx.link(j+1,i+1);
    }
    dlx.ans = k+1;
    dlx.dfs(0);
    return dlx.ans <= k;
}

int main()
{
//  freopen("input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&x[i],&y[i]);
        for(int i=0;i<m;i++)
            scanf("%lf%lf",&X[i],&Y[i]);
        double l=0 , r = 1e7;
        while( r-l > 1e-9 )
        {
            double mid = (l+r)/2.0;
            if( cal(mid) )
                r = mid;
            else
                l = mid;
        }
        printf("%.6f\n",l);
    }
    return 0;
}

FZU 1686

/**
    試問嶺南應不好,卻道,此心安處是吾鄉。
**/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <queue>
//#include <tr1/unordered_set>
//#include <tr1/unordered_map>
//#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf 1e9
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) \
    for(i=a;a<b?i<=b:i>=b;a<b?i++:i--)

const int maxn = 15*15*15*15*2+100;

struct sad
{
    int L[maxn],R[maxn],U[maxn],D[maxn];
    int Row[maxn] , Col[maxn];
    int h[maxn] , s[maxn];
    int si,ans;
    void init(int m){
        for(int i=0;i<=m;i++){
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
            s[i] = 0;
        }
        L[0] = m;
        R[m] = 0;
        clr(h,-1);
        si=m;
        ans=inf;
    }
    void link(int row,int col){
        si++;
        Col[si] = col;
        Row[si] = row;
        s[col]++;
        U[si] = U[col];
        D[si] = col;
        D[U[col]] = si;
        U[col] = si;
        if(h[row]!=-1)
        {
            R[si] = h[row];
            L[si] = L[h[row]];
            R[L[si]] = si;
            L[R[si]] = si;
        }
        else
            h[row] = L[si] = R[si] = si;
    }
    void Remove(int c)
    {
        for(int i=D[c];i-c;i=D[i]){
            L[R[i]] = L[i];
            R[L[i]] = R[i];
        }
    }
    void Resume(int c)
    {
        for(int i=U[c];i!=c;i=U[i]){
            L[R[i]] = R[L[i]] = i;
        }
    }
    int vis[maxn];
    int H()
    {
        int ret=0;
        for(int i=R[0];i;i=R[i])
            vis[i] = 0;
        for(int i=R[0];i;i=R[i]) if(!vis[i]){
            ret++;
            vis[i] = 1;
            for(int j=D[i];j!=i;j=D[j])
                for(int k=R[j];k-j;k=R[k])
                    vis[Col[k]] = 1;
        }
        return ret;
    }
    void dfs(int cnt)
    {
        if(cnt+H()>=ans) return ;
        if(R[0]==0){
            ans = min(ans,cnt);
            return ;
        }
        int c=R[0];
        for(int i=R[0];i;i=R[i])
            if(s[c]>s[i]) c=i;
        for(int i=D[c];i-c;i=D[i]){
            Remove(i);
            for(int j=R[i];j-i;j=R[j])
                Remove(j);
            dfs(cnt+1);
            for(int j=L[i];j-i;j=L[j])
                Resume(j);
            Resume(i);
        }
    }
}dlx;

int mp[20][20];
int id[20][20];

int main()
{
//  freopen("input.txt","r",stdin);
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int si=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                scanf("%d",&mp[i][j]);
                if(mp[i][j])
                    id[i][j] = ++si;
            }
//      debug(si);
        dlx.init(si);
        int a,b;
        scanf("%d%d",&a,&b);
        int cnt = 0;
        int t = (n+a-1)/a * ( (m+b-1)/b );
        for(int i=0;i+a<=n;i++)
            for(int j=0;j+b<=m;j++)
            {
                cnt++;
                for(int di=0;di<a;di++)
                    for(int dj=0;dj<b;dj++) if(mp[i+di][j+dj])
                        dlx.link(cnt,id[i+di][j+dj]);
            }
        dlx.ans = t;
        dlx.dfs(0);
        printf("%d\n",dlx.ans);
    }
    return 0;
}


POJ 1084

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
//#include <tr1/unordered_set>
//#include <tr1/unordered_map>
#include <bitset>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf (1e9+7)
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define LL long long
#define uLL unsigned LL
#define ffor(i,A,s) for(int i=A[s];i!=s;i=A[i])

using namespace std;

const int maxn = 2e5;

struct sad
{
    int L[maxn] , R[maxn] , U[maxn] , D[maxn];
    int Row[maxn] , Col[maxn];
    int h[maxn] , s[maxn];
    int si;
    int ans;
    void init(int m)
    {
        for(int i=0;i<=m;i++)
        {
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
            s[i] = 0;
        }
        L[0] = m;
        R[m] = 0;
        clr(h,-1);
        si=m;
        ans=inf;
    }
    void link(int row,int col)
    {
        si++;
        Col[si] = col;
        Row[si] = row;
        s[col]++;
        U[si] = U[col];
        D[si] = col;
        D[U[col]] = si;
        U[col] = si;
        if(h[row]!=-1)
        {
            R[si] = h[row];
            L[si] = L[h[row]];
            R[L[si]] = si;
            L[R[si]] = si;
        }else
            h[row] = L[si] = R[si] = si;
    }
    void Remove(int c){
        ffor(i,D,c){
            L[R[i]] = L[i];
            R[L[i]] = R[i];
        }
    }
    void Resume(int c){
        ffor(i,U,c)
            L[R[i]] = R[L[i]] = i;
    }
    int vis[maxn];
    int H(){
        int ret=0;
        ffor(i,R,0)
            vis[i] = 0;
        ffor(i,R,0) if(!vis[i]){
            ret++;
            vis[i] = 1;
            ffor(j,D,i)
                ffor(k,R,j)
                    vis[Col[k]] = 1;
        }
        return ret;
    }
    void dfs(int cnt){
        if( cnt +H() >= ans ) return ;
        if(R[0]==0){
            ans = min(ans,cnt);
            return ;
        }
        int c=R[0];
        ffor(i,R,0)
            if(s[c]>s[i])
                c= i;
        ffor(i,D,c){
            Remove(i);
            ffor(j,R,i)
                Remove(j);
            dfs(cnt+1);
            ffor(j,L,i)
                Resume(j);
            Resume(i);
        }
    }
}dlx;

int id[10][10][3];
vector<int>a[1000];

int main(){
//    freopen("input.txt","r",stdin);
    int T,n;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        int si=0;
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<n;j++)
                id[i][j][0]=++si;
            if( i == n ) break;
            for(int j=0;j<=n;j++)
                id[i][j][1]=++si;
        }
        ++si;
        for(int i=0;i<=si;i++)
            a[i].clear();
        int sa=0;
        for(int x1=0;x1<n;x1++)
            for(int y1=0;y1<n;y1++)
            {
                for(int d=1;x1+d<=n&&y1+d<=n;d++)
                    {
                        int x2=x1+d,y2=y1+d;
                        ++sa;
//                        printf("x1 %d y1 %d x2 %d y2 %d\n",x1,y1,x2,y2);
                        for(int x=x1;x<x2;x++)
                        {
                            a[ id[x][y1][1] ].push_back(sa);
                            a[ id[x][y2][1] ].push_back(sa);
                        }
                        for(int y=y1;y<y2;y++)
                        {
                            a[ id[x1][y][0] ].push_back(sa);
                            a[ id[x2][y][0] ].push_back(sa);
                        }
                    }
            }
        ++sa;
        dlx.init(sa);
        for(int i=1;i<si;i++)
        {
            for(int j=0;j<a[i].size();j++)
                dlx.link(i,a[i][j]);
        }
        int k,x;
        scanf("%d",&k);
        for(int i=0;i<k;i++)
        {
            scanf("%d",&x);
            for(int j=0;j<a[x].size();j++)
                dlx.link(si,a[x][j]);
        }
        dlx.link(si,sa);
        dlx.dfs(0);
        printf("%d\n",dlx.ans-1);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章