我是靠谱客的博主 高挑花卷,最近开发中收集的这篇文章主要介绍hdu 4751 染色法判断二分图 Divide Groups,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Divide Groups

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2539    Accepted Submission(s): 906


Problem Description

  This year is the 60th anniversary of NJUST, and to make the celebration more colorful, Tom200 is going to invite distinguished alumnus back to visit and take photos.
  After carefully planning, Tom200 announced his activity plan, one that contains two characters:
  1. Whether the effect of the event are good or bad has nothing to do with the number of people join in.
   2. The more people joining in one activity know each other, the more interesting the activity will be. Therefore, the best state is that, everyone knows each other.
  The event appeals to a great number of alumnus, and Tom200 finds that they may not know each other or may just unilaterally recognize others. To improve the activities effects, Tom200 has to divide all those who signed up into groups to take part in the activity at different time. As we know, one's energy is limited, and Tom200 can hold activity twice. Tom200 already knows the relationship of each two person, but he cannot divide them because the number is too large.
  Now Tom200 turns to you for help. Given the information, can you tell if it is possible to complete the dividing mission to make the two activity in best state.
 

Input
  The input contains several test cases, terminated by EOF.
  Each case starts with a positive integer n (2<=n<=100), which means the number of people joining in the event.
  N lines follow. The i-th line contains some integers which are the id
of students that the i-th student knows, terminated by 0. And the id starts from 1.
 

Output
  If divided successfully, please output "YES" in a line, else output "NO".
 

Sample Input
3 3 0 1 0 1 2 0
 

Sample Output
YES
 

Source


把无相互关系的两个人分到两个集合


初涉染色法:

两种方法

dfs 

bfs

dfs:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
int n,x;
vector<int >v[103];
int m[103][103];
int color[103];
bool dfs(int x,int c)
{
color[x]=c;
int len=v[x].size();
for(int i=0; i<len; i++)
{
int tmp=v[x][i];
if(color[tmp]!=-1)
{
if(color[tmp]==c)
return false;
continue;
}
if(!dfs(tmp,!c))
return false;
}
return true;
}
bool solve()
{
int i,j;
memset(color,-1,sizeof(color));
for(int i=1; i<=n; i++)
{
if(color[i]==-1&&dfs(i,0)==false)
return false;
}
return true;
}
int main()
{
while(~scanf("%d",&n))
{
memset(m,0,sizeof(m));
for(int i=1; i<=n; i++)
{
v[i].clear();
while(scanf("%d",&x),x)
{
m[i][x]=1;
}
}
for(int i=1; i<=n; i++)
{
for(int j=i+1; j<=n; j++)
{
if(m[i][j]==0||m[j][i]==0)
{
v[i].push_back(j);
v[j].push_back(i);
}
}
}
if(solve())
printf("YESn");
else printf("NOn");
}
}



bfs:

#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>
#include<queue>
#include<string.h>
using namespace std;
int m[103][103];
int color[103];
queue<int>q;
int n;
bool bfs(int x)
{
int i,j,tmp;
while(!q.empty())q.pop();
q.push(x);
while(!q.empty())
{
tmp=q.front();
q.pop();
for(i = 1; i <= n; i++)
{
if(i==tmp||m[i][tmp]&&m[tmp][i])continue;
if(color[i]==-1)
{
color[i]=color[tmp]^1;
q.push(i);
}
else if(color[i]==color[tmp])return true;
}
}
return false;
}
int main()
{
int i,j,u,v,x;
while(~scanf("%d",&n))
{
memset(m,0,sizeof(m));
memset(color,-1,sizeof(color));
for(int i=1;i<=n;i++)
{
while(scanf("%d",&x),x)
m[i][x]=1;
}
for(i=1;i<=n;i++)
{
if(color[i]==-1)
{
color[i]=0;
if(bfs(i))break;
}
}
if(i<=n)
printf("NOn");
else printf("YESn");
}
}



最后

以上就是高挑花卷为你收集整理的hdu 4751 染色法判断二分图 Divide Groups的全部内容,希望文章能够帮你解决hdu 4751 染色法判断二分图 Divide Groups所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部