Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

dfs+状态压缩水过了 对当前点u找合法的连边方案 全部点都找到合法方案就返回

Posted by zengshiyuan at 2015-07-26 12:56:20 on Problem 1659
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
int f[11],g[11][11],n;
vector<int>s[11];
int count_bit(int x)
{
    int ret=0;
    while(x)
    {
        ret+=(x&1);
        x>>=1;
    }
    return ret;
}
void init()
{
    s[0].push_back(0);
    for(int i=1;i<=10;i++)
    {
        s[i].clear();
        for(int j=1;j<1024;j++)
            if(count_bit(j)==i)
                s[i].push_back(j);
    }
}
bool dfs(int u)
{
    if(u>n)
    {
        return true;
    }
    int v[11],tot=f[u];
    for(int i=0;i<s[tot].size() && s[tot][i]<(1<<n);i++)
    {
        int state=s[tot][i];
        if(state&(1<<(u-1))) continue;
        memset(v,0,sizeof(v));
        for(int j=1;j<=n && state;j++,state>>=1)
            if(state&1)
                v[j]=1;
        int ok=1;
        for(int j=1;j<u;j++)
            if(g[u][j]!=v[j])
        {
            ok=0;
            break;
        }
        if(ok)
        {
            for(int j=u+1;j<=n;j++)
                if(v[j])
                    g[u][j]=g[j][u]=1;
            if(dfs(u+1))
                return true;
            for(int j=u+1;j<=n;j++)
                if(v[j])
                    g[u][j]=g[j][u]=0;

        }
    }
    return false;
}
int main()
{
    init();
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
            scanf("%d",&f[i]);
        memset(g,0,sizeof(g));
        if(dfs(1))
        {
            puts("YES");
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<n;j++)
                    cout<<g[i][j]<<' ';
                cout<<g[i][n]<<endl;
            }
        }
        else puts("NO");
        puts("");
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator