hdu 4751 Divide Groups(dfs染色 或 2-sat)

时间:2023-03-08 17:32:15
Problem Description
hdu 4751 Divide Groups(dfs染色  或  2-sat)  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染色法
 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 106
#define inf 1e12
int n;
vector<int>v[N];
int color[N];
int mp[N][N];
bool dfs(int u,int c){
color[u]=c;
for(int i=;i<v[u].size();i++){
int num=v[u][i];
if(color[num]!=-){
if(color[num]==c){
return false;
}
continue;
}
if(!dfs(num,!c)) return false;
}
return true;
}
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<N;i++){
v[i].clear();
}
memset(mp,,sizeof(mp));
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
while(x!=){
//v[i].push_back(x);
//v[x].push_back(i);
mp[i][x]=;
scanf("%d",&x);
}
} for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(mp[i][j]== || mp[j][i]==){
v[i].push_back(j);
v[j].push_back(i);
}
}
} memset(color,-,sizeof(color));
int flag=;
for(int i=;i<=n;i++){
if(color[i]==- && !dfs(i,)){
flag=;
break;
}
}
if(flag){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return ;
}

第二种是2-sat,其实本质上和上一种是一样的

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 106
#define inf 1e12
int n,m; int mp[N][N];
int tot;
int head[N];
int vis[N];
int tt;
int scc;
stack<int>s;
int dfn[N],low[N];
int col[N];
struct Node
{
int from;
int to;
int next;
}edge[N*N];
void init()
{
tot=;
scc=;
tt=;
memset(head,-,sizeof(head));
memset(dfn,-,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(col,,sizeof(col));
}
void add(int s,int u)//邻接矩阵函数
{
edge[tot].from=s;
edge[tot].to=u;
edge[tot].next=head[s];
head[s]=tot++;
}
void tarjan(int u)//tarjan算法找出图中的所有强连通分支
{
dfn[u] = low[u]= ++tt;
vis[u]=;
s.push(u);
int cnt=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dfn[v]==-)
{
// sum++;
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]==)
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int x;
scc++;
do{
x=s.top();
s.pop();
col[x]=scc;
vis[x]=;
}while(x!=u);
}
}
bool two_sat(){ for(int i=;i<*n;i++){
if(dfn[i]==-){
tarjan(i);
}
}
for(int i=;i<n;i++){
if(col[*i]==col[*i+]){
return false;
}
}
return true;
}
int main()
{
while(scanf("%d",&n)==){
init();
memset(mp,,sizeof(mp)); while(!s.empty()){
s.pop();
}
int a,b,c;
int x;
for(int i=;i<n;i++){
scanf("%d",&x);
while(x!=){
x--;
mp[i][x]=;
scanf("%d",&x);
}
}
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(i==j) continue;
if(mp[i][j]==){
add(*i,*j+);
add(*j,*i+);
add(*i+,*j);
add(*j+,*i);
}
}
}
if(two_sat()){
printf("YES\n");
}
else{
printf("NO\n");
}
}
return ;
}