Algorithm template question in Hungary
There are n courses, and each course may have different time periods. different time periods are equivalent.
Ask the maximum number of courses that can be selected without conflict.
Create a bipartite graph. One vertex indicates different courses, and the other vertex indicates the course time (hash ).
#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#define inf 0x3f3f3f3f#define eps 1e-6#define ll __int64using namespace std;int mp[310][310],used[310],link[310],n,m;bool dfs(int x){ for(int i=1;i<=m;i++) { if(!used[i]&&mp[x][i]) { used[i]=1; if(link[i]==-1||dfs(link[i])) { link[i]=x; return 1; } } } return 0;}int hungry(){ int res=0; memset(link,-1,sizeof link); for(int i=1;i<=n;i++) { memset(used,0,sizeof used); if(dfs(i)) res++; } return res;}int main(){ int i,a,b; while(~scanf("%d",&n)) { memset(mp,0,sizeof mp); for(i=1;i<=n;i++) { scanf("%d",&m); while(m--) { scanf("%d%d",&a,&b); mp[i][(a-1)*12+b]=1; } } m=7*12; printf("%d\n",hungry()); } return 0;}