Divide Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 303 Accepted Submission(s): 111
Problem Description
![](https://i-blog.csdnimg.cn/blog_migrate/f5f6036cc2338f4f8be7fd0db6196e18.jpeg)
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.
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
题意:
给你一个图,问是否能把图分成两块,两块都要是完全图。
思路1:bfs
把一个点p放入一个集合,然后和p没有双向边的点都要放入另一个集合。如果在操作过程中有矛盾的情况则不满足条件。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 105
#define mod 1000000000
#define INF 0x3f3f3f3f
using namespace std;
int n,m,ans;
int c[maxn];
int city[maxn][maxn];
queue<int>q;
bool bfs(int pos)
{
int i,j,nx;
while(!q.empty()) q.pop();
q.push(pos);
while(!q.empty())
{
nx=q.front();
q.pop();
for(i=1;i<=n;i++)
{
if(i==nx||city[i][nx]&&city[nx][i]) continue ;// 是nx的朋友暂时不确定他属于哪个集合
if(c[i]==-1) // 不是nx的朋友肯定属于另一个集合
{
c[i]=c[nx]^1;
q.push(i);
}
else if(c[i]==c[nx]) return true ; // 不是nx朋友但和他属于同一集合就不满足条件
}
}
return false ;
}
int main()
{
int i,j,u,v;
while(~scanf("%d",&n))
{
memset(city,0,sizeof(city));
for(i=1;i<=n;i++)
{
while(scanf("%d",&v),v)
{
city[i][v]=1;
}
}
memset(c,-1,sizeof(c));
for(i=1;i<=n;i++)
{
if(c[i]==-1)
{
c[i]=0; // 默认在集合0 (这点表示不会证明)(︶︿︶)
if(bfs(i)) break ;
}
}
if(i<=n) printf("NO\n");
else printf("YES\n");
}
return 0;
}
思路2:dfs
数据比较水,直接暴力dfs都15ms过了。 +_+
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 105
#define mod 1000000000
#define INF 0x3f3f3f3f
using namespace std;
int n,m,ans,flag;
int a[maxn],b[maxn];
int city[maxn][maxn];
void dfs(int pos,int la,int lb)
{
if(flag) return ;
if(pos>n)
{
flag=1;
return ;
}
int i,j,flg1=0,flg2=0;
for(i=1;i<=la;i++)
{
if(!city[pos][a[i]]||!city[a[i]][pos])
{
flg1=1;
break ;
}
}
for(i=1;i<=lb;i++)
{
if(!city[pos][b[i]]||!city[b[i]][pos])
{
flg2=1;
break ;
}
}
if(flg1&&flg2) return ;
else if(!flg1&&flg2)
{
a[la+1]=pos;
dfs(pos+1,la+1,lb);
}
else if(flg1&&!flg2)
{
b[lb+1]=pos;
dfs(pos+1,la,lb+1);
}
else
{
a[la+1]=pos;
dfs(pos+1,la+1,lb);
b[lb+1]=pos;
dfs(pos+1,la,lb+1);
}
}
int main()
{
int i,j,u,v;
while(~scanf("%d",&n))
{
memset(city,0,sizeof(city));
for(i=1;i<=n;i++)
{
while(scanf("%d",&v),v)
{
city[i][v]=1;
}
}
flag=0;
a[1]=1;
dfs(2,1,0);
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}