hdu2444二分匹配
2016-02-29 21:29
411 查看
B - The Accomodation of Students
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
2444
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
能否形成二分图,能得话,最匹配数
#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;
}
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
2444
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
能否形成二分图,能得话,最匹配数
#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;
}
相关文章推荐
- 每日三只DP
- yii2表单学习
- 安卓异常java.lang.IllegalStateException: Content has been consumed
- poj 1664 放苹果(递推)
- 韩顺平XML学习3
- C++开发人脸性别识别教程(19)——界面美化
- 面向OPENCL的ALTERA SDK
- D4
- hrbust 哈理工oj 1729 Finding the Way【计算几何】
- 登录界面获取用户名密码,在第二个页面读出该用户名,然后显示。
- CF 116A Tram
- 括号匹配(栈和队列)
- throw与throws与自定义异常
- DOM文档加载
- [疯狂Java笔记]AWT容器简介
- printf打印格式
- Cpp_静态成员
- 安卓通知的使用系列2:状态栏通知和自定义状态栏通知通知
- R语言学习:数据结构7-数据框
- 我想当老师