hdu 4751 Divide Groups bfs/dfs 连通性的问题 种子染色法
2016-05-25 22:07
429 查看
互斥集合,很常见的做法就是把不能在一起的点连上,然后进行染色,如果当前点颜色为1,那么与之相连的点的颜色必须为0。
关键在于判断是否会有一个点有矛盾。
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1954 Accepted Submission(s): 701
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
2013 ACM/ICPC Asia Regional Nanjing Online
Recommend
liuyiding | We have carefully selected several similar problems for you: 5701 5700 5699 5698 5697
Statistic | Submit | Discuss | Note
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 100 ;
int n;
bool know[maxn+10][maxn+10];
bool G[maxn+10][maxn+10];
int color[maxn+10];
void build()
{
for1(i,n)
{
for1(j,n) if(i!=j&& (!know[i][j] || !know[j][i]) )
{
G[i][j]=1;
}
}
}
bool bfs(int S)
{
queue<int >q;
color[S]=0;
q.push(S);
while(!q.empty())
{
int x=q.front();q.pop();
int tc=color[x]^1;
for1(y,n) if(G[x][y])
{
if(~color[y])
{
if(color[y]!=tc) return false;
else continue;
}
color[y]=tc;
q.push(y);
}
}
return true;
}
bool work()
{
memset(color,-1,sizeof color);
for1(i,n) if(color[i]==-1)
{
if(!bfs(i) ) return false;
}
return true;
}
int main()
{
int x;
while(~scanf("%d",&n))
{
memset(know,0,sizeof know);
memset(G,0,sizeof G);
for1(i,n)
{
while(~scanf("%d",&x)&&x)
{
know[i][x]=1;
}
}
build();
puts(work()?"YES":"NO");
}
return 0;
}
关键在于判断是否会有一个点有矛盾。
Divide Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1954 Accepted Submission(s): 701
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
2013 ACM/ICPC Asia Regional Nanjing Online
Recommend
liuyiding | We have carefully selected several similar problems for you: 5701 5700 5699 5698 5697
Statistic | Submit | Discuss | Note
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn= 100 ;
int n;
bool know[maxn+10][maxn+10];
bool G[maxn+10][maxn+10];
int color[maxn+10];
void build()
{
for1(i,n)
{
for1(j,n) if(i!=j&& (!know[i][j] || !know[j][i]) )
{
G[i][j]=1;
}
}
}
bool bfs(int S)
{
queue<int >q;
color[S]=0;
q.push(S);
while(!q.empty())
{
int x=q.front();q.pop();
int tc=color[x]^1;
for1(y,n) if(G[x][y])
{
if(~color[y])
{
if(color[y]!=tc) return false;
else continue;
}
color[y]=tc;
q.push(y);
}
}
return true;
}
bool work()
{
memset(color,-1,sizeof color);
for1(i,n) if(color[i]==-1)
{
if(!bfs(i) ) return false;
}
return true;
}
int main()
{
int x;
while(~scanf("%d",&n))
{
memset(know,0,sizeof know);
memset(G,0,sizeof G);
for1(i,n)
{
while(~scanf("%d",&x)&&x)
{
know[i][x]=1;
}
}
build();
puts(work()?"YES":"NO");
}
return 0;
}
相关文章推荐
- 格子刷油漆(DP)
- 在idea中运行tinysample的websample为何会报404错
- 新的开始 春光明媚
- Tomcat启动:A fatal error has been detected by the Java Runtime Environment(JVM Crash分析及相关资料)
- Java 之类的加载顺序
- Eclipse中的Maven插件运行项目中的Goals设置
- 利用 ACE 来实现 UDP 通讯
- how to improve working efficency
- 设计模式的学习——工厂模式
- JAVA泛型 一
- AsyncTask出现RejectExcetionExcetion原因
- Java Object
- 配置java 和tomacat环境过程
- 数据库中的分页查询rownum的使用
- yum
- 仿函数的使用
- CodeVS 1039 数的划分
- django中上传和下载的配置
- Zookeeper的核心:ZAB原子消息广播协议
- PAT乙级—1047. 编程团体赛(20)-native