HDU 4751 (判断二分图)
2016-05-20 23:55
267 查看
Divide Groups
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1951 Accepted Submission(s): 699
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
题意:判断一个图能不能分成连个完全有向图。
枚举每一对点。如果这对点不是相互可达就建边,然后dfs染色判断是不是二分图。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
#define maxn 111
#define maxm 21111
bool mp[maxn][maxn];
struct node {
int u, v, next;
} edge[maxm];
int head[maxn], cnt;
int n;
void add_edge (int u, int v) {
edge[cnt].u = u, edge[cnt].v = v, edge[cnt].next = head[u], head[u] = cnt++;
}
int color[maxn];
bool dfs (int u, int flag) {
color[u] = flag;
flag ^= 1;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if (color[v] == -1) {
if (!dfs (v, flag)) {
return 0;
}
}
else {
if (color[v] == flag)
continue;
else {
return 0;
}
}
}
return 1;
}
void solve () {
for (int i = 1; i <= n; i++) {
for (int j = i+1; j <= n; j++) {
if (!mp[i][j] || !mp[j][i]) {
add_edge (i, j);
add_edge (j, i);
}
}
}
memset (color, -1, sizeof color);
for (int i = 1; i <= n; i++) if (color[i] == -1) {
if (!dfs (i, 1)) {
cout << "NO" << "\n";
return ;
}
}
cout << "YES" << "\n";
}
int main () {
while (cin >> n) {
memset (head, -1, sizeof head);
cnt = 0;
memset (mp, 0, sizeof mp);
for (int i = 1; i <= n; i++) {
int u;
while (scanf ("%d", &u) == 1 && u) {
mp[i][u] = 1;
}
}
solve ();
}
return 0;
}
相关文章推荐
- C++ STL 基础及应用(1) STL历史、组成和使用方法
- bzoj1076【SCOI2008】奖励关
- [javaSE] 网络编程(URLConnection)
- 利用函数实现华氏摄氏温度转换
- Hibernate一些概念
- bzoj3036 绿豆蛙的归宿
- android开发笔记之权重(layout_weight)
- 深入JVM系列之内存模型与内存分配及 AndroidGC原理
- 网络黑客攻防学习平台之基础关第六题
- tftp_nfs_samba搭建
- 广义线性模型4
- UNIX网络编程之线程浅析
- Iwfu-ListView(二) -BaseAdapter分析以及实现同一个ListView加载不同的布局
- c++作业6
- 4554: [Tjoi2016&Heoi2016]游戏 二分图匹配
- bzoj3450 Tyvj1952 Easy
- HDU 2121 Ice_cream’s world II(无固定根最小树形图、找最小根)
- 不如跳舞
- Beginner's Guide to Python-新手指导
- 数据分割,顿号替换成制表符