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

    The Accomodation of Students
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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认识,并不代表A和C认识,现在提供互相认识的学生编号,问是否能将这些学生分成两组,同一组的学生互相不认识,如果可以,则分配给这些学生双人间,只有是朋友才能住双人间,问最多要准备几间房(即问是否为二分图,若是,其最大匹配数是多少
    http://www.cnblogs.com/syhandll/p/4721270.html
    分析:
    首先进行染色,判断是否是二分图,如果是二分图的话求出最大匹配,如果不是的话,输出No
     
    判断二分图的常见方法是染色法: 开始对任意一未染色的顶点染色,之后判断其相邻的顶点中,若未染色则将其染上和相邻顶点不同的颜色, 若已经染色且颜色和相邻顶点的颜色相同则说明不是二分图。
    http://blog.csdn.net/w144215160044/article/details/47404665
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 
     8 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
     9 //int main(int argc, char** argv) 
    10 
    11 #define N 1005
    12 
    13 int n, m, flag, ans, x, y;
    14 int maps[N][N], vis[N], used[N], c[N];
    15 
    16 int found(int u)
    17 {
    18     for(int i = 1; i <= n; i++)
    19     {
    20         if(! vis[i] && maps[u][i])
    21         {
    22             vis[i] = 1;
    23             if(!used[i] || found(used[i]))
    24             {
    25                 used[i] = u;
    26                 return true;
    27             }
    28         }
    29     }
    30     return false;
    31 }
    32 
    33 void dfs(int u, int k)  //所谓染色
    34 {
    35     c[u] = k;
    36     for(int i = 1; i <= n; i++)
    37     {
    38         if(maps[u][i] && !c[i])

    41 dfs(i, -k); 42 } 43 } 44 45 46 int main() 47 { 48 while(scanf("%d%d", &n, &m) != EOF) 49 { 50 memset(used, 0, sizeof(used)); 51 memset(maps, 0, sizeof(maps)); 52 memset(c, 0, sizeof(c)); 53 54 ans = flag = 0; 55 56 while(m--) 57 { 58 scanf("%d%d", &x, &y); 59 maps[x][y] = 1; 60 vis[x] = vis[y] = true; 61 } 62 63 for(int i = 1; i <= n; i++) 64 { 65 if(!vis[i]) 66 continue; 67 else if(!c[i]) 68 dfs(i, 1); 69 } 70 for(int i = 1; i <= n; i++) 71 { 72 for(int j = 1; j <= n; j++) 73 { 74 if(maps[i][j] && c[i]+c[j]) //一个集合里有认识的人了,flag = 1 75 flag = 1; 76 } 77 } 78 if(flag) 79 printf("No "); 80 else 81 { 82 for(int i = 1; i <= n; i++) 83 { 84 memset(vis, 0, sizeof(vis)); 85 if(found(i) && c[i]) 86 ans++; 87 } 88 printf("%d ", ans); 89 } 90 } 91 return 0; 92 }
     
    让未来到来 让过去过去
  • 相关阅读:
    选择排序
    迭代器使用过程中为什么抛出ConcurrentModificationException
    自定义实现的ArrayList以及自定义实现的Iterator迭代器
    单链表
    collections方法记录
    JDK1.9中关于集合的新方法
    请使用时间相关的API,计算一个人从出生到现在一共活了多少天?
    java中对象的向上转型和向下转型
    异常相关知识整理
    可变参数相关知识
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4722232.html
Copyright © 2011-2022 走看看