【luogu P2071 座位安排】 题解
生活随笔
收集整理的這篇文章主要介紹了
【luogu P2071 座位安排】 题解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:https://www.luogu.org/problemnew/show/P2071#sub
鄰接表 + 匈牙利
把之前的鄰接矩陣匈牙利變成鄰接表
要不然存不下...
code:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 inline int read() 7 { 8 int ret=0; 9 char c=getchar(); 10 while (c<'0' || c>'9') c=getchar(); 11 while (c>='0' && c<='9'){ 12 ret=((ret<<3)+(ret<<1))+c-'0'; 13 c=getchar(); 14 } 15 return ret; 16 } 17 const int maxn = 4010; 18 int n, ans, cnt; 19 int link[maxn], head[maxn]; 20 bool vis[maxn]; 21 struct edge{ 22 int v,next; 23 }e[maxn<<2]; 24 void add(int u, int v) 25 { 26 e[++cnt].v = v; 27 e[cnt].next = head[u]; 28 head[u] = cnt; 29 } 30 bool dfs(int u) 31 { 32 int v = head[u]; 33 for(int i = v; i != 0; i = e[i].next) 34 { 35 if(!vis[e[i].v]) 36 { 37 vis[e[i].v] = 1; 38 if(!link[e[i].v] || dfs(link[e[i].v])) 39 { 40 link[e[i].v] = u; 41 return 1; 42 } 43 } 44 } 45 return 0; 46 } 47 int main() 48 { 49 int u,v; 50 n = read(); 51 for(int i = 1; i <= n*2; i++) 52 { 53 u = read(); v = read(); 54 add(i,u); 55 add(i,u+n); 56 add(i,v); 57 add(i,v+n); 58 } 59 for(int i = 1; i <= n*2; i++) 60 { 61 memset(vis,0,sizeof(vis)); 62 if(dfs(i)) ans++; 63 } 64 printf("%d",ans); 65 return 0; 66 }?
轉載于:https://www.cnblogs.com/MisakaAzusa/p/8858260.html
總結
以上是生活随笔為你收集整理的【luogu P2071 座位安排】 题解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【微服务】Spring Cloud 基础
- 下一篇: 财务管理【12】