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也认识a(无向图)而a认识b b认识c a不一定认识c(非传递)
然后问你是否可以把这些人分成两堆 这两堆里面的人互相谁也不认识 如果可以的话 让你把两个互相认识的人两两配对 找这个图的最大匹配
我用的点染法判断二分图(网上找了个错板子,然后用了自己之前做的cf上判断二分图的一个代码)
(判断二分图还可以用冰茶几...并查集
然后匈牙利来求最大匹配
//
// Created by xingchaoyue on 2019/4/12.
//
//
// Created by xingchaoyue on 2019/4/8.
//
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
using namespace std;
vector<int>v[200000+100];
int linker[200000+100];
int used[505];
int n,m;
int col[200000+100];
int flag = 1;
void init(){
for(int i =0;i<=200000;++i){
v[i].clear();
}
memset(linker,-1,sizeof(linker));
memset(used,0,sizeof(used));
memset(col,0,sizeof(col));
flag = 1;
}
int dfs(int u){
for(int i =0;i<v[u].size();++i){
int pos = v[u][i];
if(!used[pos]){
used[pos]=true;
if(linker[pos]==-1||dfs(linker[pos])){
linker[pos] = u;
return 1;
}
}
}
return 0;
}
int hungry(){
int res = 0;
memset(linker,-1,sizeof(linker));
for(int i = 1;i<=n;++i){
// cout<<":"<<i<<endl;
memset(used,0,sizeof(used));
if(dfs(i)){
res++;
}
}
return res;
}
void prin(int x,int color){
col[x] = color;
for(int i = 0;i<v[x].size();++i){
int pos = v[x][i];
if(!col[pos]){
prin(pos,-color);
}
else{
if(col[pos]==col[x])
flag = 1;
}
}
}
void prin1(int n,int c){
if(col[n]){
if(col[n]==c)
return;
else{
flag = 0;
return;
}
}
else{
col[n] = c;
}
for(int i =0;i<v[n].size();++i){
int nex = v[n][i];
if(c==1)
prin1(nex,2);
else
prin1(nex,1);
}
}
int edge[200000+100];
int main(){
while(~scanf("%d%d",&n,&m)){
init();
for(int i = 0;i<m;++i){
int a,b;
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
edge[i] = a;
}
for(int i =1;i<=n;++i){
if(col[i]==0){
prin1(i,1);
//cout<<":"<<i<<endl;
}
}
if(!flag)
cout<<"No"<<endl;
else{
cout<<hungry()/2<<endl;
}
}
}