Given n tips and M questions, one of the two tips can be used for each question. The question must be answered continuously and the maximum number of questions can be answered.
The maximum matching of a bipartite graph. If a problem occurs, the two sides are connected to the given tip, and the Hungary algorithm searches for the augmented path. If the two sides are not found, the break
At first, I practiced a side between two tips, and then I got a second answer ...... Sorry ......
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 1010using namespace std;struct abcd{ int to,next;}table[M<<1];int head[M],tot;int n,m;int result[M],state[M],T;void Add(int x,int y){ table[++tot].to=y; table[tot].next=head[x]; head[x]=tot;}bool Hungary(int x){ int i; for(i=head[x];i;i=table[i].next) { if(state[table[i].to]==T) continue; state[table[i].to]=T; if( !result[table[i].to] || Hungary(result[table[i].to]) ) { result[table[i].to]=x; return true; } } return false;}int main(){ int i,x,y; cin>>n>>m; for(i=1;i<=m;i++) { scanf("%d%d",&x,&y); ++x;++y; Add(i,x);Add(i,y); ++T; if( !Hungary(i) ) break; } cout<<i-1<<endl;}
Bzoj 1191 hnoi2006 superhero hero bipartite graph maximum match