The maximum number of matching groups for the West electric power 1228.

Source: Internet
Author: User

Problem 1228-maximum match Time Limit: 1000 MS Memory Limit: 65536KB Difficulty: Total Submit: 220 Accepted: 31 Special Judge: there are N people holding hands in nodejs (of course some people may not have hands with others). Now we need to group them. Only two people holding hands can be grouped into one group, how many groups can be divided?  Ps: because each person has only two hands, each person can only hold two people at most and Input Multiple groups of data. The first behavior of each group is two positive integers N, M, indicating N people and M holding hands. (1 <= N, M <= 10000) Next, Row M has two positive integers a, and B represents a and B holding hands (1 <= a, B <= n,! = B, ensure that a and B do not repeat) Output outputs a row of data for each group. a positive integer indicates the maximum number of groups that can be allocated. Sample Input3 31 22 31 3 Sample Output1HintSourcecyin: input n m n vertices m pairs input m pairs each pair means a B can be a group (a group can have only 2 people, in addition, one individual cannot be in two groups. Only two people can form one group. One person has only two hands and can only hold two people.) ask how many groups can be formed. The idea is: binary matching [cpp] # include <stdio. h> # include <iostream> # include <algorithm> # include <string. h ># include <vector> using namespace std; const int MAXN = 11111; // change int linker [MAXN] based on the number of points; bool used [MAXN]; vector <int> map [MAXN]; int uN; bool dfs (int u) {for (int I = 0; I <map [u]. size (); I ++) {if (! Used [map [u] [I]) {used [map [u] [I] = true; if (linker [map [u] [I] =-1 | dfs (linker [map [u] [I]) {linker [map [u] [I] = u; return true ;}}return false ;}int hungary () {int u; int res = 0; memset (linker,-1, sizeof (linker); for (u = 0; u <uN; u ++) {memset (used, false, sizeof (used )); if (dfs (u) res ++;} return res;} int main () {int u, k, v, I; int n, m; while (scanf ("% d", & n, & m )! = EOF) {for (I = 0; I <MAXN; I ++) map [I]. clear (); for (I = 0; I <m; I ++) {scanf ("% d", & v, & u); v = V-1; // if the point is counted from 1 to minus 1 if this sentence is removed from 0 and the following sentence u = U-1; map [u]. push_back (v); map [v]. push_back (u);} uN = n; printf ("% d \ n", hungary ()/2);} return 0 ;} since we can only hold two people's hands, we can use and query sets. Each person can only have one father's day, and only one son node can hold hands with each other. So there is a line for each online person. the result of dividing two online users into one group by two and all online users is [cpp] # include <stdio. h> # include <string. h> int ss [10022]; int a [10022]; int F (int x) {while (x! = A [x]) x = a [x]; return a [x];} int main () {int I, j, m, n; int x1, x2; while (scanf ("% d", & n, & m )! = EOF) {for (I = 1; I <= n; I ++) {a [I] = I; ss [I] = 0;} memset (ss, 0, sizeof (ss); while (m --) {scanf ("% d", & x1, & x2); x1 = f (x1 ); x2 = f (x2); if (x1 <x2) {a [x2] = x1;} else a [x1] = x2;} int s = 0; for (I = 1; I <= n; I ++) {ss [f (a [I])] ++;} for (I = 1; I <= n; I ++) {s = s + ss [I]/2;} printf ("% d \ n", s) ;}return 0 ;}

Related Article

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.