HDU 2444 The Accomodation of Students (判断是否是二分图,然后求最大匹配)
2016-07-02 17:20
447 查看
The Accomodation of Students
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)[align=left]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
题意:有n个学生,m对熟人,问你能否把他们分为2部分,其中在同一部分的人不认识,如果能,再将他们分到寝室,只有相互认识的人才能分到同一个寝室,问最多需要多少个寝室
分析:先判断是否为二分图,不是,就输出No,否则就求最大匹配。保存图用了两种方式。
/* **************************************************** #include<stdio.h> #include<string.h> #include<iostream> using namespace std; const int MAXN = 210; struct Edge{ int to,next; }edge[MAXN*MAXN]; int head[MAXN],tol; int uN,vN; int linker[MAXN]; bool used[MAXN]; int col[MAXN]; void init() { tol=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v) { edge[tol].to=v; edge[tol].next=head[u]; head[u]=tol++; } bool dfs(int u) { for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(!used[v]){ used[v]=true; if(linker[v]==-1||dfs(linker[v])){ linker[v]=u; return true; } } } return false; } int Hungary() { int res=0; int u; memset(linker,-1,sizeof(linker)); for(u=1;u<=uN;u++){ memset(used,false,sizeof(used)); if(dfs(u)) res++; } return res; } bool Color(int u) { if(col[u]==-1) col[u]=0; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(col[v]==-1){ col[v]=col[u]^1; if(!Color(v)) return false; } if(col[v]==col[u]) return false; } return true; } int main() { int n,m; int u,v; while(scanf("%d%d",&n,&m)!=EOF){ init(); uN=vN=n; for(int i=1;i<=m;i++){ scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } bool flag=true; memset(col,-1,sizeof(col)); for(int i=1;i<=n;i++) if(col[i]==-1) flag&=Color(i); if(!flag){ printf("No\n"); continue; } else printf("%d\n",Hungary()/2); } return 0; } ************************************************ */ #include<stdio.h> #include<string.h> #include<iostream> #include<vector> using namespace std; const int MAXN = 210; vector<int> G[MAXN]; int uN; int linker[MAXN]; bool used[MAXN]; int col[MAXN]; bool 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 Hungary() { int res=0; int u; memset(linker,-1,sizeof(linker)); for(u=1;u<=uN;u++){ memset(used,false,sizeof(used)); if(dfs(u)) res++; } return res; } bool Color(int u) { if(col[u]==-1) col[u]=0; for(int i=0;i<G[u].size();i++){ int v=G[u][i]; if(col[v]==-1){ col[v]=col[u]^1; if(!Color(v)) return false; } if(col[v]==col[u]) return false; } return true; } int main() { int n,m; int u,v; while(scanf("%d%d",&n,&m)!=EOF){ for(int i=1;i<=n;i++) G[i].clear(); uN=n; for(int i=1;i<=m;i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } bool flag=true; memset(col,-1,sizeof(col)); for(int i=1;i<=n;i++) if(col[i]==-1) flag&=Color(i); if(!flag){ printf("No\n"); continue; } else printf("%d\n",Hungary()/2); } return 0; }
相关文章推荐
- source insight使用
- glob和regex
- 解决As导入工程卡死的问题
- 使用Maven和Jersey Framework开发REST风格Web Service
- 【笔记】PMBOK第二章组织影响和项目生命周期
- 静态成员函数
- 安卓开发:服务的更多技巧--2.使用IntentService
- Ruby实现二分搜索(二分查找)算法的简单示例
- WebView自定义浏览器之在点击链接后不使用其他浏览器打开
- Android进阶(二十四)Android UI---界面开发推荐颜色
- Android进阶(二十四)Android UI---界面开发推荐颜色
- Codeforces Round #241 (Div. 2) B. Art Union 基础dp
- Sping与hibernate和Struts的整合及使用
- [Hibernate 2]session的三种状态
- 《深入理解Nginx》阅读与实践 使用upstream和subrequest访问第三方服务 no port in upstream
- [Hibernate 2]session的三种状态
- Tair源码阅读1---ConfigServer
- objective-C程序设计学习笔记3--分类和协议
- springMVC的拦截器工作流程
- 【笔记】PMBOK第一章绪论