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 }
  • 相关阅读:
    三大流程控制:1、if判断语句 2、while循环语句 3、for循环语句
    变量剩余的部分,然后是基本数据类型、输入输出和基本运算符
    1.操作系统、2.编程语言分类、3.变量、4.运行python文件的三个阶段
    数据库4 待修
    电脑组合键
    redis 基础应用
    数据库3 待改
    数据库2 待修
    mysql 数据库基础篇
    socketserver 和 事件Event
  • 原文地址:https://www.cnblogs.com/ainixu1314/p/3901779.html
Copyright © 2011-2022 走看看