• hust 1208 Playing games


    题目描述

    In our childhood, we all like playing games with our friends. Now you are a teacher in a kindergarten and there are n children in your class. Before playing games, you must divide the children into some small groups and each group must contains more than one child, but a child only wants to stand besides his or her friends. k (k > 1) children can form a group only if there exists a circular permutation of k children in which each child only stand besides his or her friends. To make the problem simple, given you n children and the relationship between them, you must judge if every child can be divided into some group and no one will be left alone.

    输入

    In the first line of the input, there is an integer T (0 < T <= 20), which means T test cases. For each test case, the first line contains two integers n (0 < n <= 400) and m (0 < m < n*n/2), which are the number of the children and the number of the relationship. There are m lines followed, every line has two integers a and b (1 <= a, b <= n ), means child a and child b are friends..

    输出

    For each test case, if every child can be divided into some group and no child will be left alone, output "YES", or else output "NO".

    样例输入

    2
    4 2
    1 2
    3 4
    3 2
    1 2
    1 3
    

    样例输出

    YES
    NO

    这个题很难想到是一个最大匹配问题,当最大匹配为n的时候就没有人独立,想谈谈二分图最大匹配的两个算法,真的效率高了很多,匈牙利算法132ms,而Hopcroft_Carp算法20ms,差别真的很大
    #include<map>
    #include<set>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define  inf 0x0f0f0f0f
     
    using namespace std;
     
    struct Hopcroft_Carp
    {
        static const int maxn=3000+10;
        int Mx[maxn],My[maxn],Nx,Ny,dx[maxn],dy[maxn],dis;
        bool vis[maxn];
        vector<int>G[maxn];
     
        void init()
        {
            for (int i=0;i<=Nx;i++)
            G[i].clear();
        }
     
        bool BFS()
        {
            queue<int>Q;
            dis=inf;
            memset(dx,-1,sizeof(dx));
            memset(dy,-1,sizeof(dy));
            for (int i=1;i<=Nx;i++)
            if (Mx[i]==-1)
            {
                Q.push(i);
                dx[i]=0;
            }
            while(!Q.empty())
            {
                int u=Q.front(); Q.pop();
                if (dx[u]>dis) break;
                for (int i=0;i<G[u].size();i++)
                {
                    int v=G[u][i];
                    if (dy[v]==-1)
                    {
                        dy[v]=dx[u]+1;
                        if (My[v]==-1) dis=dy[v];
                        else
                        {
                            dx[My[v]]=dy[v]+1;
                            Q.push(My[v]);
                        }
                    }
                }
            }
            return dis!=inf;
        }
     
        bool DFS(int u)
        {
            for (int i=0;i<G[u].size();i++)
            {
                int v=G[u][i];
                if (!vis[v] && dy[v]==dx[u]+1)
                {
                    vis[v]=true;
                    if (My[v]!=-1 && dy[v]==dis) continue;
                    if (My[v]==-1 || DFS(My[v]))
                    {
                        My[v]=u;
                        Mx[u]=v;
                        return true;
                    }
                }
            }
            return false;
        }
     
        int MaxMatch()
        {
            int ans=0;
            memset(Mx,-1,sizeof(Mx));
            memset(My,-1,sizeof(My));
            while(BFS())
            {
                memset(vis,0,sizeof(vis));
                for (int i=1;i<=Nx;i++)
                if (Mx[i]==-1 && DFS(i)) ans++;
            }
            return ans;
        }
    };
     
    Hopcroft_Carp friends;
     
    int main()
    {
        //freopen("in.txt","r",stdin);
        int t,n,m,x,y;
        scanf("%d",&t);
        while(t--)
        {
             scanf("%d%d",&n,&m);
             friends.Nx=n;friends.Ny=n;
             friends.init();
             while(m--)
             {
                  scanf("%d%d",&x,&y);
                  friends.G[x].push_back(y);
                  friends.G[y].push_back(x);
             }
             int ans=friends.MaxMatch();
             if (ans==n) printf("YES
    ");
             else printf("NO
    ");
        }
        return 0;
    }
    #include<map>
    #include<set>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define  inf 0x0f0f0f0f
     
    using namespace std;
    int Map[500][500];
    int link[500],use[500],n,m;
    bool dfs(int cap)
    {
        for (int i=1;i<=m;i++)
        {
            if(Map[cap][i] && !use[i])
            {
                use[i]=1;
                int j=link[i];
                link[i]=cap;
                if(j==-1 || dfs(j)) return true;
                link[i]=j;
            }
        }
        return false;
    }
    int hugry()
    {
        int ans=0;
        memset(link,-1,sizeof(link));
        for (int i=1;i<=n;i++)
        {
            memset(use,0,sizeof(use));
            if(dfs(i)) ans++;
        }
        return ans;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int x,y,t,M;
        scanf("%d",&t);
        while(t--)
        {
             scanf("%d%d",&n,&M);
             m=n;
             memset(Map,0,sizeof(Map));
             while(M--)
             {
                  scanf("%d%d",&x,&y);
                  Map[x][y]=Map[y][x]=1;
             }
             int ans=hugry();
             if (ans==n) printf("YES
    ");
             else printf("NO
    ");
        }
        //fclose(stdin);
        return 0;
    }

    作者 chensunrise

  • 相关阅读:
    吴裕雄--天生自然 PYTHON数据分析:糖尿病视网膜病变数据分析(续三)
    吴裕雄--天生自然 PYTHON数据分析:糖尿病视网膜病变数据分析(续二)
    吴裕雄--天生自然 PYTHON数据分析:糖尿病视网膜病变数据分析(续一)
    吴裕雄--天生自然 PYTHON数据分析:糖尿病视网膜病变数据分析
    吴裕雄--天生自然 R数据分析:2014年美国人时间使用调查(ATUS)饮食与健康模块文件分析
    吴裕雄--天生自然 PYTHON数据分析:所有美国股票和etf的历史日价格和成交量分析
    吴裕雄--天生自然 中医研究学习:入门
    吴裕雄--天生自然 python数据分析:健康指标聚集分析(健康分析)
    回首2018 | 分析型数据库AnalyticDB: 不忘初心 砥砺前行
    如何玩转跨库Join?跨数据库实例查询应用实践
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3826711.html
Copyright © 2020-2023  润新知