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)