2008年第33届ACM/ICPC亚洲区预赛(哈尔滨)网络预选赛 1007__The Accomodation of Students 二分图最大匹配+染色

来源:互联网 发布:python text 编辑:程序博客网 时间:2024/04/30 02:47

http://acm.hrbeu.edu.cn/index.php?act=problem&proid=5087

之前完全不会的说....太弱小了

想了半天终于知道啥是染色....

不过现在会了,会了总比不会好-,-

题目的意思就是把学生分成2组,每组中的学生都不互相认识,可是2组间的任何学生都认识,很明显是匹配问题,比赛的时候瞬间被无数人秒杀..

具体思路代码写的非常清楚了...

 

 

  1. #include <iostream>
  2. using namespace std;
  3. #define N 201
  4. bool map[N][N],visit[N];
  5. int n,m,match[N],color[N];
  6. bool dfs(int pre)
  7. {
  8.     for(int i=1;i<=m;i++)
  9.     {
  10.         if(map[pre][i]&&!visit[i])
  11.         {
  12.             visit[i]=true;
  13.             int t=match[i];
  14.             match[i]=pre;
  15.             if(t==-1||dfs(t)) return true;
  16.             match[i]=t;
  17.         }
  18.     }
  19.     return false;
  20. }
  21. int find_match()
  22. {
  23.     memset(match,-1,sizeof(match));
  24.     int i,sum=0;
  25.     for(i=1;i<=n;i++){memset(visit,false,sizeof(visit));if(dfs(i)) sum++;}
  26.     return sum;
  27. }
  28. int main()
  29. {
  30.     int x,y,t;
  31.     bool flag;
  32.     while(cin>>n>>t)
  33.         {
  34.             memset(map,false,sizeof(map));
  35.             memset(color,0,sizeof(color));
  36.             m=n;
  37.             flag=false;
  38.             while(t--)
  39.                 {
  40.                     cin>>x>>y;
  41.                     if(color[x]&&color[x]==color[y])flag=true;
  42.                     color[x]=1;
  43.                     color[y]=2;
  44.                     map[x][y]=true;
  45.                 }
  46.             if(flag)
  47.                 cout<<"No"<<endl;
  48.             else
  49.                 cout<<find_match()<<endl;
  50.         }
  51. }