题目链接:
邻接表 + 匈牙利
把之前的邻接矩阵匈牙利变成邻接表
要不然存不下...
code:
1 #include2 #include 3 #include 4 #include 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 }