• P1041 传染病控制


    https://www.luogu.org/problem/P1041

    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    #define sc(x) scanf("%lld",&x);
    const int maxn = 1000;
    int n,m,tot=1,ans=5000;
    int c[maxn];
    vector<int>son[maxn];
    int G[maxn][maxn];
    void dfs(int i,int fa)
    {
    
        for(int j=1; j<=n; j++)
        {
            int x=G[i][j];
            //cout<<i<<' '<<fa<<'
    ';
            if(x!=0)
            {
                x=j;
               // cout<<i<<" zenmehuishi "<<x<<'
    ';
                if(x!=fa)
                {
                    son[i].push_back(x);
                    dfs(x,i);
                }
            }
        }
    }
    void solve(int p)
    {
        bool f=0;
        if(ans<=tot) return;
        for(int i=1; i<=n; i++)
        {
            if(c[i]==p)
            {
                for(int j=0; j<son[i].size(); j++)
                {
                    f=1;
                    tot++;
                    c[son[i][j]]=p+1;
                }
            }
        }
        tot--;
        for(int i=1; i<=n; i++)
        {
            if(c[i]==p+1)
            {
                c[i]=0;///剪掉
                solve(p+1);
                c[i]=p+1;
            }
        }///儿子的儿子
        tot++;
        for(int i=1; i<=n; i++)
        {
            if(c[i]==p+1)
            {
                c[i]=0;
                tot--;
            }
        }
        if(!f)//标记是否有儿子
        {
            ans=min(ans,tot);
            return;
        }
    }
    signed main()
    {
        int x,y;
        sc(n);
        sc(m);
        c[1]=1;
        for(int i=1; i<=m; i++)
        {
            sc(x);
            sc(y);
            G[x][y]=1;
             G[y][x]=1;
        }
    
        dfs(1,-1);
        /* for(int i=1; i<=n; i++)
         {
             cout<<son[i].size()<<' ';
         }*/
        solve(1);
        cout<<ans<<'
    ';
        return 0;
    }
  • 相关阅读:
    第一章 数据库概论
    规范化
    qt mysql驱动问题解绝
    linux文件系统软链接硬链接
    linux命令总结
    shell应用技巧
    如何使滚动条保持在页面底部
    地图定位问题汇总
    webpack基本用法
    git异常操作解决办法合集
  • 原文地址:https://www.cnblogs.com/liulex/p/11417509.html
Copyright © 2020-2023  润新知