【题解】座位安排
題目鏈接
題目大意就是,每個點連著兩條邊,就是人喜歡做的兩個位置,求所有座位匹配上的最大匹配
這里,我們開一個二維的就好,表示兩列的座位是否匹配上,返回01即可。
匈牙利模板題。
#include<cstdio> #include<iostream> #include<cstring> using namespace std; int match[500000][2]; int head[500000],n,ans; int vis[500000],tot; struct node{int nxt,to; }e[500000]; bool dfs(int x){for(int i=head[x];i;i=e[i].nxt){int j=e[i].to;if(vis[j])continue;vis[j]=1;if(!match[j][0]||dfs(match[j][0])){//j的第一個座位是不是已經匹配上 match[j][0]=x;return true;}if(!match[j][1]||dfs(match[j][1])){//同上 match[j][1]=x;return true;}}return false;//沒有匹配 } void work(){for(int i=1;i<=(n<<1);++i){memset(vis,0,sizeof(vis));ans+=dfs(i);//求最大匹配 } } inline void add(int x,int y){//臨接表 e[++tot].nxt=head[x];head[x]=tot;e[tot].to=y; } int main(){scanf("%d",&n);for(int i=1;i<=(n<<1);++i){//2n個點 int x,y;scanf("%d%d",&x,&y);add(i,x);add(i,y);//i想做的位置連邊 }work();printf("%d\n",ans);return 0; }?
轉載于:https://www.cnblogs.com/h-lka/p/11256487.html
總結
 
                            
                        - 上一篇: STM32F05x加入RDP(LV1)后
- 下一篇: 产品经理02_竞品分析
