http://acm.hrbeu.edu.cn/index.php?act=problem&proid=5087
之前完全不会的说....太弱小了
想了半天终于知道啥是染色....
不过现在会了,会了总比不会好-,-
题目的意思就是把学生分成2组,每组中的学生都不互相认识,可是2组间的任何学生都认识,很明显是匹配问题,比赛的时候瞬间被无数人秒杀..
具体思路代码写的非常清楚了...
- #include <iostream>
- using namespace std;
- #define N 201
- bool map[N][N],visit[N];
- int n,m,match[N],color[N];
- 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;
- }
- }