zoukankan      html  css  js  c++  java
  • hdu2444二分图最大匹配+判断二分图

    There are a group of students. Some of them may know each other, while others don't. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. 

    Now you are given all pairs of students who know each other. Your task is to divide the students into two groups so that any two students in the same group don't know each other.If this goal can be achieved, then arrange them into double rooms. Remember, only paris appearing in the previous given set can live in the same room, which means only known students can live in the same room. 

    Calculate the maximum number of pairs that can be arranged into these double rooms. 

    InputFor each data set: 
    The first line gives two integers, n and m(1<n<=200), indicating there are n students and m pairs of students who know each other. The next m lines give such pairs. 

    Proceed to the end of file. 

    OutputIf these students cannot be divided into two groups, print "No". Otherwise, print the maximum number of pairs that can be arranged in those rooms. 
    Sample Input

    4 4
    1 2
    1 3
    1 4
    2 3
    6 5
    1 2
    1 3
    1 4
    2 5
    3 6

    Sample Output

    No
    3
    题意:给n个数配对,分成两队,相同对的人不能认识;
    题解:直接匈牙利算法,用二重循环判断是否有相同队又认识的人(居然没超时,我看网上都是bfs做的,感觉二重循环代码量少了很多)
    #include<map>
    #include<set>
    #include<list>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    
    using namespace std;
    
    const double eps=1e-8;
    const int N=205,maxn=305,inf=0x3f3f3f3f;
    
    int n,m,color[N];
    bool used[N],ok[N][N];
    
    bool match(int x)
    {
        for(int i=1;i<=n;i++)
        {
            if(!used[i]&&ok[x][i])
            {
                used[i]=1;
                if(color[i]==0||match(color[i]))
                {
                    color[i]=x;
                    return 1;
                }
            }
        }
        return 0;
    }
    int main()
    {
        while(cin>>n>>m){
            memset(ok,0,sizeof(ok));
            while(m--){
                int a,b;
                cin>>a>>b;
                ok[a][b]=ok[b][a]=1;
            }
            memset(color,0,sizeof(color));
            int num=0;
            for(int i=1;i<=n;i++)
            {
                memset(used,0,sizeof(used));
                if(match(i))num++;
            }
          //  for(int i=1;i<=n;i++)cout<<color[i]<<" ";
            bool flag=0;
            for(int i=1;i<=n;i++)
                for(int j=i+1;j<=n;j++)
                    if(ok[i][color[j]]&&ok[i][j])
                        flag=1;
            if(!flag)cout<<num/2<<endl;
            else cout<<"No"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    转 linux设备模型(4)
    SQL convert
    SQL 中的 case when
    自己写的文本文件加密器
    [A3] 2D Airfoil Aerodynamic Analysis With Fluent & Gambit
    [A2]更快的使用你的键盘:AutoHotkey
    [A4]更快的使用你的键盘:AutoHotkey(2)
    开篇HOG提取训练检测+样本制作
    Flash调用Lua脚本: 五
    Sql Server全局变量 【转载】
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6731093.html
Copyright © 2011-2022 走看看