Hdu4751 bipartite graph judgment, hdu4751 Bipartite Graph
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 4751
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
33 01 01 2 0
Sample Output
YES
/** Determination subject of the Binary Graph of hdu 4751 staining: Given a relationship of some characters, ask whether all people can be divided into two groups, so that each group knows each other's solution ideas: if two people do not know each other, create an edge, and then use the dyeing method to determine whether the image is a bipartite graph. */# include <stdio. h> # include <string. h >#include <iostream >#include <algorithm> using namespace std; const int maxn = 105; struct note {int v, next;} edge [maxn * maxn]; int head [maxn], ip; int color [maxn]; void init () {memset (head,-1, sizeof (head); ip = 0 ;} void addedge (int u, int v) {edge [ip]. v = v, edge [ip]. next = head [U], head [u] = ip ++;} bool dfs (int u, int col) {color [u] = col; for (int I = head [u]; I! =-1; I = edge [I]. next) {int v = edge [I]. v; if (color [v]! =-1) {if (color [v] = col) return false; continue;} if (! Dfs (v ,! Col) return false;} return true;} int n; int g [105] [105]; int main () {while (~ Scanf ("% d", & n) {memset (g, 0, sizeof (g); for (int I = 1; I <= n; I ++) {int x; while (~ Scanf ("% d", & x) {if (x = 0) break; g [I] [x] = 1 ;}} init (); for (int I = 1; I <= n; I ++) {for (int j = I + 1; j <= n; j ++) {if (g [I] [j] = 0 | g [j] [I] = 0) {addedge (I, j); addedge (j, i) ;}} memset (color,-1, sizeof (color); bool flag = true; for (int I = 1; I <= n; I ++) {if (color [I] =-1 & dfs (I, 0) = false) {flag = false; break ;}} if (flag) printf ("YES \ n"); else printf ("NO \ n");} return 0 ;}