Problem Portal: https://www.luogu.org/problemnew/show/P1640
Title:
There are n weapons, and each weapon has two attributes. Now you are required to choose any attribute of a certain weapon (there is one and only one), and how many attributes can you choose at most to make their values is continuous.
Ideas:
Bipartite graph matching.
Note: break as soon as no consecutive matches can be found.
Code:
#include<cstdio> #include<cstring> struct node{int x,y,next;} a[2000010]; bool bz[1000010]; int last[1000010],match[1000010]; int m,len=0,ans; void ins(int x,int y) { a[++len].x=x;a[len].y=y;a[len].next=last[x];last[x]=len; } bool find(int x) { for(int i=last[x];i;i=a[i].next) { int y=a[i].y; if(bz[y]) { bz[y]=false; if(!match[y]||find(match[y])) { match[y]=x; return true; } } } return false; } intmain() { int x,y; scanf("%d",&m); for(int i=1;i<=m;i++) { scanf("%d %d",&x,&y); ins(x,i); ins(y,i); } for(int i=1;i<=10000;i++) { memset(bz,true,sizeof(bz)); if(find(i)) ans++; else break; } printf("%d",ans); }