Tri_integral Autumn Training 5 zoj 3294 Utawarerumono by Hyoga

题目是可以做的,感觉这个题目就是:脑筋急转弯+预处理+网络流。比赛的时候队友其实已经想的差不多了,就是一直觉得边太多没敢去尝试。实际上当满足所有条件之后边非常少不超过10000。跑网络流完全可以承受。

1.bitcount[i]>11是个很有力的剪枝。这么一搞符合条件的数i、j只有3000多个了。

2.对于题目中说的这个条件“For a square formed by n*n unit squares, you can fill each grid with white or black color. If two filling designs match or match after rotation, they are considered the same. Then the number of different filling designs should be greater than or equal to 2500002 “(脑筋急转弯)

可知n>=50002一定可以,n<50000一定不可以。n=50001不可判定,但题目要求n为偶数,所以也不可以。又由于n=i&j,于是题目中满足条件的i,j一定都>=50002。

#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=210000;
const int inf=1<<28;
int bitcount[maxn];
int a[maxn],map[maxn];
int u[maxn],v[maxn];

int solve(int x)
{
    int num=0;
    while(x!=0)
    {
        if((x&1)==1)num++;
        x=x>>1;
    }
    return num;
}
//dinic
struct Edge
{
    int from,to,cap,flow;
};
struct Dinic
{
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>g[maxn];
    int d[maxn],cur[maxn];
    bool vis[maxn];
    void init(int n)
    {
        m=0;this->n=n;
        edges.clear();
        for(int i=0;i<=n;i++)
        g[i].clear();
    }
    void addedge(int from,int to,int cap)
    {
        edges.push_back((Edge){from,to,cap,0});
        edges.push_back((Edge){to,from,0,0});
        m=edges.size();
        g[from].push_back(m-2);
        g[to].push_back(m-1);
    }
    bool bfs()
    {
        memset(vis,0,sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!q.empty())
        {
            int x=q.front();q.pop();
            for(int i=0;i<g[x].size();i++)
            {
                Edge & e=edges[g[x][i]];
                if(!vis[e.to]&& e.cap>e.flow)
                {
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }
    int dfs(int x,int a)
    {
        if(x==t || a==0)return a;
        int flow=0,f;
        for(int & i=cur[x];i<g[x].size();i++)
        {
            Edge & e=edges[g[x][i]];
            if(d[x]+1==d[e.to] && (f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[g[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0)break;
            }
        }
        return flow;
    }
    int maxflow(int s,int t)
    {
        this->s=s;this->t=t;
        int flow=0;
        while(bfs())
        {
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,inf);
        }
        return flow;
    }
}ans;
int main()
{
    //freopen("data","r",stdin);
    //pre
    for(int i=0;i<=100000;i++)
    {
        bitcount[i]=solve(i);
        map[i]=-1;
    }

    int tote=0;int tot=0;
    for(int i=50002;i<=100000;i++)
    if(bitcount[i]>11)
    {
        tot++;
        a[tot]=i;
        map[i]=tot;
    }

    for(int i=1;i<=tot;i++)
     for(int j=i+1;j<=tot;j++)
    if(bitcount[(a[i]&a[j])]>11 && ((a[i]&a[j])>50002) && (a[i]&a[j])%2==0)
    {
        tote++;
        u[tote]=i;v[tote]=j;
        //u tot v
    }

    int s,t;
    while(scanf("%d%d",&s,&t)!=EOF)
    {
        if(s==t)
        {
            printf("150\n");
            continue;
        }
        if(map[s]==-1 || map[t]==-1 || s>t)
        {
            printf("0\n");
            continue;
        }

        //
        s=map[s];
        t=map[t];
        ans.init(tot+1);
        //addedge
        ans.addedge(tot+1,s,150);
        s=tot+1;
        for(int i=1;i<=tote;i++)
        ans.addedge(u[i],v[i],1);

        int out=ans.maxflow(s,t);
        printf("%d\n",out);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值