hdu2444二分匹配
来源:互联网 发布:html中调用js函数 编辑:程序博客网 时间:2024/05/19 21:00
B - The Accomodation of Students
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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.
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.
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 41 21 31 42 36 51 21 31 42 53 6
Sample Output
No3
能否形成二分图,能得话,最匹配数
#include <iostream>#include <stdio.h>#include <vector>#include <string.h>#include <queue>using namespace std;int const maxn=1010;int linker[maxn],used[maxn];vector<int>G[maxn];int col[maxn];bool bfs(int s){ col[s]=1; queue<int>p; p.push(s); while(!p.empty()) { int from=p.front(); p.pop(); for(int i=0;i<G[from].size();i++) { if(col[G[from][i]]==-1) { p.push(G[from][i]); col[G[from][i]]=!col[from]; } if(col[G[from][i]]==col[from]) return false; } } return true;}int dfs(int u){ for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(!used[v]) { used[v]=true; if(linker[v]==-1||dfs(linker[v])) { linker[v]=u; return true; } } } return false;}int solve(int n){ int res=0; memset(linker,-1,sizeof(linker)); for(int i=1;i<=n;i++) { memset(used,false,sizeof(used)); if(dfs(i))res++; } return res/2;}int main(){ int n,m; int x,y; while(scanf("%d%d",&n,&m)!=-1) { for(int i=0;i<=n;i++) { G[i].clear(); } for(int i=0; i<m; i++) { scanf("%d%d",&x,&y); G[x].push_back(y); G[y].push_back(x); } int flag=1; memset(col,-1,sizeof(col)); for(int i=1; i<=n; i++) if(col[i]==-1&&!bfs(i)) { flag=0; break; } if(flag==0) { cout<<"No"<<endl; continue; } int ans=solve(n); cout<<ans<<endl; } return 0;}
0 0
- hdu2444(二分匹配)
- hdu2444二分匹配
- hdu2444二分图最大匹配
- hdu2444 二分图判定+匹配
- hdu2444二分图判断+最大匹配
- hdu2444 二分匹配 The Accomodation of Students
- hdu2444 判断二分图+最大匹配
- hdu2444二分图判定 + 最大匹配
- HDU2444 【二分图判定+最大匹配】
- 【hdu2444】判断二分图+求最大匹配
- hdu2444(二分图判定+二分图匹配)
- hdu2444(二分图判断+二分图最大匹配)
- hdu2444 二分图匹配(判断二分图)
- hdu2444 The Accomodation of Students(判断二分匹配+最大匹配)
- 【HDU2444】【匈牙利算法】【二分匹配】【求最大匹配】【染色】
- hdu2444 The Accomodation of Students (二分图判断+最大匹配)
- hdu2444 The Accomodation of Students(判联通+二分匹配)
- HDU2444 The Accomodation of Students(二分匹配 匈牙利算法)
- Hdu 1070 Milk【水】
- Cpp_静态成员
- printf打印格式
- R语言学习:数据结构7-数据框
- DOM文档加载
- hdu2444二分匹配
- swift之图片浏览器
- poj 1664 放苹果(递推)
- PV操作--题目解析 (赞)
- R语言学习:数据结构8-日期和时间
- 多态学习续 抽象类和接口
- Centos&安装的时候提示无可用的网络设备
- POJ - 2406 Power Strings
- 使用XML资源文件来自定义颜色并使用