联通图判断二分图
#include<cstdio>
#include<algorithm>
#define N 42000
int next[N],to[N],num,head[N],col[N],flag,n,m,a,b;
void add(int false_from,int false_to){
next[++num]=head[false_from];
to[num]=false_to;
head[false_from]=num;
}
void dfs(int x,int color){
col[x]=color;
for(int i=head[x];i;i=next[i]){
if(col[to[i]]==col[x]){
printf("NO");
flag=1;
exit(0);
}
if(!col[to[i]])
dfs(to[i],-color);
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i){
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
dfs(1,1);
if(!flag)
printf("YES");
return 0;
}
非连通图
#include<cstdio>
#include<algorithm>
#define N 42000
int next[N],to[N],num,head[N],col[N],flag,n,m,a,b;
void add(int false_from,int false_to){
next[++num]=head[false_from];
to[num]=false_to;
head[false_from]=num;
}
void dfs(int x,int color){
col[x]=color;
for(int i=head[x];i;i=next[i]){
if(col[to[i]]==col[x]){
printf("NO");
flag=1;
exit(0);
}
if(!col[to[i]])
dfs(to[i],-color);
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i){
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
for(int i=1;i<=n&&!flag;++i)
if(!col[i])
dfs(i,1);
if(!flag)
printf("YES");
return 0;
}
过山车Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 35852 Accepted Submission(s): 15239 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了。可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐。但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或PQK做partner,Grass只愿意和linle或LL做partner,PrincessSnow愿意和水域浪子或伪酷儿做partner。考虑到经费问题,boss刘决定只让找到partner的人去坐过山车,其他的人,嘿嘿,就站在下面看着吧。聪明的Acmer,你可以帮忙算算最多有多少对组合可以坐上过山车吗?
Input 输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数。0<K<=1000
Output 对于每组数据,输出一个整数,表示可以坐上过山车的最多组合数。
Sample Input 6 3 3 1 1 1 2 1 3 2 1 2 3 3 1 0
Sample Output 3
Author PrincessSnow
Source
Recommend lcy |
#include <algorithm> //STL通用算法
#include <bitset> //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex> //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque> //STL双端队列容器
#include <exception> //异常处理类
#include <fstream>
#include <functional> //STL定义运算函数(代替运算符)
#include <limits>
#include <list> //STL线性列表容器
#include <map> //STL 映射容器
#include <iomanip>
#include <ios> //基本输入/输出支持
#include<iosfwd> //输入/输出系统使用的前置声明
#include <iostream>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <queue> //STL队列容器
#include <set> //STL 集合容器
#include <sstream> //基于字符串的流
#include <stack> //STL堆栈容器
#include <stdexcept> //标准异常类
#include <streambuf> //底层输入/输出支持
#include <string> //字符串类
#include <utility> //STL通用模板类
#include <vector> //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 600+66;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
int n,m,k;
bool mark[maxn][maxn];
bool vis[maxn];
int pre[maxn];
int dfs(int x)
{
for(int i=1;i<=n;i++)
{
if(!vis[i]&&mark[x][i])
{
vis[i]=1;
if(!pre[i]||dfs(pre[i]))
{
pre[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
while(scanf("%d",&k)&&k)
{
scanf("%d %d",&m,&n);
memset(mark,false,sizeof(mark));
memset(vis,false,sizeof(vis));
memset(pre,false,sizeof(pre));
int cnt=0;
for(int i=1; i<=k; i++)
{
int u,v;
scanf("%d %d",&u,&v);
mark[u][v]=1;//类似于有向图
}
for(int i=1;i<=m;i++)//让女生去匹配男生
{
memset(vis,false,sizeof(vis));
if(dfs(i))cnt++;//匹配成功
}
printf("%d\n",cnt);
}
}
非常好的解释。
https://blog.csdn.net/mengxiang000000/article/details/50353166
放一个判定二分图模板。