链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2444
题目:
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
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
题意:
给你n条边m个点,让你先判断是不是二分图,如果不是输出No
,如果是就求这个图的最大匹配。
思路:
模板题。
实现:
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
#include <functional>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#define read read()
#define edl putchar('\n')
#define clr(a,b) memset(a,b,sizeof a)
inline int read{ int x=0;char c=getchar();while(c<'0' || c>'9')c=getchar();while(c>='0' && c<='9'){ x=x*10+c-'0';c=getchar(); }return x;}
inline void write(int x){ int y=10,len=1;while(y<=x) {y*=10;len++;}while(len--){y/=10;putchar(x/y+48);x%=y;}}
using namespace std;
const int maxn = 207;
int n,m;
vector<int> edge[maxn];
void addedge (int u, int v) {
edge[u].push_back(v);
edge[v].push_back(u);
}
bool col[maxn];
bool color (int u) {
for (auto v : edge[u]) {
if (!col[v]) {
col[v] = !col[u];
if (!color(v)) return false;
} else if (col[v] == col[u]) return false;
}
return true;
}
bool vis[maxn];
int link[maxn];
bool dfs(int u) {
for(auto v = edge[u].begin() ; v!=edge[u].end() ; v++) {
if(!vis[*v]) {
vis[*v] = true;
if(link[*v] == -1 || dfs(link[*v])) {
link[*v] = u;
link[u] = *v;
return true;
}
}
}
return false;
}
int match() {
int ans{};
clr(link, -1);
for (int i = 1; i <= n; i++)
if (link[i] == -1) {
clr(vis, 0);
if (dfs(i)) ans++;
}
return ans;
}
void init () {
clr(col,0);
for(int i=1 ; i<=n ; i++) edge[i].clear();
}
int main() {
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while(cin >> n >> m) {
if(n == 1) {
puts("No");
continue;
}
init();
for(int i=0, u, v ; i<m ; i++) {
u = read, v = read;
addedge(u,v);
}
col[1] = true;
if(!color(1)) {
puts("No");
continue;
}
write(match()),edl;
}
return 0;
}