HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
2015-10-06 21:10
639 查看
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3775 Accepted Submission(s): 1771
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
4 4
1 2
1 3
1 4
2 3
6 5
1 2
1 3
1 4
2 5
3 6
[align=left]Sample Output[/align]
No
3
/* *********************************************** Author :pk28 Created Time :2015/10/6 19:53:21 File Name :hdu2444.cpp ************************************************ */ #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <iomanip> #include <list> #include <deque> #include <stack> #define ull unsigned long long #define ll long long #define mod 90001 #define INF 0x3f3f3f3f #define maxn 40000+10 #define cle(a) memset(a,0,sizeof(a)) const ull inf = 1LL << 61; const double eps=1e-5; using namespace std; bool cmp(int a,int b){ return a>b; } int n; int m,l,tot; int pre[10000],match[300]; int vis[maxn]; struct node{ int v,next; }edge[maxn]; void init(){ l=0; memset(pre,-1,sizeof pre); memset(match,-1,sizeof match); } void add(int u,int v){ edge[l].v=v; edge[l].next=pre[u]; pre[u]=l++; } int dfs(int u){ for(int i=pre[u];i+1;i=edge[i].next){ int v=edge[i].v; if(!vis[v]){ vis[v]=1; if(match[v]==-1||dfs(match[v])){ match[v]=u; return 1; } } } return 0; } int hungary(){ tot=0; for(int i=1;i<=n;i++){ cle(vis); if(dfs(i))tot++; } return tot; } int color[maxn]; bool bs(int u){ for(int i=pre[u];i+1;i=edge[i].next){ int v=edge[i].v; if(color[v]==color[u])return false; if(!color[v]){ color[v]=3-color[u]; if(!bs(v))return false; } } return true; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif //freopen("out.txt","w",stdout); int a,b; while(cin>>n>>m){ init(); for(int i=1;i<=m;i++){ scanf("%d%d",&a,&b); add(a,b); add(b,a); } cle(color); color[1]=1; if(!bs(1))puts("No"); else printf("%d\n",hungary()/2); } return 0; }
相关文章推荐
- [Training Video - 4] [Groovy] Object equality and variable equality check
- 【Python】Learn Python the hard way, ex2 注释
- 深入理解Java:注解(Annotation)--注解处理器
- Ligthoj 1407 Explosion
- HDU 1029
- 0~10的随机整数运算
- SQL语句中where和having的区别
- 破解myeclipse10找不到plugins文件
- Notepad++配合QuickText打造快速高效的文本编辑器
- 【Python】Learn Python the hard way, ex1 简单print语句
- C#学习之多线程开发技术(三)
- UI 动画之CALayer+CAAnimation 之 CABasicAnimation
- UVA 1632题解
- 四则运算 不太会
- apache无法启动:php 因为计算机中丢失 MSVCR110.dll
- 每日三个笑话-20151006
- 【bzoj4152】[AMPPZ2014]The Captain
- 深入理解Java:注解(Annotation)基本概念
- IOS开发—系统定位
- bzoj2244[SDOI2011]拦截导弹