zoukankan      html  css  js  c++  java
  • 高维宇宙

     

    二分图匹配代码

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    const int MX=1001;
    struct Edge {
        int to,next;
    }edge[41*41];
    bool ispr[MX*2],vis[41];
    int match[41],ser[41],first[41*41],n,cnt;
    void add(int from,int to)
    {
        edge[++cnt].to=to;
        edge[cnt].next=first[from];
        first[from]=cnt;
    }
    
    bool hungry(int from)
    {
        for(int i=first[from];i;i=edge[i].next)
        {
            int to=edge[i].to;
            if(vis[to]) continue;
            vis[to]=1;
            if(!match[to] || hungry(match[to])) {
                match[to]=from;
                return 1;
            }
        }
        return 0;
    }
    
    int main()
    {
        memset(ispr,1,sizeof(ispr));
        for(int i=2;i<=2001;++i)
            if(ispr[i])
                for(int j=i;i*j<=2001;++j)
                    ispr[i*j]=0;
        
        scanf("%d",&n); 
        for(int i=1;i<=n;++i) {
            scanf("%d",&ser[i]);
        }
        sort(ser+1,ser+1+n); 
        for(int i=1;i<=n;++i) { //奇数 
            if(ser[i]%2)
                for(int j=1;j<=n;++j) //偶数 
                    if(!(ser[j]%2) && ispr[ser[i]+ser[j]])
                         add(i,j);    
        }
        int ans=0;
        for(int i=1;i<=n;++i)
        {
            memset(vis,0,sizeof(vis));
            if(hungry(i)) ans++; 
        }
        printf("%d",ans); 
        return 0;
    }
    /*
    5 
    2 9 11 12 37
    */
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    const int MX=1001;
    struct Edge {
        int to,next;
    }edge[41*41];
    bool ispr[MX*2],vis[41];
    int match[41],ser[41],first[41*41],n,cnt;
    void add(int from,int to)
    {
        edge[++cnt].to=to;
        edge[cnt].next=first[from];
        first[from]=cnt;
    }
    
    bool hungry(int from)
    {
        for(int i=first[from];i;i=edge[i].next)
        {
            int to=edge[i].to;
            if(vis[to]) continue;
            vis[to]=1;
            if(!match[to] || hungry(match[to])) {
                match[to]=from;
                return 1;
            }
        }
        return 0;
    }
    
    int main()
    {
        memset(ispr,true,sizeof(ispr));
        for(int i=2;i<=2001;++i)
            if(ispr[i])
                for(int j=i+i;j<=2001;j+=i)
                    ispr[j]=false;
        
        scanf("%d",&n);
        for(int i=1;i<=n;++i) scanf("%d",&ser[i]);
        sort(ser+1,ser+1+n); 
        
        for(int i=1;i<=n;i++) //
        { 
            for(int j=1;j<=n;j++)
            { 
                if(ispr[ser[i]+ser[j]] && i!=j &&ser[i]%2==1&&ser[j]%2==0) 
                {
                    add(i,j);
                }
            }
        }
        int ans=0;
        for(int i=1;i<=n;++i)
        {
            memset(vis,0,sizeof(vis));
            if(hungry(i)) ans++; 
        }
        printf("%d",ans);
        return 0;
    }
    /*
    5 
    2 9 11 12 37
    
    10
    3 11 6 8 17 12 7 4 6 9
    */
    从0到1很难,但从1到100很容易
  • 相关阅读:
    SQL Server 重新组织生成索引
    atitit.软件设计模式大的总结attialx总结
    Linux防火墙限制指定port仅仅能由指定IP訪问
    android TextView里边实现图文混配效果
    Codeforces Round #270
    HTML5中x-webkit-speech语音输入功能
    oracle11g中SQL优化(SQL TUNING)新特性之SQL Plan Management(SPM)
    Android-HttpURLConnection自己主动管理cookie
    iOS UI01_UIView
    Okio简化处理I/O操作原理
  • 原文地址:https://www.cnblogs.com/qseer/p/9531621.html
Copyright © 2011-2022 走看看