zoukankan      html  css  js  c++  java
  • hdu 2444 The Accomodation of Students

    The Accomodation of Students

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2458    Accepted Submission(s): 1177


    Problem Description
    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.
     
    Input
    For 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.

    Output
    If 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
    先判断 A->B ,B->C ,C->A 这种情况在不在,如果不存在再用匈牙利求出最大匹配
     1 #include<string>
     2 #include<cstdio>
     3 #include<iostream>
     4 #include<vector>
     5 #include<queue>
     6 #include<stack>
     7 #include<algorithm>
     8 #include<cstring>
     9 #include<stdlib.h>
    10 #include<string>
    11 #include<cmath>
    12 using namespace std;
    13 #define pb push_back
    14 #define ll __int64
    15 int fa[210],vit[210],p[210][210],x[210],y[210],cnt1,cnt2,cx[210];
    16 vector<int >ko[210];
    17 int n,m;
    18 int Find(int pos){
    19     for(int i=0;i<ko[pos].size();i++){
    20         int to=ko[pos][i];
    21         if(!vit[to]){
    22             vit[to]=1;
    23             if(!cx[to]||Find(cx[to])){
    24                 cx[to]=pos;
    25                 return 1;
    26             }
    27         }
    28     }
    29     return 0;
    30 }
    31 void solve(){
    32     int cnt=0;
    33     memset(cx,0,sizeof(cx));
    34     for(int i=1;i<=cnt1;i++){
    35         memset(vit,0,sizeof(vit));
    36         if(Find(x[i])) cnt++;
    37     }
    38     cout<<cnt<<endl;
    39 }
    40 int main(){
    41     while(cin>>n>>m){
    42         for(int i=1;i<=n;i++) ko[i].clear();
    43         memset(p,0,sizeof(p));
    44         memset(vit,0,sizeof(vit));
    45         for(int i=1;i<=m;i++){
    46             int a,b;scanf("%d%d",&a,&b);
    47             p[a][b]=1,p[b][a]=1;
    48             ko[a].pb(b);
    49             ko[b].pb(a);
    50         }
    51         for(int i=1;i<=n;i++)
    52         if(!vit[i]){
    53             vit[i]=1;
    54             fa[i]=0;
    55             queue<int >kk;
    56             kk.push(i);
    57             while(!kk.empty()){
    58                 int pos=kk.front();
    59                 kk.pop();
    60                 for(int j=0;j<ko[pos].size();j++){
    61                     int to=ko[pos][j];
    62                     if(!vit[to]){
    63                         fa[to]=!fa[pos];   //把相互认识的人放在不同的集合里里面
    64                         vit[to]=1;
    65                         kk.push(to);
    66                     }
    67                 }
    68             }
    69         }
    70         cnt1=cnt2=0;
    71         for(int i=1;i<=n;i++)
    72             if(!fa[i]) x[++cnt1]=i;
    73         for(int i=1;i<=n;i++)
    74             if(fa[i]) y[++cnt2]=i;
    75         int can=1;
    76         for(int i=1;i<=cnt1;i++)
    77             for(int j=1;j<=cnt1;j++)
    78                 if(p[x[i]][x[j]]) can=0;
    79           for(int i=1;i<=cnt2;i++)
    80             for(int j=1;j<=cnt2;j++)
    81                 if(p[y[i]][y[j]]) can=0;
    82         if(!can){
    83             cout<<"No"<<endl;
    84             continue;
    85         }
    86         else  solve();//不存在那种情况,进行匹配
    87     }
    88 }
  • 相关阅读:
    php 修改、增加xml结点属性的实现代码
    mysql rand随机查询记录效率
    分享:mysql 随机查询数据
    分享:perl 文件操作总结
    分享:Perl打开与读取文件的方法
    js日期相关函数总结分享
    php后台如何避免用户直接进入方法实例
    python 函数的进阶
    python 初识函数
    python 冒泡排序
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/3901779.html
Copyright © 2011-2022 走看看