hdu4751Divide Groups【判断二分图】
2016-04-19 11:08
260 查看
Total Submission(s): 1904 Accepted Submission(s): 676
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
If divided successfully, please output "YES" in a line, else output "NO".
[align=left]Sample Input[/align]
3
3 0
1 0
1 2 0
[align=left]Sample Output[/align]
YES
[align=left]Source[/align]
2013 ACM/ICPC Asia Regional Nanjing Online
[align=left]Recommend[/align]
liuyiding | We have carefully selected several similar problems for you: 5669 5668 5667 5666 5665
自己好菜啊QAQ 题意:给出彼此认识的关系(不传递),问是否可以分成两部分使得两部分中都互相认识
裸的判断二分图,模板手误写错了两个单词,最大的问题是最开始为标记数组赋值的时候是单向的!加边的时候是双向的==
再有,难道不应该染色的时候只在for循环外面只赋值一次吗?为什么写里面写外面都对啊==
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int col[200],n,x;
vector<int>G[200];
bool flag[200][200];
bool dfs(int u,int color)
{
col[u]=color;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(col[v]==color) return false;
if(col[v]==-1)
{
if(!dfs(v,!color)) return false;
}
}
return true;
}
int main()
{
// freopen("cin.txt","r",stdin);
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++) G[i].clear();
memset(flag,0,sizeof(flag));
for(int i=1;i<=n;i++)
{
while(scanf("%d",&x)&&x)
{
flag[i][x]=1;
// flag[x][i]=1;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(0==flag[i][j]||0==flag[j][i])
{
G[i].push_back(j);G[j].push_back(i);
}
}
}
bool fl=true;
memset(col,-1,sizeof(col));
for(int i=1;i<=n&&fl;i++)
{
if(col[i]==-1)
{
if(!dfs(i,0)) fl=false;
}
}
if(fl) puts("YES");
else puts("NO");
}
return 0;
}
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
If divided successfully, please output "YES" in a line, else output "NO".
[align=left]Sample Input[/align]
3
3 0
1 0
1 2 0
[align=left]Sample Output[/align]
YES
[align=left]Source[/align]
2013 ACM/ICPC Asia Regional Nanjing Online
[align=left]Recommend[/align]
liuyiding | We have carefully selected several similar problems for you: 5669 5668 5667 5666 5665
自己好菜啊QAQ 题意:给出彼此认识的关系(不传递),问是否可以分成两部分使得两部分中都互相认识
裸的判断二分图,模板手误写错了两个单词,最大的问题是最开始为标记数组赋值的时候是单向的!加边的时候是双向的==
再有,难道不应该染色的时候只在for循环外面只赋值一次吗?为什么写里面写外面都对啊==
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int col[200],n,x;
vector<int>G[200];
bool flag[200][200];
bool dfs(int u,int color)
{
col[u]=color;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(col[v]==color) return false;
if(col[v]==-1)
{
if(!dfs(v,!color)) return false;
}
}
return true;
}
int main()
{
// freopen("cin.txt","r",stdin);
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++) G[i].clear();
memset(flag,0,sizeof(flag));
for(int i=1;i<=n;i++)
{
while(scanf("%d",&x)&&x)
{
flag[i][x]=1;
// flag[x][i]=1;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(0==flag[i][j]||0==flag[j][i])
{
G[i].push_back(j);G[j].push_back(i);
}
}
}
bool fl=true;
memset(col,-1,sizeof(col));
for(int i=1;i<=n&&fl;i++)
{
if(col[i]==-1)
{
if(!dfs(i,0)) fl=false;
}
}
if(fl) puts("YES");
else puts("NO");
}
return 0;
}
相关文章推荐
- 蘑菇街笔试--剪桌腿的最小代价
- Windows下使用wxPython的pycrust交互shell方法。运行pywrap命令
- windows命令
- iOS开发 贝塞尔曲线UIBezierPath
- 服务器返回的HTTP状态码和与之关联的消息
- 黄聪:说说JSON和JSONP,也许你会豁然开朗(转)
- JS字符串拼接优化
- Apache + Tomcat +mod_jk实现集群服务
- (01)JS大法好,JavaScript一统天下开篇
- group by 注意
- js格式化日期函数
- win7系统强制关机后出现黑屏无反应现象的三种解决方法
- Android中的windowSoftInputMode属性详解
- 使用Eclipse构建Maven项目 (step-by-step)
- Win7系统中如何用命令行方式查看JDK版本
- Linux开机自启动总结
- 线性代数常用基本知识 (含向量和矩阵范数<Matrix or vector norm>)
- 第4周 C语言及程序设计提高例程-3 体验自定义函数
- mpandroidchat实例 仿360你财富
- c#开发_Dev的关于XtraGrid的使用(GridControl小结)_First