一、二分图判断
struct pointtype
{
vector<int> next;
int color;
}point[100001];
int n,m;
queue<int> q;
void init()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
point[i].next.clear();
point[i].color=-1;
}
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
point[x].next.push_back(y);
point[y].next.push_back(x);
}
return;
}bool judge()
{
for(int i=1;i<=n;i++)
{
while(!q.empty())
{
q.pop();
}
if(point[i].color==-1)
{
point[i].color=1;
}
q.push(i);
while(!q.empty())
{
int now=q.front();
int tip=point[now].color;
if(point[now].color==1)
{
tip=2;
}
else
{
tip=1;
}
vector<int>::iterator it;
for(it=point[now].next.begin();it!=point[now].next.end();it++)
{
if(point[*it].color!=-1)
{
if(point[*it].color!=tip)
{
return false;
}
}
else
{
point[*it].color=tip;
q.push(*it);
}
}
q.pop();
}
}
return true;
}
二、二分图匹配(匈牙利算法)
https://en.wikipedia.org/wiki/Hungarian_algorithm
1 //a,b数组分别是二分图左右两侧的点匹配到的点编号
2 bool work(int x)
3 {
4 vector<int>::iterator it;
5 for(it=map[x].begin();it!=map[x].end();it++)
6 {
7 if(vis[*it]==false)
8 {
9 vis[*it]=true;
10 if(b[*it]==0||work(b[*it]))
11 {
12 b[*it]=x;
13 a[x]=*it;
14 return true;
15 }
16 }
17 }
18 return false;
19 }