2008年第33届ACM/ICPC亚洲区预赛(哈尔滨)网络预选赛 1007__The Accomodation of Students 二分图最大匹配+染色
2008-10-02 08:51
489 查看
http://acm.hrbeu.edu.cn/index.php?act=problem&proid=5087
之前完全不会的说....太弱小了
想了半天终于知道啥是染色....
不过现在会了,会了总比不会好-,-
题目的意思就是把学生分成2组,每组中的学生都不互相认识,可是2组间的任何学生都认识,很明显是匹配问题,比赛的时候瞬间被无数人秒杀..
具体思路代码写的非常清楚了...
#include <iostream>
using namespace std;
#define N 201
bool map
,visit
;
int n,m,match
,color
;
bool dfs(int pre)
{
for(int i=1;i<=m;i++)
{
if(map[pre][i]&&!visit[i])
{
visit[i]=true;
int t=match[i];
match[i]=pre;
if(t==-1||dfs(t)) return true;
match[i]=t;
}
}
return false;
}
int find_match()
{
memset(match,-1,sizeof(match));
int i,sum=0;
for(i=1;i<=n;i++){memset(visit,false,sizeof(visit));if(dfs(i)) sum++;}
return sum;
}
int main()
{
int x,y,t;
bool flag;
while(cin>>n>>t)
{
memset(map,false,sizeof(map));
memset(color,0,sizeof(color));
m=n;
flag=false;
while(t--)
{
cin>>x>>y;
if(color[x]&&color[x]==color[y])flag=true;
color[x]=1;
color[y]=2;
map[x][y]=true;
}
if(flag)
cout<<"No"<<endl;
else
cout<<find_match()<<endl;
}
}
之前完全不会的说....太弱小了
想了半天终于知道啥是染色....
不过现在会了,会了总比不会好-,-
题目的意思就是把学生分成2组,每组中的学生都不互相认识,可是2组间的任何学生都认识,很明显是匹配问题,比赛的时候瞬间被无数人秒杀..
具体思路代码写的非常清楚了...
#include <iostream>
using namespace std;
#define N 201
bool map
,visit
;
int n,m,match
,color
;
bool dfs(int pre)
{
for(int i=1;i<=m;i++)
{
if(map[pre][i]&&!visit[i])
{
visit[i]=true;
int t=match[i];
match[i]=pre;
if(t==-1||dfs(t)) return true;
match[i]=t;
}
}
return false;
}
int find_match()
{
memset(match,-1,sizeof(match));
int i,sum=0;
for(i=1;i<=n;i++){memset(visit,false,sizeof(visit));if(dfs(i)) sum++;}
return sum;
}
int main()
{
int x,y,t;
bool flag;
while(cin>>n>>t)
{
memset(map,false,sizeof(map));
memset(color,0,sizeof(color));
m=n;
flag=false;
while(t--)
{
cin>>x>>y;
if(color[x]&&color[x]==color[y])flag=true;
color[x]=1;
color[y]=2;
map[x][y]=true;
}
if(flag)
cout<<"No"<<endl;
else
cout<<find_match()<<endl;
}
}
相关文章推荐
- 2008年第33届ACM/ICPC亚洲区预赛(合肥)网络预选赛_1007 The Luckies number
- 参加第33届ACM/ICPC亚洲区预赛网络赛(哈尔滨)有感
- HDOJ 题目2444 The Accomodation of Students(染色法判二分图,最大匹配)
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 - J Our Journey of Dalian Ends(最小费用最大流)
- HUD 2444 The Accomodation of Students (二分图染色+最大匹配)
- HDU2444 The Accomodation of Students(染色法判断二分图+最大匹配)
- hdoj 2444 The Accomodation of Students 【黑白染色判二分图 + 最大匹配】
- hdu 2444 The Accomodation of Students【二分图染色+最大二分匹配数】
- hdu 5899 oasis in desert(acm/icpc 沈阳赛区网络赛,Floyd+二分图判定+最大匹配,好难啊)
- HDU 2444 The Accomodation of Students (二分图最大匹配+二分图染色)
- [HDOJ2444]The Accomodation of Students(二分图染色判定,最大匹配,匈牙利算法)
- HDU 2444 The Accomodation of Students 二分图判定+最大匹配
- 【2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛】 J Our Journey of Dalian Ends 【拆点费用流】
- HDU 2444 The Accomodation of Students (二分图存在的判定以及最大匹配数)
- hdu 2444 The Accomodation of Students 【二分图判断+求最大匹配】
- “亚信科技杯”南邮第七届大学生程序设计竞赛之网络预赛 A noj 2073 FFF [ 二分图最大权匹配 || 最大费用最大流 ]
- HDU 2444 The Accomodation of Students(判断是否是二分图及求最大匹配)
- 2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 HDU 6208 1003 The Dominator of Strings(AC自动机)
- 2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 HDU 6212 1007 Zuma (区间DP)