Hdoj 2444-the accomodation of students determine whether an image is a bipartite graph. Condition of a Bipartite Graph

Source: Internet
Author: User

Question:

Some students are friends (links cannot be transferred )... now we want to divide a bunch of students into two groups so that there is no friend between the students in the same group .. if it succeeds .. ask me to lock every friend in the dark room .. maximum number of Pairs

Question:

This question reflects the basic conditions of the Bipartite Graph Model... there are two parts .. there is no edge inside. the side is only between two parts... first ask .. is to judge the bipartite graph .. just use the dyeing method .. the next question is the naked Hungary...

Program:

# Include <iostream> # include <stdio. h> # include <algorithm> # include <cmath> # include <stack> # include <string. h >#include <queue> # define ll long # define ESP 1e-5 # define maxn 202 # define maxm 50000000 # define oo 100000007 using namespace STD; int color [maxn], match [maxn]; bool used [maxn]; vector <int> T [maxn]; bool putcolor (int x, int TP) {If (color [x] = 1-tp) return false; If (color [x] = TP) return true; color [x] = TP; For (INT I = 0; I <t [X]. Size (); I ++) if (! Putcolor (T [x] [I],! TP) return false; return true;} bool judge (int n) {memset (color,-1, sizeof (color); For (INT I = 1; I <= N; I ++) if (color [I] =-1 &&! Putcolor (I, 0) return false; return true;} bool DFS (int x) {for (INT I = 0; I <t [X]. size (); I ++) {int y = T [x] [I]; If (used [y]) continue; used [y] = true; If (! Match [y] | DFS (Match [y]) {match [y] = x; return true ;}return false ;} int getmax (int n) {int sum = 0; memset (match, 0, sizeof (MATCH); For (INT I = 1; I <= N; I ++) if (color [I] = 1) {memset (used, false, sizeof (used); sum + = DFS (I) ;}return sum ;} int main () {int I, n, m; while (~ Scanf ("% d", & N, & M) {for (I = 1; I <= N; I ++) T [I]. clear (); While (M --) {int X, Y; scanf ("% d", & X, & Y); t [X]. push_back (Y), t [Y]. push_back (x);} If (! Judge (N) {printf ("NO \ n"); continue;} printf ("% d \ n", getmax (n);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.