B - The Accomodation of Students

B - The Accomodation of Students

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

InputcopyOutputcopy
 
4 4 1 2 1 3 1 4 2 3 6 5 1 2 1 3 1 4 2 5 3 6
 
No 3

Sponsor

先用并查集分组,再匈牙利算法求最大匹配

/*Where there is light, in my heart.*/
/*SUMMER_TRAINING DAY 26*/
#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
//
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//#define INF 0x3f3f3f
#define ll long long
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define unmap(a,b) unordered_map<a,b>
#define unset(a) unordered_set<a>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rep(i, a, b) for (int i = (a); i >= (b); --i)
#define mode 1e4+7
#define pi acos(-1)
#define U_queue priority_queue<PII,vector<PII>,greater<PII> >
typedef double db;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> vi;
const int N = 1005;
//
int line[N][N],girl[N],vis[N],pre[N],sum[N],f[N];
int n,m;
//初始化
void init(){
     mem(line,0);
     mem(girl,0);
     mem(f,0);
     mem(sum,0);
     for(int i=0;i<=n;i++) pre[i]=i;
}
//并查集分组
int find(int x){
     if(x!=pre[x]){
          int i=pre[x];
          pre[x]=find(pre[x]);
          sum[x]=(sum[x]+sum[i])%2;
     }
     return pre[x];
}
//匈牙利算法二分图最大匹配
bool found(int x){
     for(int i=1;i<=n;i++){
          if(line[x][i]&&!vis[i]){
               vis[i]=1;
               if(girl[i]==0||found(girl[i])){
                    girl[i]=x;
                    return true;
               }
          }
     }
     return false;
}
//主程序
signed main(){
     while(cin>>n>>m){
          int x,y;
          int nx=0;
          init();
          int flag=0;
          for(int i=1;i<=m;i++){
               cin>>x>>y;
               line[x][y]=line[y][x]=1;
               int fx=find(x),fy=find(y);
               if(fx==fy&&sum[x]==sum[y]) flag=1;
               else{
                    pre[fx]=fy;
                    sum[fx]=(sum[x]+sum[y]+1)%2;
               }
          }
          if(flag) cout<<"No"<<endl;
          else{
               for(int i=1;i<=n;i++){
                    if(sum[i]==0) f[nx++]=i;
               }
               int ans=0;
               for(int i=0;i<nx;i++){
                    mem(vis,0);
                    if(found(f[i])) ans++;
               }
               cout<<ans<<endl;
          }
     }
}
//made by shun 20220729

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值