• Luogu1041 NOIP2003传染病控制(搜索)


      暴搜加个最优性剪枝即可。一直觉得正式比赛出这种不能一眼看出来暴搜就行了的搜索题的出题人都是毒瘤。

    #include<iostream> 
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int read()
    {
        int x=0,f=1;char c=getchar();
        while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
        while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
        return x*f;
    }
    #define N 310
    int n,p[N],deep[N],a[N][N],ans=N,fa[N],t,m;
    bool flag[N];
    struct data{int to,nxt;
    }edge[N<<1];
    void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
    void dfs(int k)
    {
        a[deep[k]][++a[deep[k]][0]]=k;m=max(m,deep[k]);
        for (int i=p[k];i;i=edge[i].nxt)
        if (edge[i].to!=fa[k])
        {
            deep[edge[i].to]=deep[k]+1;
            fa[edge[i].to]=k;
            dfs(edge[i].to);
        }
    }
    void paint(int k,bool color)
    {
        flag[k]=color;
        for (int i=p[k];i;i=edge[i].nxt)
        if (edge[i].to!=fa[k]) paint(edge[i].to,color);
    }
    void cut(int k,int cnt)
    {
        int t=cnt;
        for (int i=1;i<=a[k][0];i++)
        if (!flag[a[k][i]]) cnt++;
        if (cnt>=ans) return;
        if (k==m) {ans=min(ans,cnt);return;}
        for (int i=1;i<=a[k][0];i++)
        if (!flag[a[k][i]])
            for (int j=p[a[k][i]];j;j=edge[j].nxt)
            if (edge[j].to!=fa[a[k][i]])
            {
                paint(edge[j].to,1);
                cut(k+1,cnt);
                paint(edge[j].to,0);
            }
        cut(k+1,cnt);
        cnt=t;
    }
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("1041.in","r",stdin);
        freopen("1041.out","w",stdout);
        const char LL[]="%I64d
    ";
    #else
        const char LL[]="%lld
    ";
    #endif
        n=read();read();
        for (int i=1;i<n;i++)
        {
            int x=read(),y=read();
            addedge(x,y),addedge(y,x);
        }
        dfs(1);
        cut(0,0);
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    如何选择硅谷的IT公司
    NSDateFormatter设定日期格式
    内存映射文件
    double 转 int 的精度损失问题
    #ifndef #define 的用法
    How to improve the performance of MB5B
    Vendor Evaluation Questionair弹出空白页
    接受报价后创建合同,却无法弹出相应的合同类型
    SRM税务代码的决定逻辑
    How to improve the performance of MB51
  • 原文地址:https://www.cnblogs.com/Gloid/p/9785991.html
Copyright © 2020-2023  润新知