HDU 2444 The Accomodation of Students(判断二分图+最大匹配)
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
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.
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 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
/* *********************************************** 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; }
原文地址:http://www.cnblogs.com/pk28/
与有肝胆人共事,从无字句处读书。
欢迎关注公众号:
欢迎关注公众号:
![](https://images.cnblogs.com/cnblogs_com/pk28/1069498/o_qrcode_for_gh_db07a03aaa56_344.jpg)