POJ-1469 COURSES (Hungarian algorithm entry question)

Source: Internet
Author: User

Test instructions

P-Gate course, n students. For each course, students are asked whether they can choose a class representative for each course. The class representative must be the student who has chosen the course and each student can only be a subject.

Exercises

An introductory question for the Hungarian algorithm.

#include <iostream>#include<cstring>#include<cstdio>#include<vector>using namespacestd;Const intMAXN =1005;intT;intK, S;intFlag;intp, N;intVIS[MAXN];intMatch[maxn];vector<int>G[MAXN];voidinit () {flag=0; memset (Match,-1,sizeof(match));  for(inti =1; I <= p+n; i++) G[i].clear ();}BOOLDfsintu) {Vis[u]=1;  for(inti =0; I < g[u].size (); i++) {        intv = g[u][i], W =Match[v]; if(w<0|| !vis[w]&&DFS (W)) {Match[u]=v; MATCH[V]=u; return true; }    }    return false;}intHungarian () {intres =0;  for(intU =1; U <= s+p; u++) {        if(Match[u] <0) {memset (Vis,0,sizeof(VIS)); if(Dfs (U)) res++; }    }    returnRes;}intMain () {scanf ("%d", &t);  while(t--) {init (); scanf ("%d%d", &p, &N);  for(inti =1; I <= p; i++) {scanf ("%d", &k); if(!k) flag =1;  while(k--) {scanf ("%d", &s); G[i].push_back (S+p); G[s+P].push_back (i); }        }        if(Flag | | n<p) {puts ("NO"); Continue; }        intAns =Hungarian (); if(ans = = p) puts ("YES"); ElsePuts"NO"); }} 
View Code

POJ-1469 COURSES (Hungarian algorithm entry)

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.