hdu 4751 Divide Groups bfs 搜索 解题报告
来源:互联网 发布:java图片上传跨域请求 编辑:程序博客网 时间:2024/06/06 04:15
Divide Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1969 Accepted Submission(s): 708
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.
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
33 01 01 2 0
Sample Output
YES
题意:将n个人分成两组,使得每组中的每个人都认识剩余所有人。
首先,只保留两个都认识的边,vis标记分组,0组与1组。
然后,bfs扫一遍,遇到矛盾的就直接NO。
代码:
#include <iostream>#include <cstring>#include <string>#include <queue>#include <cstdio>#include <algorithm>using namespace std;int n, t;int z[110][110], vis[110];int bfs(int s){ queue<int>Q; Q.push(s); vis[s] = 1; while(!Q.empty()) { int now = Q.front(); Q.pop(); for(int i = 1; i <= n; i++) { if(z[now][i] == 1 || now == i) continue; if(vis[i] == -1) { vis[i] = 1 - vis[now]; Q.push(i); } else if(vis[i] == vis[now]) return 0; } } return 1;}int main(){ int i; while(scanf("%d", &n) != EOF) { memset(z, 0, sizeof(z)); memset(vis, -1, sizeof(vis)); for(i = 1; i <= n; i++) { while(scanf("%d", &t), t) z[i][t] = 1; } //如果不是相互认识,都改为不认识 for(i = 1; i <= n; i++) for(int j = 1; j <= n; j++) if(!z[i][j]) z[j][i] = 0; for(i = 1; i <= n; i++) { if(vis[i] != -1) continue; if(!bfs(i)) break; } if(i <= n) printf("NO\n"); else printf("YES\n"); } return 0;}
0 0
- hdu 4751 Divide Groups bfs 搜索 解题报告
- HDU 4751 Divide Groups (BFS)
- HDU 4751 Divide Groups
- HDU-4751-Divide Groups
- HDU 4751 Divide Groups
- HDU:4751 Divide Groups
- HDU-4751 Divide Groups
- HDU 4751 Divide Groups
- hdu 4751 Divide Groups
- HDU 4751 Divide Groups
- hdu 4751 Divide Groups 染色
- HDU 4751 — Divide Groups
- HDU4751 Divide Groups---bfs
- hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)
- hdu 4751 Divide Groups bfs (2013 ACM/ICPC Asia Regional Nanjing Online 1004)
- hdu 4751 Divide Groups (两种解法 1.bfs 2.dfs)
- hdu 4751 Divide Groups bfs/dfs 连通性的问题 种子染色法
- hdu 1548 A strange lift bfs搜索 解题报告
- 自定义Push和Pop过渡动画
- java泛型通配符示例
- 泛型的嵌套设置
- C++ virtual关键字深入理解
- Win10Matlab7Runtime Error
- hdu 4751 Divide Groups bfs 搜索 解题报告
- 传球游戏_vijos1485
- 构造方法中super()的作用
- Codeforces 689C Mike and Chocolate Thieves (二分)
- Qt学习笔记——QTableWidget使用问题
- 异常处理基础(C#参考)
- 四大内部类
- leetcode——Isomorphic Strings
- 领域建模有很多种方法,对于同样的问题域使用不同的建模手段得到的模型可能也不尽相同。于是我经常听到这样一个问题:怎么才能保证建模的正确性?